我们假设有这个代码:
#!/usr/bin/python
# _*_ coding: utf-8 _*_
import wx
class wxappsubclass(wx.App):
def OnInit(self):
frame=wxframesubclass(None, -1, 'MyName')
frame.Show(True)
return True
class wxframesubclass(wx.Frame):
def __init__(self, parent, id, title):
wx.Frame.__init__(self, parent, id, title, wx.DefaultPosition, size=(320, 240))
panel=panel1(self)
menubar=wx.MenuBar()
menufile=wx.Menu()
ExitItem=menufile.Append(wx.NewId(), '&Exit\tCtrl+Q', "Exit")
menubar.Append(menufile, '&File')
self.SetMenuBar(menubar)
## BINDING ##
self.Bind(wx.EVT_MENU, self.CloseProgramFromFrame, ExitItem)
def CloseProgramFromFrame(self, event):
self.Close(True)
class panel1(wx.Panel):
def __init__(self, parent):
wx.Panel.__init__(self, parent, -1)
ID_EXIT_BUTTON=wx.NewId()
ExitButton=wx.Button(self, wx.NewId(), label="Exit")
self.Bind(wx.EVT_BUTTON, self.CloseProgramFromPanel, ExitButton)
def CloseProgramFromPanel(self, event):
parente=self.GetParent()
parente.Destroy()
if __name__ == '__main__':
app=wxappsubclass()
app.MainLoop()
现在,如果我想从类“panel1()”绑定相同的事件处理程序(在此示例中为“CloseProgramFromFrame”)怎么办?
ie:我想删除“panel1”中的“CloseProgramFromPanel()”方法,然后将“退出”按钮 - 即“panel1()”中的EVT_BUTTON绑定到“wxframesubclass”中的“CloseProgramFromFrame()”方法)“, 怎么做?我有点困惑......
答案 0 :(得分:1)
其中一种可能的方法是:
#!/usr/bin/python
# _*_ coding: utf-8 _*_
import wx
class wxappsubclass(wx.App):
def OnInit(self):
frame=wxframesubclass(None, -1, 'MyName')
frame.Show(True)
return True
class wxframesubclass(wx.Frame):
def __init__(self, parent, id, title):
wx.Frame.__init__(self, parent, id, title, wx.DefaultPosition, size=(320, 240))
self.panel=panel1(self)
menubar=wx.MenuBar()
menufile=wx.Menu()
ExitItem=menufile.Append(wx.NewId(), '&Exit\tCtrl+Q', "Exit")
menubar.Append(menufile, '&File')
self.SetMenuBar(menubar)
## BINDING ##
self.Bind(wx.EVT_MENU, self.CloseProgramFromFrame, ExitItem)
self.Bind(wx.EVT_BUTTON, self.CloseProgramFromFrame, self.panel.ExitButton)
def CloseProgramFromFrame(self, event):
self.Close(True)
class panel1(wx.Panel):
def __init__(self, parent):
wx.Panel.__init__(self, parent, -1)
ID_EXIT_BUTTON=wx.NewId()
self.ExitButton=wx.Button(self, wx.NewId(), label="Exit")
if __name__ == '__main__':
app=wxappsubclass()
app.MainLoop()