wxPython ListCtrl:写彩色文本

3 python wxpython

试图将字符串写入ListCtrl,我完全不理解逻辑.这是正确的方法吗?

    self.rightPanel = wx.ListCtrl(spliter, -1, style=wx.LC_REPORT)
    self.rightPanel.InsertColumn(0, 'LineNumber')
    self.rightPanel.InsertColumn(1, 'log')
    self.rightPanel.SetColumnWidth(0, 8)
    self.rightPanel.SetColumnWidth(1, 80)

def writeConsole(self,str):
    item = wx.ListItem()
    item.SetText(str)
    item.SetTextColour(wx.RED)
    item.SetBackgroundColour(wx.BLACK)                    
    index = self.rightPanel.GetItemCount()        
    self.rightPanel.InsertItem(item)
    self.rightPanel.SetStringItem(index, 0, str(index))
    self.rightPanel.SetStringItem(index, 1, item.GetText())
Run Code Online (Sandbox Code Playgroud)

1 - 为什么文字没有彩色显示?
2 - 为什么ListCtrl中有2种不同的显示文本方法?

   ListCtrl.InsertItem()
   ListCtrl.SetStringItem()
Run Code Online (Sandbox Code Playgroud)

我认为InsertItem只是将项目加载到list.SetString但显示项目内容.(不确定)

joa*_*uin 5

SetTextColour()并且SetBackgroundColour()是整个listctrl的方法,而不是项目的方法.对于您应该使用的项目(仅对报告模式有效):

GetItemTextColour(idx_item)
SetItemTextColour(idx_item, col)
Run Code Online (Sandbox Code Playgroud)

InsertItem(index, item)(item是一个实例wx.ListItem)是InsertItem()在ListCtrl上添加新行的方法之一.

SetStringItem(index, col, label, imageId=-1)(其中index和col参数是单元格的行索引和列索引)允许在任何选定列中设置字符串.其他插入方法仅适用于第一列.

参考: wxPython in Action,Noel Rappin和Robin Dunn.