2012-07-05 58 views
3

是否有可能將wx.ListCtrl列中的所有值(項目)作爲列表獲取?ListCtrl - wxPython獲取列中的所有值

我可以在文檔中看到您可以獲得指定的項目,但不是整列,GetValue()也不工作,任何幫助?

回答

5

下面是做這件事:

import wx 

######################################################################## 
class MyForm(wx.Frame): 

    #---------------------------------------------------------------------- 
    def __init__(self): 
     wx.Frame.__init__(self, None, wx.ID_ANY, "List Control Tutorial") 

     # Add a panel so it looks the correct on all platforms 
     panel = wx.Panel(self, wx.ID_ANY) 
     self.index = 0 

     self.list_ctrl = wx.ListCtrl(panel, size=(-1,100), 
         style=wx.LC_REPORT 
         |wx.BORDER_SUNKEN 
         ) 
     self.list_ctrl.InsertColumn(0, 'Subject') 
     self.list_ctrl.InsertColumn(1, 'Due') 
     self.list_ctrl.InsertColumn(2, 'Location', width=125) 

     btn = wx.Button(panel, label="Add Line") 
     btn2 = wx.Button(panel, label="Get Column 0") 
     btn.Bind(wx.EVT_BUTTON, self.add_line) 
     btn2.Bind(wx.EVT_BUTTON, self.getColumn) 

     sizer = wx.BoxSizer(wx.VERTICAL) 
     sizer.Add(self.list_ctrl, 0, wx.ALL|wx.EXPAND, 5) 
     sizer.Add(btn, 0, wx.ALL|wx.CENTER, 5) 
     sizer.Add(btn2, 0, wx.ALL|wx.CENTER, 5) 
     panel.SetSizer(sizer) 

    #---------------------------------------------------------------------- 
    def add_line(self, event): 
     line = "Line %s" % self.index 
     self.list_ctrl.InsertStringItem(self.index, line) 
     self.list_ctrl.SetStringItem(self.index, 1, "01/19/2010") 
     self.list_ctrl.SetStringItem(self.index, 2, "USA") 
     self.index += 1 

    #---------------------------------------------------------------------- 
    def getColumn(self, event): 
     """""" 
     count = self.list_ctrl.GetItemCount() 
     for row in range(count): 
      item = self.list_ctrl.GetItem(itemId=row, col=0) 
      print item.GetText() 

#---------------------------------------------------------------------- 
# Run the program 
if __name__ == "__main__": 
    app = wx.App(False) 
    frame = MyForm() 
    frame.Show() 
    app.MainLoop() 

就個人而言,我喜歡ObjectListView比的ListCtrl更好。

+0

什麼版本的wx.Python有ObjectListView? –

+1

這不是當前wx的一部分。你可以在PyPI上找到它:http://pypi.python.org/pypi/ObjectListView –

+0

太棒了!多謝,夥計!! – Hairo