![](/img/trans.png)
[英]wxPython - HyperTreeList Item BackgoundColour doesn't apply to full row
[英]wxPython ListCtrl item. SetBackgroundColour doesn't work
我在 MacBook Air M1、macOS Big Sur 上使用 Python3、wxPython 4。 我发现 SetBackgroundColour 方法不起作用,(但是当我调用 item.SetText("888") 时,文本已成功更新)有人知道原因吗? 谢谢!
import wx
import wx.lib.mixins.listctrl as listmix
class MyPanel(wx.Panel):
def __init__(self, parent):
wx.Panel.__init__(self, parent)
self.SetBackgroundColour("white")
rows = [("Tom", "11"),
("Ede", "11"),
("Jerry", "10")
]
self.list_ctrl = wx.ListCtrl(self, style=wx.LC_REPORT)
self.list_ctrl.SetBackgroundColour(wx.Colour(255,246,189,255))
self.list_ctrl.SetTextColour('black')
self.list_ctrl.InsertColumn(0, "Col1")
self.list_ctrl.InsertColumn(1, "Col2") # Revenue this Q than last Q
index = 0
for row in rows:
self.list_ctrl.InsertStringItem(index, row[0])
self.list_ctrl.SetStringItem(index, 1, row[1])
if int(row[1]) <= 10: #current PE is 50% down than 52w high
item = self.list_ctrl.GetItem(1,1)
item.SetText("888") # successfully changed
item.SetBackgroundColour('Blue') #not changed
self.list_ctrl.SetItem(item)
print(item.GetText())
print(item.GetBackgroundColour())
index += 1
sizer = wx.BoxSizer(wx.VERTICAL)
sizer.Add(self.list_ctrl, 0, wx.ALL|wx.EXPAND, 5)
self.SetSizer(sizer)
class MyFrame(wx.Frame):
def __init__(self):
"""Constructor"""
wx.Frame.__init__(self, None, wx.ID_ANY,"test")
panel = MyPanel(self)
self.Show()
if __name__ == "__main__":
app = wx.App(False)
frame = MyFrame()
app.MainLoop()
这可能看起来违反直觉,但似乎 BackGroundColour 属于 ListCtrl 而不是 ListCtrlItem。
因此,如果您通过 ListCtrl 访问它,它就可以工作。
例如
import wx
import wx.lib.mixins.listctrl as listmix
class MyPanel(wx.Panel):
def __init__(self, parent):
wx.Panel.__init__(self, parent)
self.SetBackgroundColour("white")
rows = [("Tom", "11"),
("Ede", "11"),
("Jerry", "10")
]
self.list_ctrl = wx.ListCtrl(self, style=wx.LC_REPORT)
self.list_ctrl.SetBackgroundColour(wx.Colour(255,246,189,255))
self.list_ctrl.SetTextColour('black')
self.list_ctrl.InsertColumn(0, "Col1")
self.list_ctrl.InsertColumn(1, "Col2") # Revenue this Q than last Q
index = 0
for row in rows:
self.list_ctrl.InsertStringItem(index, row[0])
self.list_ctrl.SetStringItem(index, 1, row[1])
if int(row[1]) <= 10: #current PE is 50% down than 52w high
item = self.list_ctrl.GetItem(1,1)
item.SetText("888") # successfully changed
self.list_ctrl.SetItemBackgroundColour(item.GetId(), 'LightBlue') #not changed
self.list_ctrl.SetItem(item)
print(item.GetText())
print(item.GetBackgroundColour())
print(self.list_ctrl.GetItemBackgroundColour(item.GetId()))
index += 1
sizer = wx.BoxSizer(wx.VERTICAL)
sizer.Add(self.list_ctrl, 0, wx.ALL|wx.EXPAND, 5)
self.SetSizer(sizer)
class MyFrame(wx.Frame):
def __init__(self):
"""Constructor"""
wx.Frame.__init__(self, None, wx.ID_ANY,"test")
panel = MyPanel(self)
self.Show()
if __name__ == "__main__":
app = wx.App(False)
frame = MyFrame()
app.MainLoop()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.