Python wx.lib.newevent.newevent()和wx.NewEventType()之间有什么区别?

Python wx.lib.newevent.newevent()和wx.NewEventType()之间有什么区别?,python,wxpython,Python,Wxpython,使用wx.lib.newevent.newevent()和将wx.NewEventType()与wx.PyCommandEvent()一起使用有什么区别?下面的代码示例使用两种不同的方法创建事件以完成相同的任务(传递附加到事件的数据)。什么时候应该使用一个而不是另一个 import wx import wx.lib.newevent MyEvent, EVT_MY_EVENT = wx.lib.newevent.NewEvent() EVT_ANOTHER_EVENT_TYPE = wx.N

使用wx.lib.newevent.newevent()和将wx.NewEventType()与wx.PyCommandEvent()一起使用有什么区别?下面的代码示例使用两种不同的方法创建事件以完成相同的任务(传递附加到事件的数据)。什么时候应该使用一个而不是另一个

import wx
import wx.lib.newevent

MyEvent, EVT_MY_EVENT = wx.lib.newevent.NewEvent()

EVT_ANOTHER_EVENT_TYPE = wx.NewEventType()
EVT_ANOTHER_EVENT = wx.PyEventBinder(EVT_ANOTHER_EVENT_TYPE, 1)

class EventTest(wx.Dialog):
    def __init__(self, parent, id, title):
        wx.Dialog.__init__(self, parent, id, title, size=(210, 200))

        wx.Button(self, 1, 'NewEvent', (50, 10), (110, -1))
        wx.Button(self, 2, 'PyCommandEvent', (50, 60), (110, -1))
        wx.Button(self, 3, 'Close', (50, 110), (110, -1))

        self.Bind(wx.EVT_BUTTON, self.OnNewEvent, id=1)
        self.Bind(wx.EVT_BUTTON, self.OnPyCommandEvent, id=2)
        self.Bind(wx.EVT_BUTTON, self.OnClose, id=3)

        self.Bind(EVT_MY_EVENT, self.NewEventHandler)
        self.Bind(EVT_ANOTHER_EVENT, self.PyCommandEventHandler)

        self.Centre()
        self.ShowModal()
        self.Destroy()

    def OnNewEvent(self, event):
        evt = MyEvent(SomeData=("NewEvent", 11, "aaa"))
        wx.PostEvent(self, evt)

    def OnPyCommandEvent(self, event):
        evt = wx.PyCommandEvent(EVT_ANOTHER_EVENT_TYPE, wx.ID_ANY)
        evt.SetClientData(("PyCommandEvent", 22, "bbb"))
        wx.PostEvent(self, evt)

    def NewEventHandler(self, evt=None):
        print "NewEvent Data:", evt.SomeData

    def PyCommandEventHandler(self, evt=None):
        print "PyCommandEvent Data:", evt.GetClientData()

    def OnClose(self, event):
        self.Close(True)

if __name__ == "__main__":
    app = wx.App(0)
    EventTest(None, -1, 'Event Test')
    app.MainLoop()
newevent()只是一种更简单的wxpython方法,它被添加到wx.lib.NewEventType()中

如果您查看模块newevent中的代码,您将看到它的作用

"""Easy generation of new events classes and binder objects"""

__author__ = "Miki Tebeka <miki.tebeka@gmail.com>"

import wx

#---------------------------------------------------------------------------

def NewEvent():
    """Generate new (Event, Binder) tuple
        e.g. MooEvent, EVT_MOO = NewEvent()
    """
    evttype = wx.NewEventType()

    class _Event(wx.PyEvent):
        def __init__(self, **kw):
            wx.PyEvent.__init__(self)
            self.SetEventType(evttype)
            self.__dict__.update(kw)

    return _Event, wx.PyEventBinder(evttype)



def NewCommandEvent():
    """Generate new (CmdEvent, Binder) tuple
        e.g. MooCmdEvent, EVT_MOO = NewCommandEvent()
    """
    evttype = wx.NewEventType()

    class _Event(wx.PyCommandEvent):
        def __init__(self, id, **kw):
            wx.PyCommandEvent.__init__(self, evttype, id)
            self.__dict__.update(kw)

    return _Event, wx.PyEventBinder(evttype, 1)
“轻松生成新事件类和绑定器对象”
__作者=“Miki Tebeka”
导入wx
#---------------------------------------------------------------------------
def NewEvent():
“”“生成新(事件、绑定器)元组
e、 g.MooEvent,EVT_MOO=NewEvent()
"""
evttype=wx.NewEventType()
类事件(wx.PyEvent):
def初始功率(自身功率,**kw):
wx.PyEvent.\uuuuu init\uuuuuuuuuux(self)
self.SetEventType(evttype)
自我记录更新(千瓦)
return\u事件,wx.PyEventBinder(evttype)
def NewCommandEvent():
“”“生成新(CmdEvent,Binder)元组
e、 g.MooCmdEvent,EVT_MOO=NewCommandEvent()
"""
evttype=wx.NewEventType()
类_事件(wx.PyCommandEvent):
def _初始功率(自身,id,**kw):
PyCommandEvent.\uuuu init\uuuuux(self,evttype,id)
自我记录更新(千瓦)
return\u事件,wx.PyEventBinder(evttype,1)