Reputation: 4430
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_MULTIPLE
or wx.LB_EXTENDED
look 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 calling self.myListBox.GetSelections()
)?
I'm running Linux and wxPython version 2.6.4.0.
Upvotes: 1
Views: 1680
Reputation: 4430
(Late answer, but it might be of use to document.)
I ended up writing handlers for the onClick events:
def OnSelection(self, event):
"""Simulate CTRL-click"""
selection = self.myListBox.GetSelections()
for i in selection:
if i not in self.selectedItems:
# add to list of selected items
self.selectedItems.append(i)
self.myListBox.Select(i)
elif len(selection) == 1:
# remove from list of selected items
self.selectedItems.remove(i)
self.myListBox.Deselect(i)
for i in self.selectedItems:
# actually select all the items in the list
self.myListBox.Select(i)
Upvotes: 2
Reputation: 33071
wxPython 2.6.x is super old. Upgrade to the 2.8 or 2.9 series. I just ran the wxPython demo for 2.8.11 and it seemed to work fine for me on Windows.
Upvotes: 0