Hi Robin and all
A small application showing the new features of the menus in 2.3.3.
Error in wxPython reference: kind are not in uppercase, wxItem_Check
instead of wxITEM_CHECK, ...
···
#-------------------------------------------------------------------
# essaimenu.py
#
# menus in wxPython 2.3.3
#
#-------------------------------------------------------------------
from wxPython.wx import *
#-------------------------------------------------------------------
class MyFrame(wxFrame):
def __init__(self, parent, id):
wxFrame.__init__(self, parent, id, 'Playing with menus', \
wxDefaultPosition, wxSize(400, 200))
self.CenterOnScreen()
#Status bar in a Frame and not in a Panel
self.CreateStatusBar()
self.SetStatusText("This is the statusbar")
#Menus, in Frame and not in a Panel!
#Prepare the menu bar
menuBar = wxMenuBar()
# 1st menu from left
menu1 = wxMenu()
menu1.Append(101, "Mercury", "This the text in the Statusbar")
menu1.Append(102, "Venus", "")
menu1.Append(103, "Earth", "You may select Earth too")
menu1.AppendSeparator()
menu1.Append(104, "Exit", "Close this nice application")
#Add menu to the menu bat
menuBar.Append(menu1, "&Planets")
# 2nd menu from left
menu2 = wxMenu()
menu2.Append(201, "Hydrogen")
menu2.Append(202, "Helium")
# a submenu in the 2nd menu
submenu = wxMenu()
submenu.Append(2031,"Lanthanium")
submenu.Append(2032,"Cerium")
submenu.Append(2033,"Praseodymium")
menu2.AppendMenu(203, "Lanthanides", submenu)
#Append 2nd menu
menuBar.Append(menu2, "&Elements")
menu3 = wxMenu()
menu3.Append(301, "IDLE", "a Python shell using tcl/tk as GUI",
wxITEM_RADIO)
menu3.Append(302, "PyCrust", "a Python shell using wxPython as GUI",
wxITEM_RADIO)
menu3.Append(303, "psi", "a simple Python shell using wxPython as
GUI", wxITEM_RADIO)
menu3.AppendSeparator()
menu3.Append(304, "project1", "", wxITEM_NORMAL)
menu3.Append(305, "project2", "", wxITEM_NORMAL)
menuBar.Append(menu3, "&Shells")
menu4 = wxMenu()
menu4.Append(401, "letters", "abcde...", wxITEM_CHECK)
menu4.Append(402, "digits", "123...", wxITEM_CHECK)
menu4.Append(403, "letters and digits", "abcd... + 123...",
wxITEM_CHECK)
menuBar.Append(menu4, "Chec&k")
menu5 = wxMenu()
menu5.Append(501, "Interesting thing\tCtrl+A", "Note the shortcut!")
#what for?
menu5.Append(503, "---", "", wxITEM_SEPARATOR)
menu5.Append(502, "Hello\tShift+H")
menuBar.Append(menu5, "&Fun")
# et l'ajoute au frame
self.SetMenuBar(menuBar)
# Menu events
EVT_MENU(self, 101, self.Menu101)
EVT_MENU(self, 102, self.Menu102)
EVT_MENU(self, 103, self.Menu103)
EVT_MENU(self, 104, self.OnCloseWindow)
EVT_MENU(self, 201, self.Menu201)
EVT_MENU(self, 202, self.Menu202)
EVT_MENU(self, 2031, self.Menu2031)
EVT_MENU(self, 2032, self.Menu2032)
EVT_MENU(self, 2033, self.Menu2033)
EVT_MENU(self, 301, self.Menu301To303)
EVT_MENU(self, 302, self.Menu301To303)
EVT_MENU(self, 303, self.Menu301To303)
EVT_MENU(self, 304, self.Menu304)
EVT_MENU(self, 305, self.Menu305)
EVT_MENU_RANGE(self, 401, 403, self.Menu401To403)
EVT_MENU(self, 501, self.Menu501)
EVT_MENU(self, 502, self.Menu502)
# Methods
def Menu101(self, event):
wxMessageBox('Welcome to Mercury', "Message", wxOK, None)
def Menu102(self, event):
wxMessageBox('Welcome to Venus', "Message", wxOK, None)
def Menu103(self, event):
wxMessageBox('Welcome to the Earth', "Message", wxOK, None)
def OnCloseWindow(self, event):
self.Destroy()
def Menu201(self, event):
wxMessageBox('Chemical element number 1', 'Chemical elements', wxOK,
None)
def Menu202(self, event):
wxMessageBox('Chemical element number 2', 'Chemical elements', wxOK,
None)
def Menu2031(self, event):
wxMessageBox('Element number 57', 'Lanthanides family', wxOK, None)
def Menu2032(self, event):
wxMessageBox('Element number 58', 'Lanthanides family', wxOK, None)
def Menu2033(self, event):
wxMessageBox('Element number 59', 'Lanthanides family', wxOK, None)
def Menu301To303(self, event):
id = event.GetId()
wxMessageBox('Event id: ' + str(id), '', wxOK, None)
def Menu304(self, event):
wxMessageBox('Not yet available', 'Projects', wxOK, None)
def Menu305(self, event):
wxMessageBox('Still vapour', 'Projects', wxOK, None)
def Menu401To403(self, event):
wxMessageBox('From a EVT_MENU_RANGE event', 'Message', wxOK, None)
def Menu501(self, event):
txt = 'Look in the code how the shortcut has been realized'
wxMessageBox(txt, 'Shortcut', wxOK | wxICON_EXCLAMATION, None)
def Menu502(self, event):
wxMessageBox('Hello from Jean-Michel', 'A message', wxOK |
wxICON_INFORMATION, None)
#-------------------------------------------------------------------
class MyApp(wxApp):
def OnInit(self):
frame = MyFrame(NULL, -1)
frame.Show(true)
self.SetTopWindow(frame)
return true
#-------------------------------------------------------------------
def main():
app = MyApp(0)
app.MainLoop()
#-------------------------------------------------------------------
if __name__ == "__main__" :
main()
#eof-------------------------------------------------------------------
Jean-Michel Fauth, Switzerland