EnableAlternateRowColours示例会有很大帮助

时间:2013-07-12 11:10:07

标签: wxpython listctrl

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

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

但我无法使用Alternate Row Colors,如下所示:

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

任何人都可以使用网站上的这个例子给我一个如何使用它们的简单示例吗?

!在/ usr / bin中/ Python的

capitals.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()

类MyApp(wx.App):     def OnInit(个体经营):         dia = MyDialog(无,-1,'capitals.py')         dia.ShowModal()         dia.Destroy()         返回True

app = MyApp(0) app.MainLoop()

1 个答案:

答案 0 :(得分:0)

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

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