zebra striping after sorting wx.listctrl

Hi list,

I have an application where the lines in a listctrl are coloured in a zebra way. I use the following code:

def SetBackgroundLines(self):

n = 0

i = self.GetTopItem()

while n < self.GetItemCount():

i = self.GetNextItem(i)

if i < 0:

break

if i % 2 == 1:

self.SetItemBackgroundColour(i,self.bgcolour)

else:

self.SetItemBackgroundColour(i, self.stdcolour)

self.RefreshItem(i)

n += 1

self.Refresh()

This works OK the first time. Shows nicely.

However when an item is added to the list (appended) and the list is sorted, the itemnumbers do not change hence the order of processing does not change which leads to a wrong backgroundcolour (see the added screenshot). I simply don’t know how to read the listitems in the order that they are displayed.

Thanks in advance

Cheers,

D. Kniep

Schermafdruk van 2013-12-17 17:12:28.png

Use ListRowHighlighter-mixin.

···

Am Dienstag, 17. Dezember 2013 17:24:34 UTC+1 schrieb Dick Kniep:

Hi list,

I have an application where the lines in a listctrl are coloured in a zebra way. I use the following code:

def SetBackgroundLines(self):

n = 0

i = self.GetTopItem()

while n < self.GetItemCount():

i = self.GetNextItem(i)

if i < 0:

break

if i % 2 == 1:

self.SetItemBackgroundColour(i,self.bgcolour)

else:

self.SetItemBackgroundColour(i, self.stdcolour)

self.RefreshItem(i)

n += 1

self.Refresh()

This works OK the first time. Shows nicely.

However when an item is added to the list (appended) and the list is sorted, the itemnumbers do not change hence the order of processing does not change which leads to a wrong backgroundcolour (see the added screenshot). I simply don’t know how to read the listitems in the order that they are displayed.

Thanks in advance

Cheers,

D. Kniep

Hi Torsten,

Thanks for the answer. This works nicely for regular lists. However, I am using a virtual list and with that list, ListRowHighlighter-mixin does not seem to work. Or do I need to do something special?

···

Op dinsdag 17 december 2013 17:24:34 UTC+1 schreef Dick Kniep:

Hi list,

I have an application where the lines in a listctrl are coloured in a zebra way. I use the following code:

def SetBackgroundLines(self):

n = 0

i = self.GetTopItem()

while n < self.GetItemCount():

i = self.GetNextItem(i)

if i < 0:

break

if i % 2 == 1:

self.SetItemBackgroundColour(i,self.bgcolour)

else:

self.SetItemBackgroundColour(i, self.stdcolour)

self.RefreshItem(i)

n += 1

self.Refresh()

This works OK the first time. Shows nicely.

However when an item is added to the list (appended) and the list is sorted, the itemnumbers do not change hence the order of processing does not change which leads to a wrong backgroundcolour (see the added screenshot). I simply don’t know how to read the listitems in the order that they are displayed.

Thanks in advance

Cheers,

D. Kniep

Hi,

...

However when an item is added to the list (appended) and the list is sorted, the itemnumbers do not change hence the order of processing does not change which leads to a wrong backgroundcolour (see the added screenshot). I simply don't know how to read the listitems in the order that they are displayed.

Another alternative to the mixin pointed out by Torsten is the ObjectListView - Learning to cook — ObjectListView v1.2 documentation

Werner

···

On 17/12/2013 17:24, Dick Kniep wrote:

Thanks for the answer. This works nicely for regular lists. However, I am using a virtual list and with that list, ListRowHighlighter-mixin does not seem to work. Or do I need to do something special?

I haven’t used virtual listctrl. But reading the docs i think you need to implement ListCtrl.OnGetItemAttr and return a ListItemAttr with the backgroundcolor set depending on the item.

# in listctrl __init__
self.attr1 = wx.ListItemAttr()
self.attr1.SetBackgroundColour("yellow")
self.attr2 = wx.ListItemAttr()
self.attr2.SetBackgroundColour("light blue")
def OnGetItemAttr(self, item):
    if item % 2:
        return self.attr1
    else:
        return self.attr2

The above code is from the wxPython demo.

Hey Torsten and Werner,

I have solved the problem now, so I thank you both for your valuable answers.

Cheers and I wish you well in 2014

D.Kniep

···

Op woensdag 18 december 2013 11:40:01 UTC+1 schreef Torsten:

Thanks for the answer. This works nicely for regular lists. However, I am using a virtual list and with that list, ListRowHighlighter-mixin does not seem to work. Or do I need to do something special?

I haven’t used virtual listctrl. But reading the docs i think you need to implement ListCtrl.OnGetItemAttr and return a ListItemAttr with the backgroundcolor set depending on the item.

# in listctrl __init__
self.attr1 = wx.ListItemAttr()
self.attr1.SetBackgroundColour("yellow")
self.attr2 = wx.ListItemAttr()
self.attr2.SetBackgroundColour("light blue")
def OnGetItemAttr(self, item):
    if item % 2:
        return self.attr1
    else:
        return self.attr2

The above code is from the wxPython demo.