不久前我在我的博客上写过这个。你应该检查一下。基本上,您只需将列表中的每个项目附加到列表框,并添加一些额外的数据作为第二个参数。在我的示例中,我添加了一个对象实例。这是我的博客文章中的代码:
import wx
########################################################################
class Car:
""""""
#----------------------------------------------------------------------
def __init__(self, id, model, make, year):
"""Constructor"""
self.id = id
self.model = model
self.make = make
self.year = year
########################################################################
class MyForm(wx.Frame):
#----------------------------------------------------------------------
def __init__(self):
wx.Frame.__init__(self, None, wx.ID_ANY, "Tutorial")
# Add a panel so it looks the correct on all platforms
panel = wx.Panel(self, wx.ID_ANY)
ford = Car(0, "Ford", "F-150", "2008")
chevy = Car(1, "Chevrolet", "Camaro", "2010")
nissan = Car(2, "Nissan", "370Z", "2005")
sampleList = []
lb = wx.ListBox(panel,
size=wx.DefaultSize,
choices=sampleList)
self.lb = lb
lb.Append(ford.make, ford)
lb.Append(chevy.make, chevy)
lb.Append(nissan.make, nissan)
lb.Bind(wx.EVT_LISTBOX, self.onSelect)
sizer = wx.BoxSizer(wx.VERTICAL)
sizer.Add(lb, 0, wx.ALL, 5)
panel.SetSizer(sizer)
#----------------------------------------------------------------------
def onSelect(self, event):
""""""
print "You selected: " + self.lb.GetStringSelection()
obj = self.lb.GetClientData(self.lb.GetSelection())
text = """
The object's attributes are:
%s %s %s %s
""" % (obj.id, obj.make, obj.model, obj.year)
print text
# Run the program
if __name__ == "__main__":
app = wx.App(False)
frame = MyForm()
frame.Show()
app.MainLoop()