如何向wxListBox项添加额外数据。我正在创建一个照片查看应用程序,用户双击图像的文件路径以打开图像。当用户双击列表框中的项目时,一个函数执行listbox1.GetStringSelection()以使用cuurent选择的文件打开StaticBitmap图像时。但这显示整个文件路径看起来很难看,那么如何才能将其更改为仅显示文件名?如何为每个列表框项添加额外数据?
les \ Windows Sidebar \ Gadgets \ Weather.Gadget \ images \ 120DPI(120DPI)alertIcon.png
C:\ Program Files \ Windows Sidebar \ Gadgets \ Weather.Gadget \ images \ 120DPI(120DPI)grayStateIcon.png
C:\ Program Files \ Windows Sidebar \ Gadgets \ Weather.Gadget \ images \ 120DPI(120DPI)greenStateIcon.png
C:\ Program Files \ Windows Sidebar \ Gadgets \ Weather.Gadget \ images \ 120DPI(120DPI)notConnectedStateIcon.png
C:\ Program Files \ Windows Sidebar \ Gadgets \ Weather.Gadget \ images \ 144DPI(144DPI)alertIcon.png
答案 0 :(得分:3)
我前一天在blog写了这篇文章。你应该检查一下。基本上,您只需将列表中的每个项目附加到列表框中,并添加一些额外的数据作为第二个参数。在我的示例中,我添加了一个对象实例。这是我博客文章中的代码:
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()