gpt4 book ai didi

python - wx.lib.newevent.NewEvent() 和 wx.NewEventType() 有什么区别?

转载 作者:太空宇宙 更新时间:2023-11-04 01:13:19 30 4
gpt4 key购买 nike

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

最佳答案

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)

关于python - wx.lib.newevent.NewEvent() 和 wx.NewEventType() 有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26312061/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com