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 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")
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")
86 self
.SetMenuBar(menuBar
)
89 EVT_MENU_HIGHLIGHT_ALL(self
, self
.OnMenuHighlight
)
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
)
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
)
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
)
108 EVT_MENU_RANGE(self
, 401, 403, self
.Menu401To403
)
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
)
117 EVT_UPDATE_UI(wxGetApp(), 506, self
.TestUpdateUI
)
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()
128 event
.Skip() # but in this case just call Skip so the default is done
131 def Menu101(self
, event
):
132 self
.log
.write('Welcome to Mercury\n')
134 def Menu102(self
, event
):
135 self
.log
.write('Welcome to Venus\n')
137 def Menu103(self
, event
):
138 self
.log
.write('Welcome to the Earth\n')
140 def CloseWindow(self
, event
):
143 def Menu201(self
, event
):
144 self
.log
.write('Chemical element number 1\n')
146 def Menu202(self
, event
):
147 self
.log
.write('Chemical element number 2\n')
149 def Menu2031(self
, event
):
150 self
.log
.write('Element number 57\n')
152 def Menu2032(self
, event
):
153 self
.log
.write('Element number 58\n')
155 def Menu2033(self
, event
):
156 self
.log
.write('Element number 59\n')
158 def Menu301To303(self
, event
):
160 self
.log
.write('Event id: %d\n' % id)
162 def Menu304(self
, event
):
163 self
.log
.write('Not yet available\n')
165 def Menu305(self
, event
):
166 self
.log
.write('Still vapour\n')
168 def Menu401To403(self
, event
):
169 self
.log
.write('From a EVT_MENU_RANGE event\n')
171 def Menu501(self
, event
):
172 self
.log
.write('Look in the code how the shortcut has been realized\n')
174 def Menu502(self
, event
):
175 self
.log
.write('Hello from Jean-Michel\n')
178 def TestRemove(self
, evt
):
179 mb
= self
.GetMenuBar()
180 submenuItem
= mb
.FindItemById(601)
183 submenu
= submenuItem
.GetMenu()
184 menu
= submenu
.GetParent()
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
191 def TestRemove2(self
, evt
):
192 mb
= self
.GetMenuBar()
196 def TestUpdateUI(self
, evt
):
201 def TestInsert(self
, evt
):
203 mb
= self
.GetMenuBar()
204 menuItem
= mb
.FindItemById(507)
205 menu
= menuItem
.GetMenu()
208 ##menu.Insert(9, ID, "NewItem " + str(ID))
209 item
= wxMenuItem(menu
)
211 item
.SetText("NewItem " + str(ID
))
212 menu
.InsertItem(9, item
)
216 #-------------------------------------------------------------------
220 def runTest(frame
, nb
, log
):
221 win
= MyFrame(frame
, -1, log
)
226 #-------------------------------------------------------------------
230 A demo of using wxMenuBar and wxMenu in various ways.
236 if __name__
== '__main__':
239 run
.main(['', os
.path
.basename(sys
.argv
[0])])