|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +""" |
| 4 | +Example of the very basic, minimal framework for a wxPython application |
| 5 | +
|
| 6 | +This adds a text box and reads the input from it. |
| 7 | +""" |
| 8 | + |
| 9 | + |
| 10 | +import wx |
| 11 | +from address_book_data import AddressBook |
| 12 | + |
| 13 | +class AppLogic(object): |
| 14 | + """ |
| 15 | + A class to hold the application Application Logic. |
| 16 | +
|
| 17 | + You generally don't want the real logic of the app mixed |
| 18 | + in with the GUI |
| 19 | +
|
| 20 | + In a real app, this would be a substantial collection of |
| 21 | + modules, classes, etc... |
| 22 | + """ |
| 23 | + def file_open(self, filename): |
| 24 | + """This method opens an address book file, reads the contents, and |
| 25 | +returns a list of addresses""" |
| 26 | + global address_book |
| 27 | + address_book = AddressBook() |
| 28 | + print "Using file: %s now"%filename |
| 29 | + address_book.load_from_file(filename) |
| 30 | + |
| 31 | + def file_save(self): |
| 32 | + address_book.save_to_file() |
| 33 | + |
| 34 | +class MainForm(wx.Panel): |
| 35 | + def __init__(self, *args, **kwargs): |
| 36 | + wx.Panel.__init__(self, *args, **kwargs) |
| 37 | + |
| 38 | + ## add a button: |
| 39 | + theButton1 = wx.Button(self, label="Push Me") |
| 40 | + theButton1.Bind(wx.EVT_BUTTON, self.onButton) |
| 41 | + |
| 42 | + ## add a static text lable: |
| 43 | + label1 = wx.StaticText(self, label="Input Box:") |
| 44 | + |
| 45 | + ## add a text control: |
| 46 | + self.inTextControl = wx.TextCtrl(self) |
| 47 | + |
| 48 | + ## add another button: |
| 49 | + theButton2 = wx.Button(self, label="GetData") |
| 50 | + theButton2.Bind(wx.EVT_BUTTON, self.onGetData) |
| 51 | + |
| 52 | + ## add a static text lable: |
| 53 | + label2 = wx.StaticText(self, label="Output Box:") |
| 54 | + ## and another text control: |
| 55 | + self.outTextControl = wx.TextCtrl(self, style=wx.TE_READONLY) |
| 56 | + |
| 57 | + |
| 58 | + ## do the layout |
| 59 | + buttonSizer = wx.BoxSizer(wx.VERTICAL) |
| 60 | + |
| 61 | + buttonSizer.Add(theButton1, 0, wx.GROW | wx.ALL, 4) |
| 62 | + buttonSizer.Add(label1, 0, wx.ALIGN_LEFT | wx.TOP, 4) |
| 63 | + buttonSizer.Add(self.inTextControl, 0, wx.GROW | wx.ALL, 4) |
| 64 | + buttonSizer.Add((150, 10)) |
| 65 | + buttonSizer.Add(theButton2, 0, wx.GROW | wx.ALL, 4) |
| 66 | + buttonSizer.Add(label2, 0, wx.ALIGN_LEFT | wx.TOP, 4) |
| 67 | + buttonSizer.Add(self.outTextControl, 0, wx.GROW | wx.ALL, 4) |
| 68 | + |
| 69 | + ## need another sizer to get the horizonal placement right: |
| 70 | + mainSizer = wx.BoxSizer(wx.HORIZONTAL) |
| 71 | + mainSizer.Add((1,1), 1) # stretchable space |
| 72 | + mainSizer.Add(buttonSizer, 0, wx.ALIGN_TOP) # the sizer with the buttons in it |
| 73 | + mainSizer.Add((1,1), 1) # stretchable space |
| 74 | + |
| 75 | + self.SetSizer(mainSizer) |
| 76 | + |
| 77 | + def onButton(self, evt=None): |
| 78 | + print "You pushed one of the buttons!" |
| 79 | + |
| 80 | + def onGetData(self, evt=None): |
| 81 | + print "get data button pressed" |
| 82 | + contents = self.inTextControl.Value |
| 83 | + print "the contents are:", contents |
| 84 | + |
| 85 | + self.outTextControl.Value = self.inTextControl.Value |
| 86 | + |
| 87 | + |
| 88 | +class TestFrame(wx.Frame): |
| 89 | + def __init__(self, app_logic, *args, **kwargs): |
| 90 | + kwargs.setdefault('title', "Simple test App") |
| 91 | + wx.Frame.__init__(self, *args, **kwargs) |
| 92 | + |
| 93 | + self.app_logic = app_logic |
| 94 | + |
| 95 | + # put the Panel on the frame |
| 96 | + self.buttonPanel = MainForm(self) |
| 97 | + |
| 98 | + # Build up the menu bar: |
| 99 | + menuBar = wx.MenuBar() |
| 100 | + |
| 101 | + fileMenu = wx.Menu() |
| 102 | + openMenuItem = fileMenu.Append(wx.ID_ANY, "&Open", "Open a file" ) |
| 103 | + self.Bind(wx.EVT_MENU, self.onOpen, openMenuItem) |
| 104 | + |
| 105 | + closeMenuItem = fileMenu.Append(wx.ID_ANY, "&Close", "Close a file" ) |
| 106 | + self.Bind(wx.EVT_MENU, self.onClose, closeMenuItem) |
| 107 | + |
| 108 | + exitMenuItem = fileMenu.Append(wx.ID_EXIT, "Exit", "Exit the application") |
| 109 | + self.Bind(wx.EVT_MENU, self.onExit, exitMenuItem) |
| 110 | + menuBar.Append(fileMenu, "&File") |
| 111 | + |
| 112 | + helpMenu = wx.Menu() |
| 113 | + helpMenuItem = helpMenu.Append(wx.ID_HELP, "Help", "Get help") |
| 114 | + menuBar.Append(helpMenu, "&Help") |
| 115 | + |
| 116 | + self.SetMenuBar(menuBar) |
| 117 | + |
| 118 | + def onOpen(self, evt=None): |
| 119 | + """This method opens an existing file""" |
| 120 | + print "Open a file: " |
| 121 | + # Create the dialog. In this case the current directory is forced as the starting |
| 122 | + # directory for the dialog, and no default file name is forced. This can easily |
| 123 | + # be changed in your program. This is an 'open' dialog, and allows multiple |
| 124 | + # file selections as well. |
| 125 | + # |
| 126 | + # Finally, if the directory is changed in the process of getting files, this |
| 127 | + # dialog is set up to change the current working directory to the path chosen. |
| 128 | + dlg = wx.FileDialog( |
| 129 | + self, message="Choose a file", |
| 130 | + defaultDir=os.getcwd(), |
| 131 | + defaultFile="", |
| 132 | + wildcard=wildcard, |
| 133 | + style=wx.OPEN | wx.CHANGE_DIR |
| 134 | + ) |
| 135 | + |
| 136 | + # Show the dialog and retrieve the user response. If it is the OK response, |
| 137 | + # process the data. |
| 138 | + if dlg.ShowModal() == wx.ID_OK: |
| 139 | + # This returns a Python list of files that were selected. |
| 140 | + path = dlg.GetPath() |
| 141 | + print "I'd be opening file in onOpen ", path |
| 142 | + self.app_logic.file_open( path ) |
| 143 | + else : |
| 144 | + print "The file dialog was canceled before anything was selected" |
| 145 | + |
| 146 | + # Destroy the dialog. Don't do this until you are done with it! |
| 147 | + # BAD things can happen otherwise! |
| 148 | + dlg.Destroy() |
| 149 | + |
| 150 | + def onClose(self, evt=None): |
| 151 | + print "close menu selected" |
| 152 | + self.app_logic.file_close() |
| 153 | + |
| 154 | + def onExit(self, evt=None): |
| 155 | + print "Exit the program here" |
| 156 | + print "The event passed to onExit is type ", type(evt), |
| 157 | + self.Close() |
| 158 | + |
| 159 | + |
| 160 | +class TestApp(wx.App): |
| 161 | + def OnInit(self): |
| 162 | + """ |
| 163 | + App initilization goes here -- not much to do, in this case |
| 164 | + """ |
| 165 | + app_logic = AppLogic() |
| 166 | + f = TestFrame(app_logic, parent=None) |
| 167 | + f.Show() |
| 168 | + |
| 169 | + return True |
| 170 | + |
| 171 | +if __name__ == "__main__": |
| 172 | + |
| 173 | + app = TestApp(False) |
| 174 | + ## set up the WIT -- to help debug sizers |
| 175 | +# import wx.lib.inspection |
| 176 | +# wx.lib.inspection.InspectionTool().Show() |
| 177 | + app.MainLoop() |
| 178 | + |
0 commit comments