# # 1 #!/usr/bin/env python 2 #--------------------------------------------- 3 # Name: Cookie.Py 4 # Purpose: A silly `fortune' application 5 #--------------------------------------------- 7 ## import all of the wxPython GUI package 8 from wxPython.wx import * 9 10 ## Create a new frame class, derived from the wxPython Frame. 11 class MyFrame(wxFrame): 12 def __init__(self, parent, id, title): 13 self.myFortunes = [ 14 "A fox is a wolf who sends flowers.", 15 "A good man always knows his limitations.", 16 "A man may be so much of everything that " +\ 17 "he is nothing of anything.", 18 ] 19 self.which = 0 20 myMenuTable = ( 21 ['&File', 22 ('&New', 'Get a new cookie', self.OnButtonClick), 23 ('---',), 24 ('E&xit', 'Leave the application', self.exitApp)], 25 ) 26 # Call the parent class method 27 wxFrame.__init__(self, parent, id, title, 28 wxPoint(100, 100), wxSize(160, 100)) 29 # Add a panel to place things in 30 panel = wxPanel(self, -1) 31 self.text = wxStaticText(panel, 1009, \ 32 "Click on button to get a cookie!", \ 33 wxPoint(40,25), wxSize(150,80)) 34 button = wxButton(self, 1010, "New cookie", \ 35 wxPoint(150,90), wxSize(70,20)) 36 # window layout 37 box = wxBoxSizer(wxVERTICAL) 38 spacer = wxBoxSizer(wxHORIZONTAL) 39 spacer.Add(0, 0, 1, wxEXPAND) 40 spacer.Add(button, 0, wxALIGN_BOTTOM) 41 spacer.Add(0, 0, 1, wxEXPAND) 42 box.Add(panel, 1, wxEXPAND) 43 box.Add(spacer, 0, wxEXPAND) 44 # associate button with event 45 EVT_BUTTON(self, 1010, self.OnButtonClick) 46 # associate close event with handling method 47 EVT_CLOSE(self, self.OnCloseWindow) 48 # make a status bar 49 self.CreateStatusBar() 50 # Make a main menu 51 self.mainmenu = wxMenuBar() 52 # fill the `file menu table' 53 for submenu in myMenuTable: 54 newMenu = wxMenu() 55 for item in submenu[1:]: 56 if (item[0] != '---'): 57 fmID = NewId() 58 newMenu.Append(fmID, item[0], item[1]) 59 EVT_MENU(self, fmID, item[2]) 60 else: 61 newMenu.AppendSeparator() 62 # fill the main menu 63 self.mainmenu.Append(newMenu, submenu[0]) 64 self.SetMenuBar(self.mainmenu) 65 self.Fit() 66 self.SetSizer(box) 67 self.SetAutoLayout(true) 69 def OnButtonClick(self, event): 70 self.text.SetLabel(self.myFortunes[self.which \ 71 % len(self.myFortunes)]) 72 self.which = self.which + 1 74 # This method is called automatically when the CLOSE event is 75 # sent to this window 76 def OnCloseWindow(self, event): 77 ruSure=wxMessageDialog(self, "Do you want to quit?", \ 78 "Leaving Cookie", \ 79 wxOK | wxCANCEL | wxICON_QUESTION) 80 if (ruSure.ShowModal() == wxID_CANCEL): 81 event.Veto() 82 else: 83 print "# Bye bye" 84 # tell the window to kill itself 85 self.Destroy() 87 def exitApp(self,event): 88 self.Close() 90 # Every wxWindows application must have a class derived from wxApp 91 class MyApp(wxApp): 92 # wxWindows calls this method to initialize the application 93 def OnInit(self): 94 # Create an instance of our customized Frame class 95 frame = MyFrame(NULL, -1, "Cookies!") 96 frame.Show(true) 97 # Tell wxWindows that this is our main window 98 self.SetTopWindow(frame) 99 # Return a success flag 100 return true 102 # if running standalone 103 if __name__ == "__main__": 104 app = MyApp(0) # Create an instance of the application class 105 app.MainLoop() # Tell it to start processing events