wxPython combo.ComboCtrl事件处理问题

wxPython combo.ComboCtrl事件处理问题,wxpython,Wxpython,我有一个myComboCtrl(combo.ComboCtrl)对象,我想对与之链接的ComboPopup对象中的值的更改做出反应-单击后,我想根据myComboCtrl中的值更新框架的一部分。因此,基于: 我知道我需要将事件发送到ComboCtrl事件处理程序。据我所知,发送事件可以通过两种方式完成,一种是EventHandler的ProcessEvent方法,另一种是使用PostEvent 因此,在我的ComboPopup(从ListCtrl派生)中,我试图触发事件 def SetStri

我有一个myComboCtrl(combo.ComboCtrl)对象,我想对与之链接的ComboPopup对象中的值的更改做出反应-单击后,我想根据myComboCtrl中的值更新框架的一部分。因此,基于:

我知道我需要将事件发送到ComboCtrl事件处理程序。据我所知,发送事件可以通过两种方式完成,一种是EventHandler的ProcessEvent方法,另一种是使用PostEvent

因此,在我的ComboPopup(从ListCtrl派生)中,我试图触发事件

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)
ComboCtrl已通过以下方式绑定:

    self.myComboCtrl.Bind(wx.EVT_COMBOBOX, self.DoSomethin,None,wxID_MYCOMBOCTRLID)
DoSomethin函数内部只有一个wx.MessageBox。当我运行代码时,单击弹出窗口上的项目可以正确地触发SetStringValue,但什么也没有发生。我做错了什么

----------------------------------------------------------------编辑-----------------------------------------------------------

下面是一个示例源文件,它显示了我的问题。我希望ComboCtrl的操作方式与通用ComboBox的操作方式相同,但遗憾的是,我无法使该事件正常工作

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()

创建事件时传入了错误的参数

应该是:

myEvent = wx.CommandEvent(wx.EVT_COMBOBOX.typeId)
下面是“CommandEvent”类的源代码:

编辑:

它应该可以工作,请尝试这个简单的演示

    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()
编辑2:

创建事件时,应分配正确的ID。 当绑定事件时,还应该分配正确的ID

myEvent = wx.CommandEvent(winid=self.GetId(), commandType=wx.EVT_COMBOBOX.typeId)
self.comboBox2.Bind(wx.EVT_COMBOBOX, self.OnCb2Click,None,myComboPopup.GetId())
或者,您可以不使用它们(默认为wx.ID_ANY):


两个都用代码进行了测试:)

好的,我已经更改了那一行,但是仍然没有发生任何事情。尝试像这样绑定事件:self.myComboCtrl.bind(wx.EVT_COMBOBOX,self.DoSomethin)是的,您发布的编辑可能会起作用,问题是您将事件附加到wx控件,而我在wx.combo控件上遇到问题。我的事件可以很好地用于按钮、listCtrls等,只是combo.ComboCtrl似乎在做idk。我已经发布了一个包含该活动的示例代码,请检查该代码:)
myEvent = wx.CommandEvent(winid=self.GetId(), commandType=wx.EVT_COMBOBOX.typeId)
self.comboBox2.Bind(wx.EVT_COMBOBOX, self.OnCb2Click,None,myComboPopup.GetId())
myEvent = wx.CommandEvent(commandType=wx.EVT_COMBOBOX.typeId)
self.comboBox2.Bind(wx.EVT_COMBOBOX, self.OnCb2Click)