| 1 | title = 'Pmw.OptionMenu 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 and pack the OptionMenu megawidgets. |
| 13 | # The first one has a textvariable. |
| 14 | self.var = Tkinter.StringVar() |
| 15 | self.var.set('steamed') |
| 16 | self.method_menu = Pmw.OptionMenu(parent, |
| 17 | labelpos = 'w', |
| 18 | label_text = 'Choose method:', |
| 19 | menubutton_textvariable = self.var, |
| 20 | items = ['baked', 'steamed', 'stir fried', 'boiled', 'raw'], |
| 21 | menubutton_width = 10, |
| 22 | ) |
| 23 | self.method_menu.pack(anchor = 'w', padx = 10, pady = 10) |
| 24 | |
| 25 | self.vege_menu = Pmw.OptionMenu (parent, |
| 26 | labelpos = 'w', |
| 27 | label_text = 'Choose vegetable:', |
| 28 | items = ('broccoli', 'peas', 'carrots', 'pumpkin'), |
| 29 | menubutton_width = 10, |
| 30 | command = self._printOrder, |
| 31 | ) |
| 32 | self.vege_menu.pack(anchor = 'w', padx = 10, pady = 10) |
| 33 | |
| 34 | self.direction_menu = Pmw.OptionMenu (parent, |
| 35 | labelpos = 'w', |
| 36 | label_text = 'Menu direction:', |
| 37 | items = ('flush', 'above', 'below', 'left', 'right'), |
| 38 | menubutton_width = 10, |
| 39 | command = self._changeDirection, |
| 40 | ) |
| 41 | self.direction_menu.pack(anchor = 'w', padx = 10, pady = 10) |
| 42 | |
| 43 | menus = (self.method_menu, self.vege_menu, self.direction_menu) |
| 44 | Pmw.alignlabels(menus) |
| 45 | |
| 46 | def _printOrder(self, vege): |
| 47 | # Can use 'self.var.get()' instead of 'getcurselection()'. |
| 48 | print 'You have chosen %s %s.' % \ |
| 49 | (self.method_menu.getcurselection(), vege) |
| 50 | |
| 51 | def _changeDirection(self, direction): |
| 52 | for menu in (self.method_menu, self.vege_menu, self.direction_menu): |
| 53 | menu.configure(menubutton_direction = direction) |
| 54 | |
| 55 | ###################################################################### |
| 56 | |
| 57 | # Create demo in root window for testing. |
| 58 | if __name__ == '__main__': |
| 59 | root = Tkinter.Tk() |
| 60 | Pmw.initialise(root) |
| 61 | root.title(title) |
| 62 | |
| 63 | exitButton = Tkinter.Button(root, text = 'Exit', command = root.destroy) |
| 64 | exitButton.pack(side = 'bottom') |
| 65 | widget = Demo(root) |
| 66 | root.mainloop() |