How to set the value of a wx.combobox by posting an event

Posted by Adam Fraser on Stack Overflow See other posts from Stack Overflow or by Adam Fraser
Published on 2010-04-21T17:59:29Z Indexed on 2010/04/21 18:03 UTC
Read the original article Hit count: 212

Filed under:
|
|

The code below demonstrates the problem I am running into. I am creating a wx.ComboBox and trying to mimic it's functionality for testing purposes by posting a wxEVT_COMMAND_COMBOBOX_SELECTED event... this event strangely works fine for wx.Choice, but it doesn't do anything to the ComboBox.

There doesn't appear to be a different event that I can post to the combobox, but maybe I'm missing something.

I'm running this code on Python 2.5 on a 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()

© Stack Overflow or respective owner

Related posts about wxpython

Related posts about python