| 1 | title = 'Pmw.MenuBar 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 the Balloon. |
| 13 | self.balloon = Pmw.Balloon(parent) |
| 14 | |
| 15 | # Create and pack the MenuBar. |
| 16 | menuBar = Pmw.MenuBar(parent, |
| 17 | hull_relief = 'raised', |
| 18 | hull_borderwidth = 1, |
| 19 | balloon = self.balloon) |
| 20 | menuBar.pack(fill = 'x') |
| 21 | self.menuBar = menuBar |
| 22 | |
| 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'), |
| 27 | label = 'Close') |
| 28 | menuBar.addmenuitem('File', 'separator') |
| 29 | menuBar.addmenuitem('File', 'command', 'Exit the application', |
| 30 | command = PrintOne('Action: exit'), |
| 31 | label = 'Exit') |
| 32 | |
| 33 | menuBar.addmenu('Edit', 'Cut, copy or paste') |
| 34 | menuBar.addmenuitem('Edit', 'command', 'Delete the current selection', |
| 35 | command = PrintOne('Action: delete'), |
| 36 | label = 'Delete') |
| 37 | |
| 38 | menuBar.addmenu('Options', 'Set user preferences') |
| 39 | menuBar.addmenuitem('Options', 'command', 'Set general preferences', |
| 40 | command = PrintOne('Action: general options'), |
| 41 | label = 'General...') |
| 42 | |
| 43 | # Create a checkbutton menu item. |
| 44 | self.toggleVar = Tkinter.IntVar() |
| 45 | # Initialise the checkbutton to 1: |
| 46 | self.toggleVar.set(1) |
| 47 | menuBar.addmenuitem('Options', 'checkbutton', 'Toggle me on/off', |
| 48 | label = 'Toggle', |
| 49 | command = self._toggleMe, |
| 50 | variable = self.toggleVar) |
| 51 | self._toggleMe() |
| 52 | |
| 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), |
| 58 | label = size) |
| 59 | |
| 60 | menuBar.addmenu('Help', 'User manuals', side = 'right') |
| 61 | menuBar.addmenuitem('Help', 'command', 'About this application', |
| 62 | command = PrintOne('Action: about'), |
| 63 | label = 'About...') |
| 64 | |
| 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', |
| 68 | background = 'black', |
| 69 | foreground = 'white', |
| 70 | padx = 30, |
| 71 | pady = 30) |
| 72 | self.mainPart.pack(fill = 'both', expand = 1) |
| 73 | |
| 74 | # Create and pack the MessageBar. |
| 75 | self.messageBar = Pmw.MessageBar(parent, |
| 76 | entry_width = 40, |
| 77 | entry_relief='groove', |
| 78 | labelpos = 'w', |
| 79 | label_text = 'Status:') |
| 80 | self.messageBar.pack(fill = 'x', padx = 10, pady = 10) |
| 81 | self.messageBar.message('state', 'OK') |
| 82 | |
| 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) |
| 91 | |
| 92 | # Configure the balloon to displays its status messages in the |
| 93 | # message bar. |
| 94 | self.balloon.configure(statuscommand = self.messageBar.helpmessage) |
| 95 | |
| 96 | self.testMenuList = [] |
| 97 | |
| 98 | def _toggleMe(self): |
| 99 | print 'Toggle value:', self.toggleVar.get() |
| 100 | |
| 101 | def add(self): |
| 102 | if len(self.testMenuList) == 0: |
| 103 | num = 0 |
| 104 | else: |
| 105 | num = self.testMenuList[-1] |
| 106 | num = num + 1 |
| 107 | name = 'Menu%d' % num |
| 108 | self.testMenuList.append(num) |
| 109 | |
| 110 | self.menuBar.addmenu(name, 'This is ' + name) |
| 111 | |
| 112 | def delete(self): |
| 113 | if len(self.testMenuList) == 0: |
| 114 | self.menuBar.bell() |
| 115 | else: |
| 116 | num = self.testMenuList[0] |
| 117 | name = 'Menu%d' % num |
| 118 | del self.testMenuList[0] |
| 119 | self.menuBar.deletemenu(name) |
| 120 | |
| 121 | def additem(self): |
| 122 | if len(self.testMenuList) == 0: |
| 123 | self.menuBar.bell() |
| 124 | else: |
| 125 | num = self.testMenuList[-1] |
| 126 | menuName = 'Menu%d' % num |
| 127 | menu = self.menuBar.component(menuName + '-menu') |
| 128 | if menu.index('end') is None: |
| 129 | label = 'item X' |
| 130 | else: |
| 131 | label = menu.entrycget('end', 'label') + 'X' |
| 132 | self.menuBar.addmenuitem(menuName, 'command', 'Help for ' + label, |
| 133 | command = PrintOne('Action: ' + menuName + ': ' + label), |
| 134 | label = label) |
| 135 | |
| 136 | def deleteitem(self): |
| 137 | if len(self.testMenuList) == 0: |
| 138 | self.menuBar.bell() |
| 139 | else: |
| 140 | num = self.testMenuList[-1] |
| 141 | menuName = 'Menu%d' % num |
| 142 | menu = self.menuBar.component(menuName + '-menu') |
| 143 | if menu.index('end') is None: |
| 144 | self.menuBar.bell() |
| 145 | else: |
| 146 | self.menuBar.deletemenuitems(menuName, 0) |
| 147 | |
| 148 | class PrintOne: |
| 149 | def __init__(self, text): |
| 150 | self.text = text |
| 151 | |
| 152 | def __call__(self): |
| 153 | print self.text |
| 154 | |
| 155 | ###################################################################### |
| 156 | |
| 157 | # Create demo in root window for testing. |
| 158 | if __name__ == '__main__': |
| 159 | root = Tkinter.Tk() |
| 160 | Pmw.initialise(root) |
| 161 | root.title(title) |
| 162 | |
| 163 | exitButton = Tkinter.Button(root, text = 'Exit', command = root.destroy) |
| 164 | exitButton.pack(side = 'bottom') |
| 165 | widget = Demo(root) |
| 166 | root.mainloop() |