| 1 | title = 'Pmw.MainMenuBar demonstration' |
| 2 | |
| 3 | # Import Pmw from this directory tree. |
| 4 | import sys |
| 5 | sys.path[:0] = ['../../..'] |
| 6 | |
| 7 | import Tkinter |
| 8 | import Pmw |
| 9 | |
| 10 | class Demo: |
| 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) |
| 16 | |
| 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() |
| 22 | |
| 23 | # Create the Balloon for this toplevel. |
| 24 | self.balloon = Pmw.Balloon(toplevel) |
| 25 | |
| 26 | # Create and install the MenuBar. |
| 27 | menuBar = Pmw.MainMenuBar(toplevel, |
| 28 | balloon = self.balloon) |
| 29 | toplevel.configure(menu = menuBar) |
| 30 | self.menuBar = menuBar |
| 31 | |
| 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'), |
| 36 | label = 'Close') |
| 37 | menuBar.addmenuitem('File', 'separator') |
| 38 | menuBar.addmenuitem('File', 'command', 'Exit the application', |
| 39 | command = PrintOne('Action: exit'), |
| 40 | label = 'Exit') |
| 41 | |
| 42 | menuBar.addmenu('Edit', 'Cut, copy or paste') |
| 43 | menuBar.addmenuitem('Edit', 'command', 'Delete the current selection', |
| 44 | command = PrintOne('Action: delete'), |
| 45 | label = 'Delete') |
| 46 | |
| 47 | menuBar.addmenu('Options', 'Set user preferences') |
| 48 | menuBar.addmenuitem('Options', 'command', 'Set general preferences', |
| 49 | command = PrintOne('Action: general options'), |
| 50 | label = 'General...') |
| 51 | |
| 52 | # Create a checkbutton menu item. |
| 53 | self.toggleVar = Tkinter.IntVar() |
| 54 | # Initialise the checkbutton to 1: |
| 55 | self.toggleVar.set(1) |
| 56 | menuBar.addmenuitem('Options', 'checkbutton', 'Toggle me on/off', |
| 57 | label = 'Toggle', |
| 58 | command = self._toggleMe, |
| 59 | variable = self.toggleVar) |
| 60 | self._toggleMe() |
| 61 | |
| 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), |
| 67 | label = size) |
| 68 | |
| 69 | menuBar.addmenu('Help', 'User manuals', name = 'help') |
| 70 | menuBar.addmenuitem('Help', 'command', 'About this application', |
| 71 | command = PrintOne('Action: about'), |
| 72 | label = 'About...') |
| 73 | |
| 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', |
| 77 | background = 'black', |
| 78 | foreground = 'white', |
| 79 | padx = 30, |
| 80 | pady = 30) |
| 81 | self.mainPart.pack(fill = 'both', expand = 1) |
| 82 | |
| 83 | # Create and pack the MessageBar. |
| 84 | self.messageBar = Pmw.MessageBar(toplevel, |
| 85 | entry_width = 40, |
| 86 | entry_relief='groove', |
| 87 | labelpos = 'w', |
| 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') |
| 92 | |
| 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) |
| 101 | |
| 102 | # Configure the balloon to displays its status messages in the |
| 103 | # message bar. |
| 104 | self.balloon.configure(statuscommand = self.messageBar.helpmessage) |
| 105 | |
| 106 | self.testMenuList = [] |
| 107 | |
| 108 | def _toggleMe(self): |
| 109 | print 'Toggle value:', self.toggleVar.get() |
| 110 | |
| 111 | def add(self): |
| 112 | if len(self.testMenuList) == 0: |
| 113 | num = 0 |
| 114 | else: |
| 115 | num = self.testMenuList[-1] |
| 116 | num = num + 1 |
| 117 | name = 'Menu%d' % num |
| 118 | self.testMenuList.append(num) |
| 119 | |
| 120 | self.menuBar.addmenu(name, 'This is ' + name) |
| 121 | |
| 122 | def delete(self): |
| 123 | if len(self.testMenuList) == 0: |
| 124 | self.menuBar.bell() |
| 125 | else: |
| 126 | num = self.testMenuList[0] |
| 127 | name = 'Menu%d' % num |
| 128 | del self.testMenuList[0] |
| 129 | self.menuBar.deletemenu(name) |
| 130 | |
| 131 | def additem(self): |
| 132 | if len(self.testMenuList) == 0: |
| 133 | self.menuBar.bell() |
| 134 | else: |
| 135 | num = self.testMenuList[-1] |
| 136 | menuName = 'Menu%d' % num |
| 137 | menu = self.menuBar.component(menuName) |
| 138 | if menu.index('end') is None: |
| 139 | label = 'item X' |
| 140 | else: |
| 141 | label = menu.entrycget('end', 'label') + 'X' |
| 142 | self.menuBar.addmenuitem(menuName, 'command', 'Help for ' + label, |
| 143 | command = PrintOne('Action: ' + menuName + ': ' + label), |
| 144 | label = label) |
| 145 | |
| 146 | def deleteitem(self): |
| 147 | if len(self.testMenuList) == 0: |
| 148 | self.menuBar.bell() |
| 149 | else: |
| 150 | num = self.testMenuList[-1] |
| 151 | menuName = 'Menu%d' % num |
| 152 | menu = self.menuBar.component(menuName) |
| 153 | if menu.index('end') is None: |
| 154 | self.menuBar.bell() |
| 155 | else: |
| 156 | self.menuBar.deletemenuitems(menuName, 0) |
| 157 | |
| 158 | class PrintOne: |
| 159 | def __init__(self, text): |
| 160 | self.text = text |
| 161 | |
| 162 | def __call__(self): |
| 163 | print self.text |
| 164 | |
| 165 | ###################################################################### |
| 166 | |
| 167 | # Create demo in root window for testing. |
| 168 | if __name__ == '__main__': |
| 169 | root = Tkinter.Tk() |
| 170 | Pmw.initialise(root) |
| 171 | root.title(title) |
| 172 | |
| 173 | exitButton = Tkinter.Button(root, text = 'Exit', command = root.destroy) |
| 174 | exitButton.pack(side = 'bottom') |
| 175 | widget = Demo(root) |
| 176 | root.mainloop() |