使用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()
答案 0 :(得分:2)
wx.lib.newevent.NewEvent()只是一个更简单的wxpython方式,它被添加到wx.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)