I have a wx.ListBox that I would like the user to select from as if they were holding down the CTRL key. So – if you click on an item that is not selected, it will be selected (without modifying other selections), and if you click on a selected item, it will be deselected (again without modifying other selections).
The code to create the listbox is:
self.myListBox = wx.ListBox(self,
-1,
choices=self.keys,
style = wx.LB_HSCROLL
| wx.LB_MULTIPLE
| wx.LB_NEEDED_SB
| wx.LB_SORT)
self.Bind(wx.EVT_LISTBOX, self.OnSelection, self.myListBox)
Problems / questions:
-
Is there already an easy way to do this? Either
wx.LB_MULTIPLEorwx.LB_EXTENDEDlook like they might be what I want, but I’ve tried both and it doesn’t appear to change anything. The description in the documentation is a little vague as well – what exactly is the difference between the two? -
I know can get the id of the selected item with
event.GetSelection(), but if I try to implement the above logic, i.e.:if self.myListBox.IsSelected(idx): self.myListBox.Deselect(idx) else: self.myListBox.Select(idx)the event has already fired and so it just deselects whatever was just selected. Tried calling
event.Skip()before that as well, but no luck there either. Is there a way I can stop it from doing that? -
I noticed that when dragging the mouse for multiple selections
event.GetSelection()would get the correct id’s going up – i.e.,3, 2, 1, 0– but going down it would only show the first –0, 0, 0, 0. I’m guessing it’s just printing out the first item in the list of selections — is there a way to get the list from the event object (as opposed to callingself.myListBox.GetSelections())?
I’m running Linux and wxPython version 2.6.4.0.
(Late answer, but it might be of use to document.)
I ended up writing handlers for the onClick events: