Search code examples
eventslistboxwxpythonmultiple-select

How do I access specific parameters of a wx.EVT_LISTBOX event?


I am trying to design a GUI where one of the components is a wx.ListBox with multiple selection capability (style = wx.LB_MULTIPLE.) I also have another panel where I want to set the text to match the long description of the last item selected on the ListBox.

I am aware that I can bind the ListBox to a function in this manner: listbox_obj.Bind(wx.EVT_LISTBOX, self.set_description)

However, when I define the method...

def set_description(self, event):

...how do I extrapolate from the event parameter which item in the ListBox was the LAST one selected, and whether the item was selected or de-selected?


Solution

  • I think that you are going to have to track the data yourself.
    For example:

    import wx
    
    class TestListBox(wx.Frame):
        def __init__(self, *args, **kwds):
            wx.Frame.__init__(self, *args, **kwds)
            self.OldSelections= []
            self.NewSelections = []
            self.aList = ['zero', 'one', 'two', 'three', 'four', 'five',
                          'six', 'seven', 'eight', 'nine', 'ten', 'eleven',
                          'twelve', 'thirteen', 'fourteen']
            self.lb = wx.ListBox(self, wx.NewId(), choices=self.aList, style=wx.LB_EXTENDED)
            self.lb.Bind(wx.EVT_LISTBOX, self.EvtMultiListBox)
            self.lb.SetSelection(0)
            self.Show()
    
        def EvtMultiListBox(self, event):
            self.NewSelections = self.lb.GetSelections()
            print('EvtMultiListBox: %s\n' % str(self.lb.GetSelections()))
            for i in self.NewSelections:
                if i not in self.OldSelections:
                    print (self.aList[i],"was added")
            for i in self.OldSelections:
                if i not in self.NewSelections:
                    print (self.aList[i],"was removed")
            self.OldSelections = self.NewSelections
            print("\n")
    
    if __name__ == '__main__':
        app = wx.App()
        TestListBox(None)
        app.MainLoop()