wxPython combo.ComboCtrl事件处理问题

时间:2014-02-10 12:50:48

标签: wxpython

我有一个myComboCtrl(combo.ComboCtrl)对象,我想对我与之链接的ComboPopup对象中的值更改做出反应 - 点击后我想基于以下内容更新我的框架的一部分落在myComboCtrl中的值。所以,基于:

https://groups.google.com/forum/#!topic/wxpython-users/j6zQC_f12lM

我知道我需要将事件发送到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()

1 个答案:

答案 0 :(得分:1)

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

应该是:

myEvent = wx.CommandEvent(wx.EVT_COMBOBOX.typeId)

以下是“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.
        """

编辑:

应该可以使用,请尝试这个简单的演示。

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

myEvent = wx.CommandEvent(commandType=wx.EVT_COMBOBOX.typeId)
self.comboBox2.Bind(wx.EVT_COMBOBOX, self.OnCb2Click)

使用您的代码进行测试:)

相关问题