0
votes

I have a wx.ListCtrl interface, as found on:

http://wiki.wxpython.org/AnotherTutorial#wx.ListCtrl

but I cant get the Alternate Row Colours to work, as seen here:

http://wxpython.org/Phoenix/docs/html/ListCtrl.html#ListCtrl.EnableAlternateRowColours

Can anyone give me a simple example of how to use them, using this example from the website?

!/usr/bin/python

capitals.py

import wx

class MyDialog(wx.Dialog): def init(self, parent, id, title): wx.Dialog.init(self, parent, id, title, size=(600,500), style=wx.DEFAULT_DIALOG_STYLE)

    hbox  = wx.BoxSizer(wx.HORIZONTAL)
    vbox1 = wx.BoxSizer(wx.VERTICAL)
    vbox2 = wx.BoxSizer(wx.VERTICAL)
    vbox3 = wx.GridSizer(2,2,0,0)
    vbox4 = wx.BoxSizer(wx.VERTICAL)
    pnl1 = wx.Panel(self, -1, style=wx.SIMPLE_BORDER)
    pnl2 = wx.Panel(self, -1, style=wx.SIMPLE_BORDER)
    self.lc = wx.ListCtrl(self, -1, style=wx.LC_REPORT)
    self.lc.InsertColumn(0, 'State')
    self.lc.InsertColumn(1, 'Capital')
    self.lc.SetColumnWidth(0, 140)
    self.lc.SetColumnWidth(1, 153)
    vbox1.Add(pnl1, 1, wx.EXPAND | wx.ALL, 3)
    vbox1.Add(pnl2, 1, wx.EXPAND | wx.ALL, 3)
    vbox2.Add(self.lc, 1, wx.EXPAND | wx.ALL, 3)
    self.tc1 = wx.TextCtrl(pnl1, -1)
    self.tc2 = wx.TextCtrl(pnl1, -1)
    vbox3.AddMany([ (wx.StaticText(pnl1, -1, 'State'),0, wx.ALIGN_CENTER),
                    (self.tc1, 0, wx.ALIGN_LEFT|wx.ALIGN_CENTER_VERTICAL),
                    (wx.StaticText(pnl1, -1, 'Capital'),0, wx.ALIGN_CENTER_HORIZONTAL),
                    (self.tc2,0)])
    pnl1.SetSizer(vbox3)
    vbox4.Add(wx.Button(pnl2, 10, 'Add'),   0, wx.ALIGN_CENTER| wx.TOP, 45)
    vbox4.Add(wx.Button(pnl2, 11, 'Remove'), 0, wx.ALIGN_CENTER|wx.TOP, 15)
    vbox4.Add(wx.Button(pnl2, 12, 'Clear'), 0, wx.ALIGN_CENTER| wx.TOP, 15)
    vbox4.Add(wx.Button(pnl2, 13, 'Close'), 0, wx.ALIGN_CENTER| wx.TOP, 15)
    pnl2.SetSizer(vbox4)
    self.Bind (wx.EVT_BUTTON, self.OnAdd, id=10)
    self.Bind (wx.EVT_BUTTON, self.OnRemove, id=11)
    self.Bind (wx.EVT_BUTTON, self.OnClear, id=12)
    self.Bind (wx.EVT_BUTTON, self.OnClose, id=13)
    hbox.Add(vbox1, 1, wx.EXPAND)
    hbox.Add(vbox2, 1, wx.EXPAND)
    self.SetSizer(hbox)

def OnAdd(self, event):
    if not self.tc1.GetValue() or not self.tc2.GetValue():
        return
    num_items = self.lc.GetItemCount()
    self.lc.InsertStringItem(num_items, self.tc1.GetValue())
    self.lc.SetStringItem(num_items, 1, self.tc2.GetValue())
    self.tc1.Clear()
    self.tc2.Clear()

def OnRemove(self, event):
    index = self.lc.GetFocusedItem()
    self.lc.DeleteItem(index)

def OnClose(self, event):
    self.Close()

def OnClear(self, event):
    self.lc.DeleteAllItems()

class MyApp(wx.App): def OnInit(self): dia = MyDialog(None, -1, 'capitals.py') dia.ShowModal() dia.Destroy() return True

app = MyApp(0) app.MainLoop()

1
I didn't notice anywhere in your code you posted that you actually call the method EnableAlternateRowColours & are you using the correct version of wxpython for that method as it was new in version 2.9.5.Yoriz

1 Answers

0
votes

As Yoriz has pointed out, that is a pretty new method. If you'd like to do it the old way, I show how to do so on my blog: http://www.blog.pythonlibrary.org/2011/01/04/wxpython-wx-listctrl-tips-and-tricks/

Basically, I just use SetItemBackgroundColour() and alternate as I add rows. You could also use ObjectListView, which is a wrapper around ListCtrl that has alternating row colors built in to it. This article might help you if you decide to go that route: http://www.blog.pythonlibrary.org/2009/12/23/wxpython-using-objectlistview-instead-of-a-listctrl/