How to set wx.combobox value by posting event
NOTE. This appears to be an OSX issue.
The code below shows the problem I am facing. I am creating a wx.ComboBox and trying to simulate its functionality for testing purposes by dispatching a wxEVT_COMMAND_COMBOBOX_SELECTED event ... this event works oddly fine for wx.Choice, but it does nothing for ComboBox.
There seems to be no other event that I can post to the combobox, but maybe I missed something.
I am running this code in Python 2.5 on Mac OSX 10.5.8
import wx
app = wx.PySimpleApp()
def on_btn(evt):
event = wx.CommandEvent(wx.wxEVT_COMMAND_COMBOBOX_SELECTED,combobox.Id)
event.SetEventObject(combobox)
event.SetInt(1)
event.SetString('bar')
combobox.Command(event)
app.ProcessPendingEvents()
frame = wx.Frame(None)
panel = wx.Panel(frame, -1)
# This doesn't work
combobox = wx.ComboBox(panel, -1, choices=['foo','bar'])
# This works
#combobox = wx.Choice(panel, -1, choices=['foo','bar'])
combobox.SetSelection(0)
btn = wx.Button(panel, -1, 'asdf')
btn.Bind(wx.EVT_BUTTON, on_btn)
sz = wx.BoxSizer()
sz.Add(combobox)
sz.Add(btn)
panel.SetSizer(sz)
frame.Show()
app.MainLoop()
UPDATE . I hooked up the combobox to the wx.EVT_COMBOBOX handler to find out what type of event is caught there and I got the id 10016 which matches wxEVT_COMMAND_COMBOBOX_SELECTED ... so generating this command event MUST trigger an update of the ComboBox.
a source to share
I think this fixes, or at least points the way to a more complete fix.
Edit: you can use PyCommandEvent if you like, and also use SetInt and SetString to put additional information on the event if needed. But as far as I can tell, the combo box selection needs to be set as well.
import wx
app = wx.PySimpleApp()
def on_btn(evt):
combobox.Selection=1
wx.PostEvent(combobox, wx.CommandEvent(wx.wxEVT_COMMAND_COMBOBOX_SELECTED))
print "foo"
def on_select(evt):
print "selected", combobox.Selection
frame = wx.Frame(None)
panel = wx.Panel(frame, -1)
# This doesn't work
combobox = wx.ComboBox(panel, -1, choices=['foo','bar'])
# This works
# combobox = wx.Choice(panel, -1, choices=['foo','bar'])
combobox.SetSelection(0)
btn = wx.Button(panel, -1, 'asdf')
btn.Bind(wx.EVT_BUTTON, on_btn)
combobox.Bind(wx.EVT_COMBOBOX, on_select)
sz = wx.BoxSizer()
sz.Add(combobox)
sz.Add(btn)
panel.SetSizer(sz)
frame.Show()
app.MainLoop()