wxpython ListCtrl can't change selected item colour

1k views Asked by At

As the title, I don't understand why I can't change selected item background color. Here my code...

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

The event is fired but the colors doesn't change

Thanks in advance to all that try to help me

1

There are 1 answers

1
luna80 On

I found a workaround, I don't know if is a bug or not.

I set a variable with -1 and after, when I select a row I remember the index, I deselect it ad after I set the color...

More simple to read the code...

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