wxpython ListCtrl无法更改所选项目的颜色

时间:2019-12-28 07:40:14

标签: python wxpython listctrl

作为标题,我不明白为什么我不能更改所选项目的背景颜色。这是我的代码...

self.listaFatture = wx.ListCtrl(self, size=(frame.GetSize().Width - 30, frame.GetSize().Height - 250), style=wx.LC_REPORT)
.....

self.listaFatture.Bind(wx.EVT_LIST_ITEM_SELECTED, self.SelezionaFatturaColore)

.....

def SelezionaFatturaColore(self,event):
    print('Selezionato')
    index= event.GetIndex()
    self.listaFatture.SetItemBackgroundColour(index, 'gray')
    self.listaFatture.SetItemTextColour(index, 'black')

事件被触发但颜色没有改变

在此先感谢所有尝试帮助我的人

1 个答案:

答案 0 :(得分:0)

我找到了解决方法,我不知道它是否是错误。

我将变量设置为-1,然后,当我选择一行时,我记得索引,我在设置颜色后取消了对广告的选择...

阅读代码更简单...

self.lastSelected = -1
....
self.listaFatture.Bind(wx.EVT_LIST_ITEM_SELECTED, self.SelezionaFatturaColore)
....
def SelezionaFatturaColore(self,event):
    print('Selezionato')
    index= event.GetIndex()
    if self.lastSelected > -1:
        if self.lastSelected % 2 == 0:
           self.listaFatture.SetItemBackgroundColour(self.lastSelected, wx.Colour(255,255,255,255))
        else:
            self.listaFatture.SetItemBackgroundColour(self.lastSelected, wx.Colour(152,245,255,255))

    self.listaFatture.SetItemState(index, 0, wx.LIST_STATE_SELECTED)

    self.listaFatture.SetItemBackgroundColour(index, wx.Colour(128,128,128,255))
    self.listaFatture.SetItemTextColour(index, wx.Colour(0,0,0,255))
    self.lastSelected = index