Question

How can I add extra data to wxListBox items. I'm creating a photo viewing application and the user double clicks on a file path of an image to open the image.When the user doubl clicks an item in the listbox , a function does listbox1.GetStringSelection() to use the cuurent selected file when opening a StaticBitmap image. But this displays the whole file path which looks ugly, so how can a change it to only show the filename ? how can I add extra data for each listbox item?

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

Was it helpful?

Solution

I wrote about this on my blog a while back. You should check that out. Basically you just append each item in the list to the listbox and also add some extra data as the second parameter. In my example, I add an object instance. Here's the code from my blog post:

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()
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top