|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +""" |
| 4 | +Example of the very basic, minimal farmework for a wxPython application |
| 5 | +
|
| 6 | +This version adds a basic menu bar with a file menu |
| 7 | +""" |
| 8 | + |
| 9 | +import wx |
| 10 | +import os |
| 11 | + |
| 12 | +#--------------------------------------------------------------------------- |
| 13 | + |
| 14 | +# This is how you pre-establish a file filter so that the dialog |
| 15 | +# only shows the extension(s) you want it to. |
| 16 | +wildcard = "Python source (*.py)|*.py|" \ |
| 17 | + "Compiled Python (*.pyc)|*.pyc|" \ |
| 18 | + "SPAM files (*.spam)|*.spam|" \ |
| 19 | + "Egg file (*.egg)|*.egg|" \ |
| 20 | + "All files (*.*)|*.*" |
| 21 | + |
| 22 | +#--------------------------------------------------------------------------- |
| 23 | + |
| 24 | + |
| 25 | +class AppLogic(object): |
| 26 | + """ |
| 27 | + A class to hold the application Application Logic. |
| 28 | +
|
| 29 | + You generally don't want the real logic of the app mixed |
| 30 | + in with the GUI |
| 31 | +
|
| 32 | + In a real app, this would be a substantial collection of |
| 33 | + modules, classes, etc... |
| 34 | + """ |
| 35 | + |
| 36 | + def file_open(self, filename="default_name"): |
| 37 | + """This method opens a file""" |
| 38 | + print "Open a file: " |
| 39 | + print "I'd be opening file: %s now"%filename |
| 40 | + |
| 41 | + def file_close(self): |
| 42 | + """This method closes a file""" |
| 43 | + print "Close a file: " |
| 44 | + print "I'd be closing a file now" |
| 45 | + |
| 46 | + |
| 47 | + |
| 48 | +class TestFrame(wx.Frame): |
| 49 | + def __init__(self, app_logic, *args, **kwargs): |
| 50 | + kwargs.setdefault('title', "Simple test App") |
| 51 | + wx.Frame.__init__(self, *args, **kwargs) |
| 52 | + |
| 53 | + self.app_logic = app_logic |
| 54 | + |
| 55 | + # Add a panel so it looks the correct on all platforms |
| 56 | + self.panel = wx.Panel(self, wx.ID_ANY) |
| 57 | + |
| 58 | + |
| 59 | + # Build up the menu bar: |
| 60 | + menuBar = wx.MenuBar() |
| 61 | + |
| 62 | + fileMenu = wx.Menu() |
| 63 | + |
| 64 | + newMenuItem = fileMenu.Append(wx.ID_ANY, "&New", "Create a new file") |
| 65 | + self.Bind(wx.EVT_MENU, self.onNew, newMenuItem ) |
| 66 | + |
| 67 | + openMenuItem = fileMenu.Append(wx.ID_ANY, "&Open", "Open an existing file" ) |
| 68 | + self.Bind(wx.EVT_MENU, self.onOpen, openMenuItem) |
| 69 | + |
| 70 | + closeMenuItem = fileMenu.Append(wx.ID_ANY, "&Close", "Close a file" ) |
| 71 | + self.Bind(wx.EVT_MENU, self.onClose, closeMenuItem) |
| 72 | + |
| 73 | + exitMenuItem = fileMenu.Append(wx.ID_EXIT, "Exit", "Exit the application") |
| 74 | + self.Bind(wx.EVT_MENU, self.onExit, exitMenuItem) |
| 75 | + menuBar.Append(fileMenu, "&File") |
| 76 | + |
| 77 | + helpMenu = wx.Menu() |
| 78 | + helpMenuItem = helpMenu.Append(wx.ID_HELP, "Help", "Get help") |
| 79 | + menuBar.Append(helpMenu, "&Help") |
| 80 | + |
| 81 | + self.SetMenuBar(menuBar) |
| 82 | + |
| 83 | +# def onNew (self, evt=None): |
| 84 | +# print "Create menu selected" |
| 85 | +# self.file_create() |
| 86 | + |
| 87 | +# def onOpen(self, evt=None): |
| 88 | +# print "open menu selected" |
| 89 | + |
| 90 | + def onClose(self, evt=None): |
| 91 | + print "close menu selected" |
| 92 | + self.file_close() |
| 93 | + |
| 94 | + def onExit(self, evt=None): |
| 95 | + print "Exit the program here" |
| 96 | + print "The event passed to onExit is type ", type(evt), |
| 97 | + self.Close() |
| 98 | + |
| 99 | + def onNew ( self, evt=None ): |
| 100 | + """This method creates a new file""" |
| 101 | + |
| 102 | + # Create the dialog. In this case the current directory is forced as the starting |
| 103 | + # directory for the dialog, and no default file name is forced. This can easilly |
| 104 | + # be changed in your program. This is an 'save' dialog. |
| 105 | + # |
| 106 | + # Unlike the 'open dialog' example found elsewhere, this example does NOT |
| 107 | + # force the current working directory to change if the user chooses a different |
| 108 | + # directory than the one initially set. |
| 109 | + dlg = wx.FileDialog( |
| 110 | + self, message="Save file as ...", defaultDir=os.getcwd(), |
| 111 | + defaultFile="", wildcard=wildcard, style=wx.SAVE ) |
| 112 | + |
| 113 | + # This sets the default filter that the user will initially see. Otherwise, |
| 114 | + # the first filter in the list will be used by default. |
| 115 | + dlg.SetFilterIndex(2) |
| 116 | + |
| 117 | + # Show the dialog and retrieve the user response. If it is the OK response, |
| 118 | + # process the data. |
| 119 | + if dlg.ShowModal() == wx.ID_OK: |
| 120 | + path = dlg.GetPath() |
| 121 | + |
| 122 | + # Normally, at this point you would save your data using the file and path |
| 123 | + # data that the user provided to you, but since we didn't actually start |
| 124 | + # with any data to work with, that would be difficult. |
| 125 | + # |
| 126 | + # The code to do so would be similar to this, assuming 'data' contains |
| 127 | + # the data you want to save: |
| 128 | + # |
| 129 | + # fp = file(path, 'w') # Create file anew |
| 130 | + # fp.write(data) |
| 131 | + # fp.close() |
| 132 | + # |
| 133 | + # You might want to add some error checking :-) |
| 134 | + # |
| 135 | + |
| 136 | + # Note that the current working dir didn't change. This is good since |
| 137 | + # that's the way we set it up. |
| 138 | + |
| 139 | + # Destroy the dialog. Don't do this until you are done with it! |
| 140 | + # BAD things can happen otherwise! |
| 141 | + dlg.Destroy() |
| 142 | + |
| 143 | + |
| 144 | + |
| 145 | + |
| 146 | + def onOpen(self, evt=None): |
| 147 | + """This method opens an existing file""" |
| 148 | + print "Open a file: " |
| 149 | + # Create the dialog. In this case the current directory is forced as the starting |
| 150 | + # directory for the dialog, and no default file name is forced. This can easilly |
| 151 | + # be changed in your program. This is an 'open' dialog, and allows multitple |
| 152 | + # file selections as well. |
| 153 | + # |
| 154 | + # Finally, if the directory is changed in the process of getting files, this |
| 155 | + # dialog is set up to change the current working directory to the path chosen. |
| 156 | + dlg = wx.FileDialog( |
| 157 | + self, message="Choose a file", |
| 158 | + defaultDir=os.getcwd(), |
| 159 | + defaultFile="", |
| 160 | + wildcard=wildcard, |
| 161 | + style=wx.OPEN | wx.CHANGE_DIR |
| 162 | + ) |
| 163 | + |
| 164 | + # Show the dialog and retrieve the user response. If it is the OK response, |
| 165 | + # process the data. |
| 166 | + if dlg.ShowModal() == wx.ID_OK: |
| 167 | + # This returns a Python list of files that were selected. |
| 168 | + path = dlg.GetPath() |
| 169 | + |
| 170 | + |
| 171 | + |
| 172 | + |
| 173 | + # Destroy the dialog. Don't do this until you are done with it! |
| 174 | + # BAD things can happen otherwise! |
| 175 | + dlg.Destroy() |
| 176 | + |
| 177 | + print "I'd be opening file in onOpen ", path |
| 178 | + self.app_logic.file_open( path ) |
| 179 | + |
| 180 | + def file_close(self): |
| 181 | + """This method closes a file""" |
| 182 | + print "Close a file: " |
| 183 | + print "I'd be closing a file now" |
| 184 | + |
| 185 | + |
| 186 | +class TestApp(wx.App): |
| 187 | + def OnInit(self): |
| 188 | + """ |
| 189 | + App initilization goes here -- not much to do, in this case |
| 190 | + """ |
| 191 | + app_logic = AppLogic() |
| 192 | + f = TestFrame(app_logic, parent=None) |
| 193 | +# assert isinstance(f, wxWindow) |
| 194 | + f.Show() |
| 195 | + |
| 196 | + return True |
| 197 | + |
| 198 | +if __name__ == "__main__": |
| 199 | + app = TestApp() |
| 200 | + app.MainLoop() |
| 201 | + |
0 commit comments