好吧,这是一种方法。这是其中包含菜单代码的文件:
# impMenu.py
import wx
#----------------------------------------------------------------------
def menu(self):
""""""
menuBar = wx.MenuBar()
fileMenu = wx.Menu()
exitMenuItem = fileMenu.Append(wx.NewId(), "Exit",
"Exit the application")
menuBar.Append(fileMenu, "&File")
menuItems = [(exitMenuItem, "onExit")]
return (menuBar, menuItems)
这是主程序代码:
import impMenu
import wx
########################################################################
class MyFrame(wx.Frame):
""""""
#----------------------------------------------------------------------
def __init__(self):
"""Constructor"""
wx.Frame.__init__(self, None, title="Menu Test")
panel = wx.Panel(self)
menubar, menuItems = impMenu.menu(self)
for item in menuItems:
self.Bind(wx.EVT_MENU, getattr(self, item[1]), item[0])
self.SetMenuBar(menubar)
#----------------------------------------------------------------------
def onExit(self, event):
""""""
print "in onExit!"
if __name__ == "__main__":
app = wx.App(False)
frame = MyFrame()
frame.Show()
app.MainLoop()