Search code examples
wxpython

wxPython Select Multiple items at a time Listctrl


I am trying to create a custom wxPython widget. Which would allow user to select multiple items out of Left side list and move them to right side list. I just stuck to selecting multiple items from a list. Here is the screengrab what I'm trying to achieve:

Multichoice Select on a panel

And here is my code(It's just start since not cleaned up):

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", size=(800,600))

        # 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")
        fiat = Car(2, "Fiat", "F7Z", "2005")
        fiat2 = Car(2, "Fiat", "punto", "2005")

        sampleList = []

        lb = wx.ListBox(panel,
                        size=(200, 150),
                        choices=sampleList)
        self.oneadd = wx.Button(panel,-1, ">", pos=(110, 180))
        self.multiadd = wx.Button(panel, -1,">>",pos=(200, 180))

        lb2 = wx.ListBox(panel,
                size=(200, 150),
                choices=sampleList)

        self.lb = lb
        self.lb2 = lb2
        lb2.Append(ford.make, ford)
        lb.Append(chevy.make, chevy)
        lb.Append(fiat.make, fiat)
        lb.Append(fiat2.make, fiat2)
        lb.Append(nissan.make, nissan)
        lb.Bind(wx.EVT_LISTBOX, self.onSelect)


        sizer = wx.BoxSizer(wx.HORIZONTAL)

        sizer.Add(lb, 0, wx.ALL, 5)

        sizer.Add(lb2, 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()

Solution

  • You need to enable multiple selection in the listbox

    lb = wx.ListBox(panel,
                    size=(200, 150),
                    style=wx.LB_MULTIPLE,
                    choices=sampleList)
    

    Lokla