0

我有一个 wx.ListCtrl 接口,如下所示:

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

但我无法让备用行颜色工作,如下所示:

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

谁能给我一个简单的例子来说明如何使用它们,使用网站上的这个例子?

!/usr/bin/python

大写字母.py

导入 wx

类 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()

4

1 回答 1

0

正如 Yoriz 所指出的,这是一种相当新的方法。如果您想以旧方式进行操作,我会在我的博客上展示如何操作:http: //www.blog.pythonlibrary.org/2011/01/04/wxpython-wx-listctrl-tips-and-技巧/

基本上,我只使用 SetItemBackgroundColour() 并在添加行时交替使用。您还可以使用 ObjectListView,它是 ListCtrl 的一个包装器,其中内置了交替的行颜色。如果您决定走这条路,这篇文章可能会对您有所帮助:http: //www.blog.pythonlibrary.org/2009/12/23/wxpython-using-objectlistview-instead-of-a-listctrl/

于 2013-07-12T14:21:52.733 回答