有幾種方法可以做到這一點。最簡單的(在我看來)是對象,每行聯繫在一起,但我們會做的「硬」的方式第一:
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 Data")
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()
cols = self.list_ctrl.GetColumnCount()
for row in range(count):
for col in range(cols):
item = self.list_ctrl.GetItem(itemId=row, col=col)
print item.GetText()
#----------------------------------------------------------------------
# Run the program
if __name__ == "__main__":
app = wx.App(False)
frame = MyForm()
frame.Show()
app.MainLoop()
這是稍微從早期answer到類似的問題進行修改。無論如何,讓我們來看看如何使用對象,而不是:
import wx
########################################################################
class Car(object):
""""""
#----------------------------------------------------------------------
def __init__(self, make, model, year, color="Blue"):
"""Constructor"""
self.make = make
self.model = model
self.year = year
self.color = color
########################################################################
class MyPanel(wx.Panel):
""""""
#----------------------------------------------------------------------
def __init__(self, parent):
"""Constructor"""
wx.Panel.__init__(self, parent)
rows = [Car("Ford", "Taurus", "1996"),
Car("Nissan", "370Z", "2010"),
Car("Porche", "911", "2009", "Red")
]
self.list_ctrl = wx.ListCtrl(self, size=(-1,100),
style=wx.LC_REPORT
|wx.BORDER_SUNKEN
)
self.list_ctrl.Bind(wx.EVT_LIST_ITEM_SELECTED, self.onItemSelected)
self.list_ctrl.InsertColumn(0, "Make")
self.list_ctrl.InsertColumn(1, "Model")
self.list_ctrl.InsertColumn(2, "Year")
self.list_ctrl.InsertColumn(3, "Color")
index = 0
self.myRowDict = {}
for row in rows:
self.list_ctrl.InsertStringItem(index, row.make)
self.list_ctrl.SetStringItem(index, 1, row.model)
self.list_ctrl.SetStringItem(index, 2, row.year)
self.list_ctrl.SetStringItem(index, 3, row.color)
self.myRowDict[index] = row
index += 1
sizer = wx.BoxSizer(wx.VERTICAL)
sizer.Add(self.list_ctrl, 0, wx.ALL|wx.EXPAND, 5)
self.SetSizer(sizer)
#----------------------------------------------------------------------
def onItemSelected(self, event):
""""""
currentItem = event.m_itemIndex
car = self.myRowDict[currentItem]
print car.make
print car.model
print car.color
print car.year
########################################################################
class MyFrame(wx.Frame):
""""""
#----------------------------------------------------------------------
def __init__(self):
"""Constructor"""
wx.Frame.__init__(self, None, wx.ID_ANY, "List Control Tutorial")
panel = MyPanel(self)
self.Show()
#----------------------------------------------------------------------
if __name__ == "__main__":
app = wx.App(False)
frame = MyFrame()
app.MainLoop()
這裏我們創建汽車對象的列表,我們使用點標記要添加的類別到的ListCtrl的性能。然後,當我們從列表中選擇一個項目時,我們從事件對象中獲取當前選定的項目並使用字典查找它。不完全簡單,但我更喜歡它。您可以閱讀更多關於它以及其他提示和技巧here。
但是,我認爲最好的解決方案是使用ObjectListView(一個ListCtrl包裝器),它使行成爲真實的對象,並允許更容易地訪問它們的值,並引入一系列其他增強功能。可悲的是,它還不是正常的wxPython發行版的一部分,但從PyPI很容易添加。您也可以在博客上閱讀article的更多內容!
您應該可能依賴於[GetItemData](http://docs.wxwidgets.org/stable/wx_wxlistctrl.html#wxlistctrlgetitemdata)來存儲與某一行相關的數據。 – jadkik94 2013-05-03 09:02:42
我試過了。但沒有解決。可以告訴我要在GetItemData中傳遞的參數嗎?只是爲了確認我的代碼! – Aramanethota 2013-05-03 12:03:33
如果我沒有記錯的話,你將'GetItemData'傳遞給一個索引(0到行數)並返回一個數字。所以你必須創建一個你想要的數據列表,然後創建一個'wx.ListItem'使用'SetText','SetData'爲一個數字並使用該數字來存儲你的列表中的數據。希望能幫助到你。我會做出答案,但我不確定細節。 – jadkik94 2013-05-03 12:47:48