简体   繁体   中英

wxPython ListCtrl item. SetBackgroundColour doesn't work

I'm using Python3, wxPython 4 on MacBook Air M1, macOS Big Sur. I found the SetBackgroundColour method doesn't work, (but when I call item.SetText("888"), the text has been successfully updated ) Does anyone know the reason? Thanks!

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

It may seem counter-intuitive but it appears the BackGroundColour belongs to the ListCtrl and not the ListCtrlItem.
So if you access it via the ListCtrl it works.
eg

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

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM