checklistbox SetItemBackgroundColour() doesn't work under linux(ubuntu)

I am developing an application that uses a CheckListBox.
With the SetItemBackground() method, I apply a color to the item of
CheckListBox.
My code works perfectly under Windows, but I get no result, under
Linux.
What is the problem?

Here's a semplified example

import wx

class CLBColorModel():
    def __init__(self):
        self.list = ['1', '2', '3']

class CLBColorView(wx.Frame):
    def __init__(self, parent):
        wx.Frame.__init__(self, None, -1, 'BG Color', size=(200, 300))
    def createWidgets(self, list):
        self.panel = wx.Panel(self, -1)
        self.clb = wx.CheckListBox(self.panel, -1, size = ( 100,
150 ), choices = list, style = wx.LB_HSCROLL)
        self.btn_exit = wx.Button(self.panel, wx.ID_ANY, 'Exit')
    def SizerizeWidgets(self):
        '''posiziona i vari widgets nella posizione stabilita'''
        self.vbox = wx.BoxSizer(wx.VERTICAL)
        self.vbox.Add(self.clb, 0, wx.ALL|wx.CENTER, 5)
        self.vbox.Add(wx.StaticLine(self.panel,), 0, wx.ALL|wx.EXPAND,
5)
        self.vbox.Add(self.btn_exit, 0, wx.CENTER)
        self.panel.SetSizer(self.vbox)
        self.Centre()

class CLBColorController:
    def __init__(self):
        '''Costruttore controller'''
        self.model = CLBColorModel()
        self.createView()

    def createView(self):
        '''crea la view e la rende visibile'''
        self.view = CLBColorView(None)
        self.view.createWidgets(self.model.list)
        self.view.SizerizeWidgets()
        self.view.Show()
        for item in self.view.clb.GetItems():
            index = (self.model.list.index(item))
            if int(item) < 2 :
                self.view.clb.SetItemBackgroundColour(index, 'Yellow')
                self.view.clb.SetItemForegroundColour(index, 'Blue')
        self.view.btn_exit.Bind(wx.EVT_BUTTON, self.onExit)
        self.view.clb.Bind(wx.EVT_CHECKLISTBOX, self.onCLB )

    def onCLB(self, evt):
        '''handler click checklistbox'''
        print "Clik...todo sth"

    def onExit(self, evt):
        '''handler button di uscita'''
        self.view.Close()

if __name__ == '__main__':
    app = wx.App(False)
    controller = CLBColorController()
    app.MainLoop()

On windows the wx.ListBox class can use the native "owner drawn" APIs in order to be able to allow wx.CheckListBox to draw its own checkboxes, etc. That means that I'm also able to add code like the following to the wrapper classes exposed in wxPython:

         void SetItemForegroundColour(int item, const wxColour& c)
         {
             %#ifdef __WXMSW__
                  if (self->GetWindowStyle() & wxLB_OWNERDRAW)
                      self->GetItem(item)->SetTextColour(c);
             %#endif
         }
         void SetItemBackgroundColour(int item, const wxColour& c)
         {
             %#ifdef __WXMSW__
                  if (self->GetWindowStyle() & wxLB_OWNERDRAW)
                      self->GetItem(item)->SetBackgroundColour(c);
             %#endif
         }
         void SetItemFont(int item, const wxFont& f)
         {
             %#ifdef __WXMSW__
                  if (self->GetWindowStyle() & wxLB_OWNERDRAW)
                      self->GetItem(item)->SetFont(f);
             %#endif
         }

The other platforms don't have the same owner drawn APIs and so they implement their wx.CheckListBox classes differently. Unfortunately that means that code like the above won't work and so I have to use conditional compilation to only use it on Windows.

An alternative approach you can take is to use a wx.ListCtrl in REPORT mode with a single column. You can also use a style to turn off the header to make it look more like a listbox, and you can use the CheckListCtrlMixin to give the items checkboxes.

···

On 3/20/11 5:53 AM, bancaldo wrote:

I am developing an application that uses a CheckListBox.
With the SetItemBackground() method, I apply a color to the item of
CheckListBox.
My code works perfectly under Windows, but I get no result, under
Linux.
What is the problem?

--
Robin Dunn
Software Craftsman

Ok Robin,
Now everything is clear.
I will take a look to the the alternative approach You've suggested.

Thank you very much.
bye

···

On 22 Mar, 00:01, Robin Dunn <ro...@alldunn.com> wrote:

On 3/20/11 5:53 AM, bancaldo wrote:

> I am developing an application that uses a CheckListBox.
> With the SetItemBackground() method, I apply a color to the item of
> CheckListBox.
> My code works perfectly under Windows, but I get no result, under
> Linux.
> What is the problem?

On windows the wx.ListBox class can use the native "owner drawn" APIs in
order to be able to allow wx.CheckListBox to draw its own checkboxes,
etc. That means that I'm also able to add code like the following to the
wrapper classes exposed in wxPython:

     void SetItemForegroundColour\(int item, const wxColour&amp; c\)
     \{
         %\#ifdef \_\_WXMSW\_\_
              if \(self\-&gt;GetWindowStyle\(\) &amp; wxLB\_OWNERDRAW\)
                  self\-&gt;GetItem\(item\)\-&gt;SetTextColour\(c\);
         %\#endif
     \}
     void SetItemBackgroundColour\(int item, const wxColour&amp; c\)
     \{
         %\#ifdef \_\_WXMSW\_\_
              if \(self\-&gt;GetWindowStyle\(\) &amp; wxLB\_OWNERDRAW\)
                  self\-&gt;GetItem\(item\)\-&gt;SetBackgroundColour\(c\);
         %\#endif
     \}
     void SetItemFont\(int item, const wxFont&amp; f\)
     \{
         %\#ifdef \_\_WXMSW\_\_
              if \(self\-&gt;GetWindowStyle\(\) &amp; wxLB\_OWNERDRAW\)
                  self\-&gt;GetItem\(item\)\-&gt;SetFont\(f\);
         %\#endif
     \}

The other platforms don't have the same owner drawn APIs and so they
implement their wx.CheckListBox classes differently. Unfortunately that
means that code like the above won't work and so I have to use
conditional compilation to only use it on Windows.

An alternative approach you can take is to use a wx.ListCtrl in REPORT
mode with a single column. You can also use a style to turn off the
header to make it look more like a listbox, and you can use the
CheckListCtrlMixin to give the items checkboxes.

--
Robin Dunn
Software Craftsmanhttp://wxPython.org