wxPython:将多个小部件 - Frame e Panel - 绑定到同一个处理程序
wxPython: binding multiple widgets - Frame e Panel - to the same handler
让我们假设有这个代码:
#!/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()
现在,如果我想从 class "panel1()" 绑定相同的事件处理程序(本例中的 "CloseProgramFromFrame" )怎么办?
即:我想删除 "panel1" 中的 "CloseProgramFromPanel()" 方法,然后绑定 "Exit" 按钮 - 即 "panel1()" 中的 EVT_BUTTON - 到 "CloseProgramFromFrame()"方法中的"wxframesubclass()",怎么办?我有点困惑...
一种可能的方法是:
#!/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()
让我们假设有这个代码:
#!/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()
现在,如果我想从 class "panel1()" 绑定相同的事件处理程序(本例中的 "CloseProgramFromFrame" )怎么办?
即:我想删除 "panel1" 中的 "CloseProgramFromPanel()" 方法,然后绑定 "Exit" 按钮 - 即 "panel1()" 中的 EVT_BUTTON - 到 "CloseProgramFromFrame()"方法中的"wxframesubclass()",怎么办?我有点困惑...
一种可能的方法是:
#!/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()