Question

I initialize a wx.ListBox like this :

mylistbox = wx.ListBox(self, style=wx.LB_SINGLE)
mylistbox.Bind(wx.EVT_LISTBOX, self.OnEventListBox)
# some other things (append some items to the list)
mylistbox.SetSelection(5)

I also have :

def OnEventListBox(self, event):
    print 'hello'
    # plus lots of other things

How to make that the command mylistbox.SetSelection(5) in the initialization is immediately followed by the call of OnEventListBox?

Remark : It seems that SetSelection() doesn't generate a wx.EVT_LISTBOX automatically.

Was it helpful?

Solution

From the documentation:

Note that [SetSelection] does not cause any command events to be emitted...

This is on purpose, so that the events don't all trigger while you're trying to set up the UI. You could just manually call OnEventListBox for the desired functionality.

Better yet, if you don't need the event for what you're doing on init, you could extract the initialisation into a separate function, then call that on init and in OnEventListBox.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top