Pmw.MainMenuBar() - manager for toplevel native menus
This class is a wrapper for the Tkinter.Menu class. It should be used as the main menu of toplevel windows. The class is similar to Pmw.MenuBar, but should be used when native menus are required. See the Tkinter.Menu documentation for full details.
This class should be created as the child of a Tkinter.Toplevel
and should then be specified as the menu associated with the
toplevel, using the toplevel's configure()
method. For example:
# Create a Pmw.MegaToplevel. megaToplevel = Pmw.MegaToplevel() # Get the Tkinter.Toplevel from Pmw.MegaToplevel. toplevel = megaToplevel.interior() # Create the menu bar for the toplevel. menuBar = Pmw.MainMenuBar(toplevel) # Configure the toplevel to use the menuBar. toplevel.configure(menu = menuBar)
There are methods to add menus, both as toplevel menus and sub-menus, and for adding menu items to the menus. Each menu item may have help text to be displayed by a Pmw.Balloon widget. Each menu and cascaded menu (sub-menu) is referenced by name which is supplied on creation.
This megawidget is derived from Pmw.MegaArchetype (not Pmw.MegaWidget like most other megawidgets), with the hull class being Tkinter.Menu.
(Note that due to bugs in Tk's menubar functionality, balloon help has not been impleted and status help does not work properly.)
Due to a bug in some versions of Tk (8.0 and possible others), help text will not be displayed in the balloon widget. However, help text will be displayed in the balloon's associated messagebar. The default is None.
To select a menu, simultaneously press the <Alt> key and the accelerator character indicated on a toplevel menu item. The arrows keys can then be used to select other menus and menu items. To invoke a menu item, press <Return> or press the accelerator character indicated on the menu item.
Each accelerator character will be assigned automatically unless
traverseSpec is supplied to the addmenu()
, addmenuitem()
or
addcascademenu()
methods. The automatically selected
accelerator character for a menu item is the first character in
the label text that has not already been used as an accelerator in
the menu containing the menu item.
If traverseSpec is given, it must be either an integer or a character. If an integer, it specifies the index of the character in the label text to use as the accelerator character. If a character, it specifies the character to use as the accelerator character. The default is 1.
Menu components are created dynamically by the addmenu()
and
addcascademenu()
methods. By default, these are of type
Tkinter.Menu and are created with a component group of Menu.
addmenu()
or addcascademenu()
methods.
A menu item in the parent menu is created (with the
add_cascade()
method of the parent menu) using all keyword
arguments except tearoff and name.
If the label keyword argument is not given, the label option of the menu item defaults to menuName. If the underline keyword argument is not given (and the hotkeys megawidget option is true) the underline option is determined as described under hotkeys and is used to specify the keyboard accelerator.
The statusHelp argument is used as the help string for the menu
item. This is displayed using the showstatus()
method of the
balloon.
The tearoff and name keyword arguments, if present, are passed to the constructor of the menu. See Tkinter.Menu for details of these options. The menu is created as a component named menuName.
addmenu()
or addcascademenu()
methods.
A menu item in the toplevel menu is created (with the
add_cascade()
method) using all keyword arguments except
tearoff and name.
If the label keyword argument is not given, the label option of the menu button defaults to menuName. If the underline keyword argument is not given (and the hotkeys megawidget option is true) the underline option is determined as described under hotkeys and is used to specify the keyboard accelerator.
The statusHelp argument is used as the help string for the menu
item. This is displayed using the showstatus()
method of the
balloon. Currently balloonHelp is not used, due to a bug in Tk
version 8.0.
The tearoff and name keyword arguments, if present, are passed to the constructor of the menu. See Tkinter.Menu for details of these options. The menu is created as a component named menuName.
addcascademenu()
method). Any
keyword arguments present will be passed to the menu when creating
the menu item. See Tkinter.Menu for the valid options for each
item type. In addition, a keyboard accelerator may be
automatically given to the item, as described under hotkeys.
When the mouse is moved over the menu item, the helpString will be displayed by the balloon's statuscommand.
class Demo: def __init__(self, parent): # Create button to launch the toplevel with main menubar. w = Tkinter.Button(parent, text = 'Show Pmw.MainMenuBar demo', command = lambda parent=parent: MainMenuBarToplevel(parent)) w.pack(padx = 8, pady = 8) class MainMenuBarToplevel: def __init__(self, parent): # Create the toplevel to contain the main menubar. megaToplevel = Pmw.MegaToplevel(parent, title = title) toplevel = megaToplevel.interior() # Create the Balloon for this toplevel. self.balloon = Pmw.Balloon(toplevel) # Create and install the MenuBar. menuBar = Pmw.MainMenuBar(toplevel, balloon = self.balloon) toplevel.configure(menu = menuBar) self.menuBar = menuBar # Add some buttons to the MainMenuBar. menuBar.addmenu('File', 'Close this window or exit') menuBar.addmenuitem('File', 'command', 'Close this window', command = PrintOne('Action: close'), label = 'Close') menuBar.addmenuitem('File', 'separator') menuBar.addmenuitem('File', 'command', 'Exit the application', command = PrintOne('Action: exit'), label = 'Exit') menuBar.addmenu('Edit', 'Cut, copy or paste') menuBar.addmenuitem('Edit', 'command', 'Delete the current selection', command = PrintOne('Action: delete'), label = 'Delete') menuBar.addmenu('Options', 'Set user preferences') menuBar.addmenuitem('Options', 'command', 'Set general preferences', command = PrintOne('Action: general options'), label = 'General...') # Create a checkbutton menu item. self.toggleVar = Tkinter.IntVar() # Initialise the checkbutton to 1: self.toggleVar.set(1) menuBar.addmenuitem('Options', 'checkbutton', 'Toggle me on/off', label = 'Toggle', command = self._toggleMe, variable = self.toggleVar) self._toggleMe() menuBar.addcascademenu('Options', 'Size', 'Set some other preferences', traverseSpec = 'z', tearoff = 1) for size in ('tiny', 'small', 'average', 'big', 'huge'): menuBar.addmenuitem('Size', 'command', 'Set size to ' + size, command = PrintOne('Action: size ' + size), label = size) menuBar.addmenu('Help', 'User manuals', name = 'help') menuBar.addmenuitem('Help', 'command', 'About this application', command = PrintOne('Action: about'), label = 'About...') # Create and pack the main part of the window. self.mainPart = Tkinter.Label(toplevel, text = 'This is the\nmain part of\nthe window', background = 'black', foreground = 'white', padx = 30, pady = 30) self.mainPart.pack(fill = 'both', expand = 1) # Create and pack the MessageBar. self.messageBar = Pmw.MessageBar(toplevel, entry_width = 40, entry_relief='groove', labelpos = 'w', label_text = 'Status:') self.messageBar.pack(fill = 'x', padx = 10, pady = 10) self.messageBar.message('state', 'Balloon/status help not working properly - Tk menubar bug') buttonBox = Pmw.ButtonBox(toplevel) buttonBox.pack(fill = 'x') buttonBox.add('Disable\nall', command = menuBar.disableall) buttonBox.add('Enable\nall', command = menuBar.enableall) buttonBox.add('Create\nmenu', command = self.add) buttonBox.add('Delete\nmenu', command = self.delete) buttonBox.add('Create\nitem', command = self.additem) buttonBox.add('Delete\nitem', command = self.deleteitem) # Configure the balloon to displays its status messages in the # message bar. self.balloon.configure(statuscommand = self.messageBar.helpmessage) self.testMenuList = [] def _toggleMe(self): print 'Toggle value:', self.toggleVar.get() def add(self): if len(self.testMenuList) == 0: num = 0 else: num = self.testMenuList[-1] num = num + 1 name = 'Menu%d' % num self.testMenuList.append(num) self.menuBar.addmenu(name, 'This is ' + name) def delete(self): if len(self.testMenuList) == 0: self.menuBar.bell() else: num = self.testMenuList[0] name = 'Menu%d' % num del self.testMenuList[0] self.menuBar.deletemenu(name) def additem(self): if len(self.testMenuList) == 0: self.menuBar.bell() else: num = self.testMenuList[-1] menuName = 'Menu%d' % num menu = self.menuBar.component(menuName) if menu.index('end') is None: label = 'item X' else: label = menu.entrycget('end', 'label') + 'X' self.menuBar.addmenuitem(menuName, 'command', 'Help for ' + label, command = PrintOne('Action: ' + menuName + ': ' + label), label = label) def deleteitem(self): if len(self.testMenuList) == 0: self.menuBar.bell() else: num = self.testMenuList[-1] menuName = 'Menu%d' % num menu = self.menuBar.component(menuName) if menu.index('end') is None: self.menuBar.bell() else: self.menuBar.deletemenuitems(menuName, 0) class PrintOne: def __init__(self, text): self.text = text def __call__(self): print self.text
Home.
Pmw 0.8.5
Maintainer
gregm@iname.com.
9 Feb 2001
Manual page last reviewed: 22 April 2000