1 title = 'Pmw.MenuBar demonstration'
3 # Import Pmw from this directory tree.
5 sys.path[:0] = ['../../..']
11 def __init__(self, parent):
13 self.balloon = Pmw.Balloon(parent)
15 # Create and pack the MenuBar.
16 menuBar = Pmw.MenuBar(parent,
17 hull_relief = 'raised',
19 balloon = self.balloon)
20 menuBar.pack(fill = 'x')
21 self.menuBar = menuBar
23 # Add some buttons to the MenuBar.
24 menuBar.addmenu('File', 'Close this window or exit')
25 menuBar.addmenuitem('File', 'command', 'Close this window',
26 command = PrintOne('Action: close'),
28 menuBar.addmenuitem('File', 'separator')
29 menuBar.addmenuitem('File', 'command', 'Exit the application',
30 command = PrintOne('Action: exit'),
33 menuBar.addmenu('Edit', 'Cut, copy or paste')
34 menuBar.addmenuitem('Edit', 'command', 'Delete the current selection',
35 command = PrintOne('Action: delete'),
38 menuBar.addmenu('Options', 'Set user preferences')
39 menuBar.addmenuitem('Options', 'command', 'Set general preferences',
40 command = PrintOne('Action: general options'),
43 # Create a checkbutton menu item.
44 self.toggleVar = Tkinter.IntVar()
45 # Initialise the checkbutton to 1:
47 menuBar.addmenuitem('Options', 'checkbutton', 'Toggle me on/off',
49 command = self._toggleMe,
50 variable = self.toggleVar)
53 menuBar.addcascademenu('Options', 'Size',
54 'Set some other preferences', traverseSpec = 'z', tearoff = 1)
55 for size in ('tiny', 'small', 'average', 'big', 'huge'):
56 menuBar.addmenuitem('Size', 'command', 'Set size to ' + size,
57 command = PrintOne('Action: size ' + size),
60 menuBar.addmenu('Help', 'User manuals', side = 'right')
61 menuBar.addmenuitem('Help', 'command', 'About this application',
62 command = PrintOne('Action: about'),
65 # Create and pack the main part of the window.
66 self.mainPart = Tkinter.Label(parent,
67 text = 'This is the\nmain part of\nthe window',
72 self.mainPart.pack(fill = 'both', expand = 1)
74 # Create and pack the MessageBar.
75 self.messageBar = Pmw.MessageBar(parent,
77 entry_relief='groove',
79 label_text = 'Status:')
80 self.messageBar.pack(fill = 'x', padx = 10, pady = 10)
81 self.messageBar.message('state', 'OK')
83 buttonBox = Pmw.ButtonBox(parent)
84 buttonBox.pack(fill = 'x')
85 buttonBox.add('Disable\nall', command = menuBar.disableall)
86 buttonBox.add('Enable\nall', command = menuBar.enableall)
87 buttonBox.add('Create\nmenu', command = self.add)
88 buttonBox.add('Delete\nmenu', command = self.delete)
89 buttonBox.add('Create\nitem', command = self.additem)
90 buttonBox.add('Delete\nitem', command = self.deleteitem)
92 # Configure the balloon to displays its status messages in the
94 self.balloon.configure(statuscommand = self.messageBar.helpmessage)
96 self.testMenuList = []
99 print 'Toggle value:', self.toggleVar.get()
102 if len(self.testMenuList) == 0:
105 num = self.testMenuList[-1]
107 name = 'Menu%d' % num
108 self.testMenuList.append(num)
110 self.menuBar.addmenu(name, 'This is ' + name)
113 if len(self.testMenuList) == 0:
116 num = self.testMenuList[0]
117 name = 'Menu%d' % num
118 del self.testMenuList[0]
119 self.menuBar.deletemenu(name)
122 if len(self.testMenuList) == 0:
125 num = self.testMenuList[-1]
126 menuName = 'Menu%d' % num
127 menu = self.menuBar.component(menuName + '-menu')
128 if menu.index('end') is None:
131 label = menu.entrycget('end', 'label') + 'X'
132 self.menuBar.addmenuitem(menuName, 'command', 'Help for ' + label,
133 command = PrintOne('Action: ' + menuName + ': ' + label),
136 def deleteitem(self):
137 if len(self.testMenuList) == 0:
140 num = self.testMenuList[-1]
141 menuName = 'Menu%d' % num
142 menu = self.menuBar.component(menuName + '-menu')
143 if menu.index('end') is None:
146 self.menuBar.deletemenuitems(menuName, 0)
149 def __init__(self, text):
155 ######################################################################
157 # Create demo in root window for testing.
158 if __name__ == '__main__':
163 exitButton = Tkinter.Button(root, text = 'Exit', command = root.destroy)
164 exitButton.pack(side = 'bottom')