| 1 | #------------------------------------------------------------------- |
| 2 | # essaimenu.py |
| 3 | # |
| 4 | # menus in wxPython 2.3.3 |
| 5 | # |
| 6 | #------------------------------------------------------------------- |
| 7 | |
| 8 | from wxPython.wx import * |
| 9 | |
| 10 | import time |
| 11 | |
| 12 | #------------------------------------------------------------------- |
| 13 | |
| 14 | class MyFrame(wxFrame): |
| 15 | |
| 16 | def __init__(self, parent, id, log): |
| 17 | wxFrame.__init__(self, parent, id, 'Playing with menus', size=(400, 200)) |
| 18 | self.log = log |
| 19 | self.CenterOnScreen() |
| 20 | |
| 21 | self.CreateStatusBar() |
| 22 | self.SetStatusText("This is the statusbar") |
| 23 | |
| 24 | tc = wxTextCtrl(self, -1, """ |
| 25 | A bunch of bogus menus have been created for this frame. You |
| 26 | can play around with them to see how they behave and then |
| 27 | check the source for this sample to see how to implement them. |
| 28 | """, style=wxTE_READONLY|wxTE_MULTILINE) |
| 29 | |
| 30 | # Prepare the menu bar |
| 31 | menuBar = wxMenuBar() |
| 32 | |
| 33 | # 1st menu from left |
| 34 | menu1 = wxMenu() |
| 35 | menu1.Append(101, "&Mercury", "This the text in the Statusbar") |
| 36 | menu1.Append(102, "&Venus", "") |
| 37 | menu1.Append(103, "&Earth", "You may select Earth too") |
| 38 | menu1.AppendSeparator() |
| 39 | menu1.Append(104, "&Close", "Close this frame") |
| 40 | # Add menu to the menu bar |
| 41 | menuBar.Append(menu1, "&Planets") |
| 42 | |
| 43 | # 2nd menu from left |
| 44 | menu2 = wxMenu() |
| 45 | menu2.Append(201, "Hydrogen") |
| 46 | menu2.Append(202, "Helium") |
| 47 | # a submenu in the 2nd menu |
| 48 | submenu = wxMenu() |
| 49 | submenu.Append(2031,"Lanthanium") |
| 50 | submenu.Append(2032,"Cerium") |
| 51 | submenu.Append(2033,"Praseodymium") |
| 52 | menu2.AppendMenu(203, "Lanthanides", submenu) |
| 53 | # Append 2nd menu |
| 54 | menuBar.Append(menu2, "&Elements") |
| 55 | |
| 56 | menu3 = wxMenu() |
| 57 | menu3.Append(301, "IDLE", "a Python shell using tcl/tk as GUI", wxITEM_RADIO) |
| 58 | menu3.Append(302, "PyCrust", "a Python shell using wxPython as GUI", wxITEM_RADIO) |
| 59 | menu3.Append(303, "psi", "a simple Python shell using wxPython as GUI", wxITEM_RADIO) |
| 60 | menu3.AppendSeparator() |
| 61 | menu3.Append(304, "project1", "", wxITEM_NORMAL) |
| 62 | menu3.Append(305, "project2", "", wxITEM_NORMAL) |
| 63 | menuBar.Append(menu3, "&Shells") |
| 64 | |
| 65 | menu4 = wxMenu() |
| 66 | menu4.Append(401, "letters", "abcde...", wxITEM_CHECK) |
| 67 | menu4.Append(402, "digits", "123...", wxITEM_CHECK) |
| 68 | menu4.Append(403, "letters and digits", "abcd... + 123...", wxITEM_CHECK) |
| 69 | menuBar.Append(menu4, "Chec&k") |
| 70 | |
| 71 | menu5 = wxMenu() |
| 72 | menu5.Append(501, "Interesting thing\tCtrl+A", "Note the shortcut!") |
| 73 | menu5.AppendSeparator() |
| 74 | menu5.Append(502, "Hello\tShift+H") |
| 75 | menu5.AppendSeparator() |
| 76 | menu5.Append(503, "remove the submenu") |
| 77 | menu6 = wxMenu() |
| 78 | menu6.Append(601, "Submenu Item") |
| 79 | menu5.AppendMenu(504, "submenu", menu6) |
| 80 | menu5.Append(505, "remove this menu") |
| 81 | menu5.Append(506, "this is updated") |
| 82 | menu5.Append(507, "insert after this...") |
| 83 | menu5.Append(508, "...and before this") |
| 84 | menuBar.Append(menu5, "&Fun") |
| 85 | |
| 86 | self.SetMenuBar(menuBar) |
| 87 | |
| 88 | # Menu events |
| 89 | EVT_MENU_HIGHLIGHT_ALL(self, self.OnMenuHighlight) |
| 90 | |
| 91 | EVT_MENU(self, 101, self.Menu101) |
| 92 | EVT_MENU(self, 102, self.Menu102) |
| 93 | EVT_MENU(self, 103, self.Menu103) |
| 94 | EVT_MENU(self, 104, self.CloseWindow) |
| 95 | |
| 96 | EVT_MENU(self, 201, self.Menu201) |
| 97 | EVT_MENU(self, 202, self.Menu202) |
| 98 | EVT_MENU(self, 2031, self.Menu2031) |
| 99 | EVT_MENU(self, 2032, self.Menu2032) |
| 100 | EVT_MENU(self, 2033, self.Menu2033) |
| 101 | |
| 102 | EVT_MENU(self, 301, self.Menu301To303) |
| 103 | EVT_MENU(self, 302, self.Menu301To303) |
| 104 | EVT_MENU(self, 303, self.Menu301To303) |
| 105 | EVT_MENU(self, 304, self.Menu304) |
| 106 | EVT_MENU(self, 305, self.Menu305) |
| 107 | |
| 108 | EVT_MENU_RANGE(self, 401, 403, self.Menu401To403) |
| 109 | |
| 110 | EVT_MENU(self, 501, self.Menu501) |
| 111 | EVT_MENU(self, 502, self.Menu502) |
| 112 | EVT_MENU(self, 503, self.TestRemove) |
| 113 | EVT_MENU(self, 505, self.TestRemove2) |
| 114 | EVT_MENU(self, 507, self.TestInsert) |
| 115 | EVT_MENU(self, 508, self.TestInsert) |
| 116 | |
| 117 | EVT_UPDATE_UI(wxGetApp(), 506, self.TestUpdateUI) |
| 118 | |
| 119 | # Methods |
| 120 | |
| 121 | def OnMenuHighlight(self, event): |
| 122 | # Show how to get menu item imfo from this event handler |
| 123 | id = event.GetMenuId() |
| 124 | item = self.GetMenuBar().FindItemById(id) |
| 125 | text = item.GetText() |
| 126 | help = item.GetHelp() |
| 127 | #print text, help |
| 128 | event.Skip() # but in this case just call Skip so the default is done |
| 129 | |
| 130 | |
| 131 | def Menu101(self, event): |
| 132 | self.log.write('Welcome to Mercury\n') |
| 133 | |
| 134 | def Menu102(self, event): |
| 135 | self.log.write('Welcome to Venus\n') |
| 136 | |
| 137 | def Menu103(self, event): |
| 138 | self.log.write('Welcome to the Earth\n') |
| 139 | |
| 140 | def CloseWindow(self, event): |
| 141 | self.Close() |
| 142 | |
| 143 | def Menu201(self, event): |
| 144 | self.log.write('Chemical element number 1\n') |
| 145 | |
| 146 | def Menu202(self, event): |
| 147 | self.log.write('Chemical element number 2\n') |
| 148 | |
| 149 | def Menu2031(self, event): |
| 150 | self.log.write('Element number 57\n') |
| 151 | |
| 152 | def Menu2032(self, event): |
| 153 | self.log.write('Element number 58\n') |
| 154 | |
| 155 | def Menu2033(self, event): |
| 156 | self.log.write('Element number 59\n') |
| 157 | |
| 158 | def Menu301To303(self, event): |
| 159 | id = event.GetId() |
| 160 | self.log.write('Event id: %d\n' % id) |
| 161 | |
| 162 | def Menu304(self, event): |
| 163 | self.log.write('Not yet available\n') |
| 164 | |
| 165 | def Menu305(self, event): |
| 166 | self.log.write('Still vapour\n') |
| 167 | |
| 168 | def Menu401To403(self, event): |
| 169 | self.log.write('From a EVT_MENU_RANGE event\n') |
| 170 | |
| 171 | def Menu501(self, event): |
| 172 | self.log.write('Look in the code how the shortcut has been realized\n') |
| 173 | |
| 174 | def Menu502(self, event): |
| 175 | self.log.write('Hello from Jean-Michel\n') |
| 176 | |
| 177 | |
| 178 | def TestRemove(self, evt): |
| 179 | mb = self.GetMenuBar() |
| 180 | submenuItem = mb.FindItemById(601) |
| 181 | if not submenuItem: |
| 182 | return |
| 183 | submenu = submenuItem.GetMenu() |
| 184 | menu = submenu.GetParent() |
| 185 | |
| 186 | #menu.Remove(504) # works |
| 187 | menu.RemoveItem(mb.FindItemById(504)) # this also works |
| 188 | #menu.RemoveItem(submenuItem) # doesn't work, as expected since submenuItem is not on menu |
| 189 | |
| 190 | |
| 191 | def TestRemove2(self, evt): |
| 192 | mb = self.GetMenuBar() |
| 193 | mb.Remove(4) |
| 194 | |
| 195 | |
| 196 | def TestUpdateUI(self, evt): |
| 197 | text = time.ctime() |
| 198 | evt.SetText(text) |
| 199 | |
| 200 | |
| 201 | def TestInsert(self, evt): |
| 202 | # get the menu |
| 203 | mb = self.GetMenuBar() |
| 204 | menuItem = mb.FindItemById(507) |
| 205 | menu = menuItem.GetMenu() |
| 206 | |
| 207 | ID = wxNewId() |
| 208 | ##menu.Insert(9, ID, "NewItem " + str(ID)) |
| 209 | item = wxMenuItem(menu) |
| 210 | item.SetId(ID) |
| 211 | item.SetText("NewItem " + str(ID)) |
| 212 | menu.InsertItem(9, item) |
| 213 | |
| 214 | |
| 215 | |
| 216 | #------------------------------------------------------------------- |
| 217 | |
| 218 | wxRegisterId(10000) |
| 219 | |
| 220 | def runTest(frame, nb, log): |
| 221 | win = MyFrame(frame, -1, log) |
| 222 | frame.otherWin = win |
| 223 | win.Show(True) |
| 224 | |
| 225 | |
| 226 | #------------------------------------------------------------------- |
| 227 | |
| 228 | |
| 229 | overview = """\ |
| 230 | A demo of using wxMenuBar and wxMenu in various ways. |
| 231 | """ |
| 232 | |
| 233 | |
| 234 | |
| 235 | |
| 236 | if __name__ == '__main__': |
| 237 | import sys,os |
| 238 | import run |
| 239 | run.main(['', os.path.basename(sys.argv[0])]) |
| 240 | |