在wxPython中将事件传递给类外的函数
我需要做些什么才能处理一个事件,而这个事件是从一个类外部的函数触发的。例如,下面的代码在点击面板时调用onClick方法时运行得很好。
class MyFrame(wx.Frame):
def __init__(self,parent,id):
wx.Frame.__init__(self,parent,id,'Cubit 2D',size=(600,600))
self.Center()
self.panel=(wx.Panel(self))
self.panel.SetBackgroundColour('grey')
self.panel.Bind(wx.EVT_LEFT_DOWN,self.onClick)
def onClick(self,event):
print 'hello'
但是,如果我把onClick函数移动到另一个.py文件中,如下所示,它就不工作了。我该如何将事件信息传递给位于另一个文件中的函数呢?
#main file
import wx
import onclick
class MyFrame(wx.Frame):
def __init__(self,parent,id):
wx.Frame.__init__(self,parent,id,'Cubit 2D',size=(600,600))
self.Center()
self.panel=(wx.Panel(self))
self.panel.SetBackgroundColour('grey')
self.panel.Bind(wx.EVT_LEFT_DOWN,onclick.onClick)
另一个文件中的函数
def onClick(self,event):
print 'hello'
1 个回答
1
只需要把另一个文件中onClick
函数里的self
参数去掉就可以了:
def onClick(event):
print 'hello'
你只在实例方法里需要self
参数,普通函数不需要。