]> git.saurik.com Git - wxWidgets.git/blob - wxPython/demo/wxMenu.py
Lots of demo tweaks for API updates, bug fixes and new images for the
[wxWidgets.git] / wxPython / demo / wxMenu.py
1 #-------------------------------------------------------------------
2 # essaimenu.py
3 #
4 # menus in wxPython 2.3.3
5 #
6 #-------------------------------------------------------------------
7
8 from wxPython.wx import *
9 import images
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 # Show how to put an icon in the menu
73 item = wxMenuItem(menu5, 500, "&Smile!\tCtrl+S", "This one has an icon")
74 item.SetBitmap(images.getSmilesBitmap())
75 menu5.AppendItem(item)
76
77 menu5.Append(501, "Interesting thing\tCtrl+A", "Note the shortcut!")
78 menu5.AppendSeparator()
79 menu5.Append(502, "Hello\tShift+H")
80 menu5.AppendSeparator()
81 menu5.Append(503, "remove the submenu")
82 menu6 = wxMenu()
83 menu6.Append(601, "Submenu Item")
84 menu5.AppendMenu(504, "submenu", menu6)
85 menu5.Append(505, "remove this menu")
86 menu5.Append(506, "this is updated")
87 menu5.Append(507, "insert after this...")
88 menu5.Append(508, "...and before this")
89 menuBar.Append(menu5, "&Fun")
90
91 self.SetMenuBar(menuBar)
92
93 # Menu events
94 EVT_MENU_HIGHLIGHT_ALL(self, self.OnMenuHighlight)
95
96 EVT_MENU(self, 101, self.Menu101)
97 EVT_MENU(self, 102, self.Menu102)
98 EVT_MENU(self, 103, self.Menu103)
99 EVT_MENU(self, 104, self.CloseWindow)
100
101 EVT_MENU(self, 201, self.Menu201)
102 EVT_MENU(self, 202, self.Menu202)
103 EVT_MENU(self, 2031, self.Menu2031)
104 EVT_MENU(self, 2032, self.Menu2032)
105 EVT_MENU(self, 2033, self.Menu2033)
106
107 EVT_MENU(self, 301, self.Menu301To303)
108 EVT_MENU(self, 302, self.Menu301To303)
109 EVT_MENU(self, 303, self.Menu301To303)
110 EVT_MENU(self, 304, self.Menu304)
111 EVT_MENU(self, 305, self.Menu305)
112
113 EVT_MENU_RANGE(self, 401, 403, self.Menu401To403)
114
115 EVT_MENU(self, 500, self.Menu500)
116 EVT_MENU(self, 501, self.Menu501)
117 EVT_MENU(self, 502, self.Menu502)
118 EVT_MENU(self, 503, self.TestRemove)
119 EVT_MENU(self, 505, self.TestRemove2)
120 EVT_MENU(self, 507, self.TestInsert)
121 EVT_MENU(self, 508, self.TestInsert)
122
123 EVT_UPDATE_UI(wxGetApp(), 506, self.TestUpdateUI)
124
125 # Methods
126
127 def OnMenuHighlight(self, event):
128 # Show how to get menu item imfo from this event handler
129 id = event.GetMenuId()
130 item = self.GetMenuBar().FindItemById(id)
131 text = item.GetText()
132 help = item.GetHelp()
133 #print text, help
134 event.Skip() # but in this case just call Skip so the default is done
135
136
137 def Menu101(self, event):
138 self.log.write('Welcome to Mercury\n')
139
140 def Menu102(self, event):
141 self.log.write('Welcome to Venus\n')
142
143 def Menu103(self, event):
144 self.log.write('Welcome to the Earth\n')
145
146 def CloseWindow(self, event):
147 self.Close()
148
149 def Menu201(self, event):
150 self.log.write('Chemical element number 1\n')
151
152 def Menu202(self, event):
153 self.log.write('Chemical element number 2\n')
154
155 def Menu2031(self, event):
156 self.log.write('Element number 57\n')
157
158 def Menu2032(self, event):
159 self.log.write('Element number 58\n')
160
161 def Menu2033(self, event):
162 self.log.write('Element number 59\n')
163
164 def Menu301To303(self, event):
165 id = event.GetId()
166 self.log.write('Event id: %d\n' % id)
167
168 def Menu304(self, event):
169 self.log.write('Not yet available\n')
170
171 def Menu305(self, event):
172 self.log.write('Still vapour\n')
173
174 def Menu401To403(self, event):
175 self.log.write('From a EVT_MENU_RANGE event\n')
176
177 def Menu500(self, event):
178 self.log.write('Have a happy day!\n')
179
180 def Menu501(self, event):
181 self.log.write('Look in the code how the shortcut has been realized\n')
182
183 def Menu502(self, event):
184 self.log.write('Hello from Jean-Michel\n')
185
186
187 def TestRemove(self, evt):
188 mb = self.GetMenuBar()
189 submenuItem = mb.FindItemById(601)
190 if not submenuItem:
191 return
192 submenu = submenuItem.GetMenu()
193 menu = submenu.GetParent()
194
195 #menu.Remove(504) # works
196 menu.RemoveItem(mb.FindItemById(504)) # this also works
197 #menu.RemoveItem(submenuItem) # doesn't work, as expected since submenuItem is not on menu
198
199
200 def TestRemove2(self, evt):
201 mb = self.GetMenuBar()
202 mb.Remove(4)
203
204
205 def TestUpdateUI(self, evt):
206 text = time.ctime()
207 evt.SetText(text)
208
209
210 def TestInsert(self, evt):
211 theID = 508
212 # get the menu
213 mb = self.GetMenuBar()
214 menuItem = mb.FindItemById(theID)
215 menu = menuItem.GetMenu()
216
217 # figure out the position to insert at
218 pos = 0
219 for i in menu.GetMenuItems():
220 if i.GetId() == theID:
221 break
222 pos += 1
223
224 # now insert the new item
225 ID = wxNewId()
226 ##menu.Insert(pos, ID, "NewItem " + str(ID))
227 item = wxMenuItem(menu)
228 item.SetId(ID)
229 item.SetText("NewItem " + str(ID))
230 menu.InsertItem(pos, item)
231
232
233
234 #-------------------------------------------------------------------
235
236 wxRegisterId(10000)
237
238 def runTest(frame, nb, log):
239 win = MyFrame(frame, -1, log)
240 frame.otherWin = win
241 win.Show(True)
242
243
244 #-------------------------------------------------------------------
245
246
247 overview = """\
248 A demo of using wxMenuBar and wxMenu in various ways.
249 """
250
251
252
253
254 if __name__ == '__main__':
255 import sys,os
256 import run
257 run.main(['', os.path.basename(sys.argv[0])])
258