0

如何向 wxListBox 项添加额外数据。我正在创建一个照片查看应用程序,用户双击图像的文件路径以打开图像。当用户双击列表框中的项目时,函数执行 listbox1.GetStringSelection() 以使用当前选定的文件打开 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

4

1 回答 1

3

不久前我在我的博客上写过这个。你应该检查一下。基本上,您只需将列表中的每个项目附加到列表框,并添加一些额外的数据作为第二个参数。在我的示例中,我添加了一个对象实例。这是我的博客文章中的代码:

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()
于 2012-06-22T13:37:55.790 回答