Pmw.OptionMenu() - single item selection megawidget
An option menu consists of a menu button and an associated menu which pops up when the button is pressed. The text displayed in the menu button is updated whenever an item is selected in the menu. The currently selected value can be retrieved from the megawidget.
invoke()
method is called. The function is called with the
currently selected value as its single argument. The default is None.
setitems()
method. The default is None.
If None, a label component is not created. The default is None.
getvalue()
method.
index()
method. The value returned by
command is returned.
If index is not None, set the selected value to index, which
may have any of the forms accepted by the index()
method.
If index is None and the textvariable option of the menubutton component is the empty string, then if the previous selected value is one of the items, then do not change the selection. If the previous selected value is no longer in items, then set the selected value to the first value in items. If items is empty, set the selected value to the empty string.
If index is None and the textvariable option of the menubutton component is not the empty string, then do not set the selected value. This assumes that the variable is already (or will be) set to the desired value.
class Demo: def __init__(self, parent): # Create and pack the OptionMenu megawidgets. # The first one has a textvariable. self.var = tkinter.StringVar() self.var.set('steamed') self.method_menu = Pmw.OptionMenu(parent, labelpos = 'w', label_text = 'Choose method:', menubutton_textvariable = self.var, items = ['baked', 'steamed', 'stir fried', 'boiled', 'raw'], menubutton_width = 10, ) self.method_menu.pack(anchor = 'w', padx = 10, pady = 10) self.vege_menu = Pmw.OptionMenu (parent, labelpos = 'w', label_text = 'Choose vegetable:', items = ('broccoli', 'peas', 'carrots', 'pumpkin'), menubutton_width = 10, command = self._printOrder, ) self.vege_menu.pack(anchor = 'w', padx = 10, pady = 10) self.direction_menu = Pmw.OptionMenu (parent, labelpos = 'w', label_text = 'Menu direction:', items = ('flush', 'above', 'below', 'left', 'right'), menubutton_width = 10, command = self._changeDirection, ) self.direction_menu.pack(anchor = 'w', padx = 10, pady = 10) menus = (self.method_menu, self.vege_menu, self.direction_menu) Pmw.alignlabels(menus) def _printOrder(self, vege): # Can use 'self.var.get()' instead of 'getcurselection()'. print('You have chosen %s %s.' % \ (self.method_menu.getcurselection(), vege)) def _changeDirection(self, direction): for menu in (self.method_menu, self.vege_menu, self.direction_menu): menu.configure(menubutton_direction = direction)
Pmw 2.1 -
31 Dec 2020
- Home
Manual page last reviewed: 23 October 1998