user812786
user812786

Reputation: 4430

CTRL-click behavior in wx.ListBox with multiple selections

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:

I'm running Linux and wxPython version 2.6.4.0.

Upvotes: 1

Views: 1680

Answers (2)

user812786
user812786

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

Mike Driscoll
Mike Driscoll

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

Related Questions