以下是两个例外中的一个。
在wx.PostEvent(self, MyEvent())
方法中调用MyPanel.OnButton
会抛出:
TypeError: __init__() takes exactly 2 arguments (1 given)
如果我将代码称为粘贴,没有任何参数。TypeError: in method 'new_PyCommandEvent', expected argument 2 of type 'int'
如果我将任何非整数值传递给MyEvent()构造函数。我发现一个破旧的论坛帖子建议我需要传入任何可能启动事件的对象的对象ID。我可以通过以下方式做到:
wx.PostEvent(self, MyEvent(e.EventObject.GetId()))
- 这很有效。但是,
-
import wx
import wx.lib.newevent
# Custom Events
MyEvent, EVT_MYEVENT = wx.lib.newevent.NewCommandEvent()
class MyPanel(wx.Panel):
def __init__(self, parent):
super(MyPanel, self).__init__(parent)
sizer = wx.BoxSizer()
self.button1 = wx.Button(self, label="Button 1")
sizer.Add(self.button1)
self.Bind(wx.EVT_BUTTON, self.OnButton)
def OnButton(self, e):
e.Skip()
##
# only works if I throw in an Int in the constructor but most
# of the tutorials don't do this.
wx.PostEvent(self, MyEvent())
class MyFrame(wx.Frame):
def __init__(self, parent, title=""):
super(MyFrame, self).__init__(parent, title=title)
self.panel = MyPanel(self)
class MyApp(wx.App):
def OnInit(self):
self.frame = MyFrame(None, title="Event Propagation")
self.frame.Show()
self.Bind(EVT_MYEVENT, self.OnMyEvt)
return True
def OnMyEvt(self, e):
print("MY EVENT HAS HAPPENED!")
e.Skip()
if __name__ == "__main__":
app = MyApp()
app.MainLoop()
答案 0 :(得分:0)
我怀疑你不需要Command Event
并且正常事件会发生
变化:
MyEvent, EVT_MYEVENT = wx.lib.newevent.NewCommandEvent()
为:
MyEvent, EVT_MYEVENT = wx.lib.newevent.NewEvent()
它应该按照您的意愿工作。
如果确实需要command event
,则需要提供id
快速嘲笑你的代码(它可能需要进一步的工作):
import wx
import wx.lib.newevent
# Custom Events
MyEvent, EVT_MYEVENT = wx.lib.newevent.NewCommandEvent()
CMD_ID = wx.NewId()
class MyPanel(wx.Panel):
def __init__(self, parent):
super(MyPanel, self).__init__(parent)
sizer = wx.BoxSizer()
self.button1 = wx.Button(self, CMD_ID, label="Button 1")
sizer.Add(self.button1)
self.Bind(wx.EVT_BUTTON, self.OnButton,id=CMD_ID)
def OnButton(self, e):
e.Skip()
##
# only works if I throw in an Int in the constructor but most
# of the tutorials don't do this.
id = e.GetId()
wx.PostEvent(self, MyEvent(id,myid=id))
class MyFrame(wx.Frame):
def __init__(self, parent, title=""):
super(MyFrame, self).__init__(parent, title=title)
self.panel = MyPanel(self)
class MyApp(wx.App):
def OnInit(self):
self.frame = MyFrame(None, title="Event Propagation")
self.frame.Show()
self.Bind(EVT_MYEVENT, self.OnMyEvt)
return True
def OnMyEvt(self, e):
print("MY EVENT HAS HAPPENED!")
e.Skip()
if __name__ == "__main__":
app = MyApp()
app.MainLoop()