1 title = 'Pmw.MainMenuBar demonstration'
3 # Import Pmw from this directory tree.
5 sys.path[:0] = ['../../..']
11 def __init__(self, parent):
12 # Create button to launch the toplevel with main menubar.
13 w = Tkinter.Button(parent, text = 'Show Pmw.MainMenuBar demo',
14 command = lambda parent=parent: MainMenuBarToplevel(parent))
15 w.pack(padx = 8, pady = 8)
17 class MainMenuBarToplevel:
18 def __init__(self, parent):
19 # Create the toplevel to contain the main menubar.
20 megaToplevel = Pmw.MegaToplevel(parent, title = title)
21 toplevel = megaToplevel.interior()
23 # Create the Balloon for this toplevel.
24 self.balloon = Pmw.Balloon(toplevel)
26 # Create and install the MenuBar.
27 menuBar = Pmw.MainMenuBar(toplevel,
28 balloon = self.balloon)
29 toplevel.configure(menu = menuBar)
30 self.menuBar = menuBar
32 # Add some buttons to the MainMenuBar.
33 menuBar.addmenu('File', 'Close this window or exit')
34 menuBar.addmenuitem('File', 'command', 'Close this window',
35 command = PrintOne('Action: close'),
37 menuBar.addmenuitem('File', 'separator')
38 menuBar.addmenuitem('File', 'command', 'Exit the application',
39 command = PrintOne('Action: exit'),
42 menuBar.addmenu('Edit', 'Cut, copy or paste')
43 menuBar.addmenuitem('Edit', 'command', 'Delete the current selection',
44 command = PrintOne('Action: delete'),
47 menuBar.addmenu('Options', 'Set user preferences')
48 menuBar.addmenuitem('Options', 'command', 'Set general preferences',
49 command = PrintOne('Action: general options'),
52 # Create a checkbutton menu item.
53 self.toggleVar = Tkinter.IntVar()
54 # Initialise the checkbutton to 1:
56 menuBar.addmenuitem('Options', 'checkbutton', 'Toggle me on/off',
58 command = self._toggleMe,
59 variable = self.toggleVar)
62 menuBar.addcascademenu('Options', 'Size',
63 'Set some other preferences', traverseSpec = 'z', tearoff = 1)
64 for size in ('tiny', 'small', 'average', 'big', 'huge'):
65 menuBar.addmenuitem('Size', 'command', 'Set size to ' + size,
66 command = PrintOne('Action: size ' + size),
69 menuBar.addmenu('Help', 'User manuals', name = 'help')
70 menuBar.addmenuitem('Help', 'command', 'About this application',
71 command = PrintOne('Action: about'),
74 # Create and pack the main part of the window.
75 self.mainPart = Tkinter.Label(toplevel,
76 text = 'This is the\nmain part of\nthe window',
81 self.mainPart.pack(fill = 'both', expand = 1)
83 # Create and pack the MessageBar.
84 self.messageBar = Pmw.MessageBar(toplevel,
86 entry_relief='groove',
88 label_text = 'Status:')
89 self.messageBar.pack(fill = 'x', padx = 10, pady = 10)
90 self.messageBar.message('state',
91 'Balloon/status help not working properly - Tk menubar bug')
93 buttonBox = Pmw.ButtonBox(toplevel)
94 buttonBox.pack(fill = 'x')
95 buttonBox.add('Disable\nall', command = menuBar.disableall)
96 buttonBox.add('Enable\nall', command = menuBar.enableall)
97 buttonBox.add('Create\nmenu', command = self.add)
98 buttonBox.add('Delete\nmenu', command = self.delete)
99 buttonBox.add('Create\nitem', command = self.additem)
100 buttonBox.add('Delete\nitem', command = self.deleteitem)
102 # Configure the balloon to displays its status messages in the
104 self.balloon.configure(statuscommand = self.messageBar.helpmessage)
106 self.testMenuList = []
109 print 'Toggle value:', self.toggleVar.get()
112 if len(self.testMenuList) == 0:
115 num = self.testMenuList[-1]
117 name = 'Menu%d' % num
118 self.testMenuList.append(num)
120 self.menuBar.addmenu(name, 'This is ' + name)
123 if len(self.testMenuList) == 0:
126 num = self.testMenuList[0]
127 name = 'Menu%d' % num
128 del self.testMenuList[0]
129 self.menuBar.deletemenu(name)
132 if len(self.testMenuList) == 0:
135 num = self.testMenuList[-1]
136 menuName = 'Menu%d' % num
137 menu = self.menuBar.component(menuName)
138 if menu.index('end') is None:
141 label = menu.entrycget('end', 'label') + 'X'
142 self.menuBar.addmenuitem(menuName, 'command', 'Help for ' + label,
143 command = PrintOne('Action: ' + menuName + ': ' + label),
146 def deleteitem(self):
147 if len(self.testMenuList) == 0:
150 num = self.testMenuList[-1]
151 menuName = 'Menu%d' % num
152 menu = self.menuBar.component(menuName)
153 if menu.index('end') is None:
156 self.menuBar.deletemenuitems(menuName, 0)
159 def __init__(self, text):
165 ######################################################################
167 # Create demo in root window for testing.
168 if __name__ == '__main__':
173 exitButton = Tkinter.Button(root, text = 'Exit', command = root.destroy)
174 exitButton.pack(side = 'bottom')