|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +""" |
| 4 | +Example of the very basic, minimal wxPython address book application |
| 5 | +
|
| 6 | +This module defines the main Frame |
| 7 | +""" |
| 8 | + |
| 9 | +import os |
| 10 | + |
| 11 | +import wx |
| 12 | +from address_book_data import AddressBook |
| 13 | +from entry_form import AddBookForm |
| 14 | + |
| 15 | +class AddBookFrame(wx.Frame): |
| 16 | + def __init__(self, add_book, *args, **kwargs): |
| 17 | + kwargs.setdefault('title', "Micro Address Book") |
| 18 | + wx.Frame.__init__(self, *args, **kwargs) |
| 19 | + |
| 20 | + self.add_book = add_book |
| 21 | + self.current_index = 1 |
| 22 | + |
| 23 | + # put the Panel on the frame |
| 24 | + self.entryPanel = AddBookForm(add_book.book[self.current_index], self) |
| 25 | + |
| 26 | + # Build up the menu bar: |
| 27 | + menuBar = wx.MenuBar() |
| 28 | + |
| 29 | + fileMenu = wx.Menu() |
| 30 | + openMenuItem = fileMenu.Append(wx.ID_ANY, "&Open", "Open a file" ) |
| 31 | + self.Bind(wx.EVT_MENU, self.onOpen, openMenuItem) |
| 32 | + |
| 33 | + closeMenuItem = fileMenu.Append(wx.ID_ANY, "&Close", "Close a file" ) |
| 34 | + self.Bind(wx.EVT_MENU, self.onClose, closeMenuItem) |
| 35 | + |
| 36 | + exitMenuItem = fileMenu.Append(wx.ID_EXIT, "Exit", "Exit the application") |
| 37 | + self.Bind(wx.EVT_MENU, self.onExit, exitMenuItem) |
| 38 | + menuBar.Append(fileMenu, "&File") |
| 39 | + |
| 40 | + helpMenu = wx.Menu() |
| 41 | + helpMenuItem = helpMenu.Append(wx.ID_HELP, "Help", "Get help") |
| 42 | + menuBar.Append(helpMenu, "&Help") |
| 43 | + |
| 44 | + self.SetMenuBar(menuBar) |
| 45 | + |
| 46 | + def onOpen(self, evt=None): |
| 47 | + """This method opens an existing file""" |
| 48 | + print "Open a file: " |
| 49 | + # Create the dialog. In this case the current directory is forced as the starting |
| 50 | + # directory for the dialog, and no default file name is forced. This can easily |
| 51 | + # be changed in your program. This is an 'open' dialog, and allows multiple |
| 52 | + # file selections as well. |
| 53 | + # |
| 54 | + # Finally, if the directory is changed in the process of getting files, this |
| 55 | + # dialog is set up to change the current working directory to the path chosen. |
| 56 | + dlg = wx.FileDialog( |
| 57 | + self, message="Choose a file", |
| 58 | + defaultDir=os.getcwd(), |
| 59 | + defaultFile="", |
| 60 | + wildcard="*.json", |
| 61 | + style=wx.OPEN | wx.CHANGE_DIR |
| 62 | + ) |
| 63 | + |
| 64 | + # Show the dialog and retrieve the user response. If it is the OK response, |
| 65 | + # process the data. |
| 66 | + if dlg.ShowModal() == wx.ID_OK: |
| 67 | + # This returns a Python list of files that were selected. |
| 68 | + path = dlg.GetPath() |
| 69 | + print "I'd be opening file in onOpen ", path |
| 70 | + self.add_book.save_to_file( path ) |
| 71 | + else : |
| 72 | + print "The file dialog was canceled before anything was selected" |
| 73 | + |
| 74 | + # Destroy the dialog. Don't do this until you are done with it! |
| 75 | + # BAD things can happen otherwise! |
| 76 | + dlg.Destroy() |
| 77 | + |
| 78 | + def onClose(self, evt=None): |
| 79 | + print "close menu selected" |
| 80 | + self.add_book.close() |
| 81 | + |
| 82 | + def onExit(self, evt=None): |
| 83 | + print "Exit the program here" |
| 84 | + print "The event passed to onExit is type ", type(evt), |
| 85 | + self.Close() |
| 86 | + |
| 87 | + |
| 88 | +class AddBookApp(wx.App): |
| 89 | + def OnInit(self): |
| 90 | + """ |
| 91 | + App initilization goes here -- not much to do, in this case |
| 92 | + """ |
| 93 | + a_book = AddressBook() |
| 94 | + a_book.load_from_file() |
| 95 | + |
| 96 | + f = AddBookFrame(a_book, parent=None) |
| 97 | + f.Show() |
| 98 | + |
| 99 | + return True |
| 100 | + |
| 101 | +if __name__ == "__main__": |
| 102 | + |
| 103 | + app = AddBookApp(False) |
| 104 | + |
| 105 | + |
| 106 | + |
| 107 | + ## set up the WIT -- to help debug sizers |
| 108 | +# import wx.lib.inspection |
| 109 | +# wx.lib.inspection.InspectionTool().Show() |
| 110 | + app.MainLoop() |
| 111 | + |
0 commit comments