Pmw.MenuBar() - manager widget for menu buttons and menus
This class creates a manager widget for containing menus. There are methods to add menu buttons and menus to the menu bar and for adding menu items to the menus. Menu buttons may be added to the left or right of the widget. Each menu button and menu item may have help text to be displayed by a Pmw.Balloon widget.
Menu buttons and menu components are created dynamically by
the addmenu()
method. By default, the buttons are of type
Tkinter.Menubutton and are created with a component group of
Button. Menus are of type Tkinter.Menu and are created with
a component group of Menu.
If the balloon option has been defined, balloonHelp and
statusHelp are passed to the balloon as the help strings for the
menu button. See the bind()
method of Pmw.Balloon for how these
strings may be displayed.
The menu button is created as a component named menuName-button and the menu is created as a component named menuName-menu. The method returns the menu button component widget.
class Demo: def __init__(self, parent): # Create the Balloon. self.balloon = Pmw.Balloon(parent) # Create and pack the MenuBar. menuBar = Pmw.MenuBar(parent, hull_relief = 'raised', hull_borderwidth = 1, balloon = self.balloon) menuBar.pack(fill = 'x') self.menuBar = menuBar # Add some buttons to the MenuBar. 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') 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', side = 'right') 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(parent, 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(parent, entry_width = 40, entry_relief='groove', labelpos = 'w', label_text = 'Status:') self.messageBar.pack(fill = 'x', padx = 10, pady = 10) buttonBox = Pmw.ButtonBox(parent) buttonBox.pack(fill = 'x', expand = 1) buttonBox.add('Disable all', command = menuBar.disableall) buttonBox.add('Enable all', command = menuBar.enableall) buttonBox.add('Create menu', command = self.add) buttonBox.add('Delete menu', command = self.delete) # 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) self.menuBar.addmenuitem(name, 'command', 'Some help', command = PrintOne('Action: new menu ' + name), label = 'Item1') def delete(self): if len(self.testMenuList) == 0: self.menuBar.bell() else: num = self.testMenuList[0] name = 'Menu%d' % num self.testMenuList[:1] = [] self.menuBar.deletemenu(name) class PrintOne: def __init__(self, text): self.text = text def __call__(self): print self.text
Home. Pmw 0.8.3 Maintainer gregm@iname.com. 22 Oct 1999