|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +""" |
| 4 | +Example of the very basic, minimal farmework for a wxPython application |
| 5 | +""" |
| 6 | + |
| 7 | +import wx |
| 8 | + |
| 9 | + |
| 10 | +class AppLogic(object): |
| 11 | + """ |
| 12 | + A class to hold the application Application Logic. |
| 13 | +
|
| 14 | + You generally don't want the real logic of the app mixed |
| 15 | + in with the GUI |
| 16 | +
|
| 17 | + In a real app, this would be a substantial collection of |
| 18 | + modules, classes, etc... |
| 19 | + """ |
| 20 | + def file_open(self, filename="default_name"): |
| 21 | + """This method opens a file""" |
| 22 | + print "Open a file: " |
| 23 | + print "I'd be opening file: %s now"%filename |
| 24 | + |
| 25 | + def file_close(self): |
| 26 | + """This method closes a file""" |
| 27 | + print "Close a file: " |
| 28 | + print "I'd be closing a file now" |
| 29 | + |
| 30 | + |
| 31 | +class TestFrame(wx.Frame): |
| 32 | + def __init__(self, app_logic, *args, **kwargs): |
| 33 | + kwargs.setdefault('title', "Simple test App") |
| 34 | + wx.Frame.__init__(self, *args, **kwargs) |
| 35 | + |
| 36 | + self.app_logic = app_logic |
| 37 | + |
| 38 | + # Add a panel so it looks the correct on all platforms |
| 39 | + self.panel = wx.Panel(self, wx.ID_ANY) |
| 40 | + |
| 41 | + |
| 42 | + # Build up the menu bar: |
| 43 | + menuBar = wx.MenuBar() |
| 44 | + |
| 45 | + fileMenu = wx.Menu() |
| 46 | + openMenuItem = fileMenu.Append(wx.ID_ANY, "&Open", "Open a file" ) |
| 47 | + self.Bind(wx.EVT_MENU, self.onOpen, openMenuItem) |
| 48 | + |
| 49 | + closeMenuItem = fileMenu.Append(wx.ID_ANY, "&Close", "Close a file" ) |
| 50 | + self.Bind(wx.EVT_MENU, self.onClose, closeMenuItem) |
| 51 | + |
| 52 | + exitMenuItem = fileMenu.Append(wx.ID_EXIT, "Exit", "Exit the application") |
| 53 | + self.Bind(wx.EVT_MENU, self.onExit, exitMenuItem) |
| 54 | + menuBar.Append(fileMenu, "&File") |
| 55 | + |
| 56 | + helpMenu = wx.Menu() |
| 57 | + helpMenuItem = helpMenu.Append(wx.ID_HELP, "Help", "Get help") |
| 58 | + menuBar.Append(helpMenu, "&Help") |
| 59 | + |
| 60 | + self.SetMenuBar(menuBar) |
| 61 | + |
| 62 | + def onOpen(self, evt=None): |
| 63 | + print "open menu selected" |
| 64 | + self.app_logic.file_open() |
| 65 | + |
| 66 | + def onClose(self, evt=None): |
| 67 | + print "close menu selected" |
| 68 | + self.app_logic.file_close() |
| 69 | + |
| 70 | + def onExit(self, evt=None): |
| 71 | + print "Exit the program here" |
| 72 | + print "The event passed to onExit is type ", type(evt), |
| 73 | + self.Close() |
| 74 | + |
| 75 | + |
| 76 | +class TestApp(wx.App): |
| 77 | + def OnInit(self): |
| 78 | + """ |
| 79 | + App initilization goes here -- not much to do, in this case |
| 80 | + """ |
| 81 | + app_logic = AppLogic() |
| 82 | + f = TestFrame(app_logic, parent=None) |
| 83 | + f.Show() |
| 84 | + |
| 85 | + return True |
| 86 | + |
| 87 | +if __name__ == "__main__": |
| 88 | + app = TestApp() |
| 89 | + app.MainLoop() |
| 90 | + |
0 commit comments