Reputation: 47
I have a myComboCtrl (combo.ComboCtrl) object, that I want to react to a change of value in the ComboPopup object I have linked with it - upon click I'd like to update a portion of my frame based on the value that lands in myComboCtrl. So, based on:
https://groups.google.com/forum/#!topic/wxpython-users/j6zQC_f12lM
I understand I need to send the event to the ComboCtrl event handler. From what I read, sending events can be done in two ways, either the ProcessEvent method of the EventHandler or by using PostEvent.
So, in my ComboPopup (subclassed from ListCtrl ) I am trying to fire the event
def SetStringValue(self, val):
idx = self.FindItem(-1, val)
if idx != wx.NOT_FOUND:
self.Select(idx)
myComboCtrlEventHandler = self.GetCombo().GetEventHandler()
myEvent = wx.CommandEvent(0, wx.EVT_COMBOBOX.typeId)
myComboCtrlEventHandler.ProcessEvent(myEvent)
The ComboCtrl is already bound using:
self.myComboCtrl.Bind(wx.EVT_COMBOBOX, self.DoSomethin,None,wxID_MYCOMBOCTRLID)
and the DoSomethin function has just a wx.MessageBox inside. When I run the code, clicking the items on the popup correctly fires the SetStringValue, but nothing happens. What am I doing wrong ?
---------------------------------------------------------------- EDIT -----------------------------------------------------------
Here's a sample source file that shows my problem. I expect to get the ComboCtrl to act the same way as the generic ComboBox, alas I cannot get that event thing working
import wx
import wx.combo
[wxID_MYFRAME, wxID_MYFRAMECOMBOBOX1, wxID_MYFRAMECOMBOBOX2, wxID_MYFRAMEPANEL1,
wxID_MYFRAMESTATICTEXT1, wxID_MYFRAMESTATICTEXT2,
] = [wx.NewId() for _init_ctrls in range(6)]
######################################################################################
class ListCtrlComboPopup(wx.ListCtrl, wx.combo.ComboPopup):
def __init__(self):
self.PostCreate(wx.PreListCtrl())
wx.combo.ComboPopup.__init__(self)
def AddItem(self, txt):
self.InsertStringItem(self.GetItemCount(), txt)
def OnMotion(self, evt):
item, flags = self.HitTest(evt.GetPosition())
if item >= 0:
self.Select(item)
self.curitem = item
def OnLeftDown(self, evt):
self.value = self.curitem
self.Dismiss()
def Init(self):
self.value = -1
self.curitem = -1
def Create(self, parent):
wx.ListCtrl.Create(self, parent,
style=wx.LC_LIST|wx.LC_SINGLE_SEL|wx.SIMPLE_BORDER)
self.Bind(wx.EVT_MOTION, self.OnMotion)
self.Bind(wx.EVT_LEFT_DOWN, self.OnLeftDown)
return True
def GetControl(self):
return self
def SetStringValue(self, val):
idx = self.FindItem(-1, val)
if idx != wx.NOT_FOUND:
self.Select(idx)
myComboCtrlEventHandler = self.GetCombo().GetEventHandler()
myEvent = wx.CommandEvent(winid=0, commandType=wx.EVT_COMBOBOX.typeId)
myComboCtrlEventHandler.ProcessEvent(myEvent)
def GetStringValue(self):
if self.value >= 0:
return self.GetItemText(self.value)
return ""
def OnPopup(self):
wx.combo.ComboPopup.OnPopup(self)
def OnDismiss(self):
wx.combo.ComboPopup.OnDismiss(self)
######################################################################################
class myFrame(wx.Frame):
def __init__(self, objParent):
myChoices = ['one','two','three','four']
wx.Frame.__init__(self, id=wxID_MYFRAME, name='', parent=objParent,
pos=wx.Point(515, 255), size=wx.Size(258, 129),
style=wx.DEFAULT_FRAME_STYLE, title='MYFRAME')
self.panel1 = wx.Panel(id=wxID_MYFRAMEPANEL1, name='panel1', parent=self,
pos=wx.Point(0, 0), size=wx.Size(250, 102),
style=wx.TAB_TRAVERSAL)
self.comboBox1 = wx.ComboBox(choices=myChoices, id=wxID_MYFRAMECOMBOBOX1,
name='comboBox1', parent=self.panel1, pos=wx.Point(32, 24),
size=wx.Size(130, 21), style=0, value='comboBox1')
self.comboBox2 = wx.combo.ComboCtrl(id=wxID_MYFRAMECOMBOBOX2,
name='comboBox2', parent=self.panel1, pos=wx.Point(32, 56),
size=wx.Size(130, 21), style=0, value='comboBox2')
self.staticText1 = wx.StaticText(id=wxID_MYFRAMESTATICTEXT1,
label='staticText1', name='staticText1', parent=self.panel1,
pos=wx.Point(176, 24), size=wx.Size(54, 13), style=0)
self.staticText2 = wx.StaticText(id=wxID_MYFRAMESTATICTEXT2,
label='staticText2', name='staticText2', parent=self.panel1,
pos=wx.Point(176, 56), size=wx.Size(54, 13), style=0)
myComboPopup = ListCtrlComboPopup()
self.comboBox2.SetPopupControl(myComboPopup)
for i in range(len(myChoices)):
print type(myChoices[i])
myComboPopup.AddItem(myChoices[i])
self.comboBox1.Bind(wx.EVT_COMBOBOX, self.OnCb1Click,None,wxID_MYFRAMECOMBOBOX1)
self.comboBox2.Bind(wx.EVT_COMBOBOX, self.OnCb2Click,None,wxID_MYFRAMECOMBOBOX2)
def OnCb1Click(self, event):
del self.staticText1
self.staticText1 = wx.StaticText(id=wxID_MYFRAMESTATICTEXT1,
label=self.comboBox1.GetValue(), parent=self.panel1,
pos=wx.Point(176, 24), size=wx.Size(54, 13), style=0)
event.Skip()
def OnCb2Click(self, event):
del self.staticText2
self.staticText2 = wx.StaticText(id=wxID_MYFRAMESTATICTEXT1,
label=self.comboBox2.GetValue(), parent=self.panel1,
pos=wx.Point(176, 24), size=wx.Size(54, 13), style=0)
event.Skip()
######################################################################################
def main():
app = wx.PySimpleApp()
myFrameObj = myFrame(None)
myFrameObj.Show()
app.MainLoop()
if __name__ == '__main__':
main()
Upvotes: 1
Views: 880
Reputation: 1734
You pass in the incorrect parameter while create the event.
It should be:
myEvent = wx.CommandEvent(wx.EVT_COMBOBOX.typeId)
Here is the source code of the class "CommandEvent":
class CommandEvent(Event):
"""
This event class contains information about command events, which
originate from a variety of simple controls, as well as menus and
toolbars.
"""
thisown = property(lambda x: x.this.own(), lambda x, v: x.this.own(v), doc='The membership flag')
__repr__ = _swig_repr
def __init__(self, *args, **kwargs):
"""
__init__(self, EventType commandType=wxEVT_NULL, int winid=0) -> CommandEvent
This event class contains information about command events, which
originate from a variety of simple controls, as well as menus and
toolbars.
"""
Edit:
It should work, please try this simple demo.
import wx
def cbEvent(evt):
print "cbEvent"
def btnEvent(evt):
print "btnEvent"
myComboCtrlEventHandler = cb.GetEventHandler()
myEvent = wx.CommandEvent(wx.EVT_COMBOBOX.typeId)
myComboCtrlEventHandler.ProcessEvent(myEvent)
if __name__=='__main__':
app = wx.App(redirect=False)
frame = wx.Frame(None)
p = wx.Panel(frame)
cb = wx.ComboBox(p, 500, "default value", (90, 50), (160, -1), "test")
cb.Bind(wx.EVT_COMBOBOX, cbEvent)
bt = wx.Button(p, -1, "btn", (90,100))
bt.Bind(wx.EVT_BUTTON, btnEvent)
frame.Show()
app.MainLoop()
Edit 2:
When you create the event, you should assign correct ID. And When you bind the event, you should assign the correct ID as well.
myEvent = wx.CommandEvent(winid=self.GetId(), commandType=wx.EVT_COMBOBOX.typeId)
self.comboBox2.Bind(wx.EVT_COMBOBOX, self.OnCb2Click,None,myComboPopup.GetId())
Or you can leave them alone(with default wx.ID_ANY):
myEvent = wx.CommandEvent(commandType=wx.EVT_COMBOBOX.typeId)
self.comboBox2.Bind(wx.EVT_COMBOBOX, self.OnCb2Click)
Both tested with your code :)
Upvotes: 1