|
| 1 | +import wx |
| 2 | + |
| 3 | +def file_open(obj): |
| 4 | + """This method opens a file""" |
| 5 | + print "Open a file from file_open()" |
| 6 | + |
| 7 | +def file_close(obj): |
| 8 | + """This method closes a file""" |
| 9 | + print "Close a file from file_close()" |
| 10 | + |
| 11 | + |
| 12 | + |
| 13 | +class MyForm(wx.Frame): |
| 14 | + |
| 15 | + |
| 16 | + def __init__(self): |
| 17 | + wx.Frame.__init__(self, None, wx.ID_ANY, "wx.Menu Tutorial") |
| 18 | + |
| 19 | + # Add a panel so it looks the correct on all platforms |
| 20 | + self.panel = wx.Panel(self, wx.ID_ANY) |
| 21 | + |
| 22 | + menuBar = wx.MenuBar() |
| 23 | + |
| 24 | + fileMenu = wx.Menu() |
| 25 | + openMenuItem = fileMenu.Append(wx.NewId(), "Open", |
| 26 | + "Open a file" ) |
| 27 | + self.Bind(wx.EVT_MENU, file_open, openMenuItem ) |
| 28 | + closeMenuItem = fileMenu.Append(wx.NewId(), "Close", |
| 29 | + "Close a file" ) |
| 30 | + self.Bind(wx.EVT_MENU, file_close, closeMenuItem ) |
| 31 | + exitMenuItem = fileMenu.Append(wx.NewId(), "Exit", |
| 32 | + "Exit the application") |
| 33 | + self.Bind(wx.EVT_MENU, self.onExit, exitMenuItem) |
| 34 | + menuBar.Append(fileMenu, "&File") |
| 35 | + |
| 36 | + helpMenu = wx.Menu() |
| 37 | + helpMenuItem = helpMenu.Append(wx.NewId(), "Help", |
| 38 | + "Get help") |
| 39 | + menuBar.Append(helpMenu, "&Help") |
| 40 | + self.SetMenuBar(menuBar) |
| 41 | + |
| 42 | + def onExit(self, obj): |
| 43 | + print "Exit the program here" |
| 44 | + print "The object passed to onExit is type ", type(obj),\ |
| 45 | + "and has attributes", dir(obj) |
| 46 | + |
| 47 | + |
| 48 | + |
| 49 | +# Run the program |
| 50 | +if __name__ == "__main__": |
| 51 | + app = wx.PySimpleApp() |
| 52 | + frame = MyForm().Show() |
| 53 | + app.MainLoop() |
0 commit comments