开发者

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

开发者 https://www.devze.com 2022-12-27 12:47 出处:网络
NOTE: This appears to be an OSX specific problem. 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

NOTE: This appears to be an OSX specific problem.

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.P开发者_StackOverflowrocessPendingEvents()


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 a handler of wx.EVT_COMBOBOX to see what event type is being caught there and I got the id 10016 which matches wxEVT_COMMAND_COMBOBOX_SELECTED... so generating this command event certainly SHOULD cause the ComboBox to update.


This seems to be a specific bug from OSX. Both alternatives work perfect in windowsXP.


I think this fixes it, or at least points the way towards a more complete fix.

Edit: you can use PyCommandEvent if you prefer, as well as using SetInt and SetString to put more information in the Event if needed. But it is necessary, as far as I can tell, to set the selection of the combobox 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()
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号