1 #-------------------------------------------------------------------
4 # menus in wxPython 2.3.3
6 #-------------------------------------------------------------------
8 from wxPython
.wx
import *
12 #-------------------------------------------------------------------
14 class MyFrame(wxFrame
):
16 def __init__(self
, parent
, id, log
):
17 wxFrame
.__init
__(self
, parent
, id, 'Playing with menus', size
=(400, 200))
21 self
.CreateStatusBar()
22 self
.SetStatusText("This is the statusbar")
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
)
30 # Prepare the menu bar
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")
45 menu2
.Append(201, "Hydrogen")
46 menu2
.Append(202, "Helium")
47 # a submenu in the 2nd menu
49 submenu
.Append(2031,"Lanthanium")
50 submenu
.Append(2032,"Cerium")
51 submenu
.Append(2033,"Praseodymium")
52 menu2
.AppendMenu(203, "Lanthanides", submenu
)
54 menuBar
.Append(menu2
, "&Elements")
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")
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")
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
)
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")
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")
91 self
.SetMenuBar(menuBar
)
94 EVT_MENU_HIGHLIGHT_ALL(self
, self
.OnMenuHighlight
)
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
)
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
)
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
)
113 EVT_MENU_RANGE(self
, 401, 403, self
.Menu401To403
)
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
)
123 EVT_UPDATE_UI(wxGetApp(), 506, self
.TestUpdateUI
)
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()
134 event
.Skip() # but in this case just call Skip so the default is done
137 def Menu101(self
, event
):
138 self
.log
.write('Welcome to Mercury\n')
140 def Menu102(self
, event
):
141 self
.log
.write('Welcome to Venus\n')
143 def Menu103(self
, event
):
144 self
.log
.write('Welcome to the Earth\n')
146 def CloseWindow(self
, event
):
149 def Menu201(self
, event
):
150 self
.log
.write('Chemical element number 1\n')
152 def Menu202(self
, event
):
153 self
.log
.write('Chemical element number 2\n')
155 def Menu2031(self
, event
):
156 self
.log
.write('Element number 57\n')
158 def Menu2032(self
, event
):
159 self
.log
.write('Element number 58\n')
161 def Menu2033(self
, event
):
162 self
.log
.write('Element number 59\n')
164 def Menu301To303(self
, event
):
166 self
.log
.write('Event id: %d\n' % id)
168 def Menu304(self
, event
):
169 self
.log
.write('Not yet available\n')
171 def Menu305(self
, event
):
172 self
.log
.write('Still vapour\n')
174 def Menu401To403(self
, event
):
175 self
.log
.write('From a EVT_MENU_RANGE event\n')
177 def Menu500(self
, event
):
178 self
.log
.write('Have a happy day!\n')
180 def Menu501(self
, event
):
181 self
.log
.write('Look in the code how the shortcut has been realized\n')
183 def Menu502(self
, event
):
184 self
.log
.write('Hello from Jean-Michel\n')
187 def TestRemove(self
, evt
):
188 mb
= self
.GetMenuBar()
189 submenuItem
= mb
.FindItemById(601)
192 submenu
= submenuItem
.GetMenu()
193 menu
= submenu
.GetParent()
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
200 def TestRemove2(self
, evt
):
201 mb
= self
.GetMenuBar()
205 def TestUpdateUI(self
, evt
):
210 def TestInsert(self
, evt
):
212 mb
= self
.GetMenuBar()
213 menuItem
= mb
.FindItemById(507)
214 menu
= menuItem
.GetMenu()
217 ##menu.Insert(9, ID, "NewItem " + str(ID))
218 item
= wxMenuItem(menu
)
220 item
.SetText("NewItem " + str(ID
))
221 menu
.InsertItem(9, item
)
225 #-------------------------------------------------------------------
229 def runTest(frame
, nb
, log
):
230 win
= MyFrame(frame
, -1, log
)
235 #-------------------------------------------------------------------
239 A demo of using wxMenuBar and wxMenu in various ways.
245 if __name__
== '__main__':
248 run
.main(['', os
.path
.basename(sys
.argv
[0])])