wxPython ListCtrl 项。设置背景颜色不起作用

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()

我重新访问了这个,因为你的代码做了一些我没有考虑到的摆弄。
我还解决了您可能会收到的弃用警告。
简而言之,通过 ListCtrl 访问项目并使用 index.

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", "10"),
                ("Ede", "11"),
                ("Jerry", "12"),
                ("Rolf", "10"),
                ("Neo", "12")
                ]
        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.InsertItem(index, row[0])
            self.list_ctrl.SetItem(index, 1, row[1])

            if int(row[1]) <= 10: #current PE is 50% down than 52w high
                self.list_ctrl.SetItemBackgroundColour(index, 'LightGreen')

            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()