Pmw.OptionMenu

Name

Pmw.OptionMenu() - single item selection megawidget

Inherits

Pmw.MegaWidget

Description

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.

Options

Options for this megawidget and its base classes are described below.

command
Specifies a function to call whenever a menu item is selected or the invoke() method is called. The function is called with the currently selected value as its single argument. The default is None.

initialitem
Initialisation option. Specifies the initial selected value. This option is treated in the same way as the index argument of the setitems() method. The default is None.

items
Initialisation option. A sequence of strings containing the initial items to be displayed in the menu component. The default is ().

labelmargin
Initialisation option. If the labelpos option is not None, this specifies the distance between the label component and the rest of the megawidget. The default is 0.

labelpos
Initialisation option. Specifies where to place the label component. If not None, it should be a concatenation of one or two of the letters 'n', 's', 'e' and 'w'. The first letter specifies on which side of the megawidget to place the label. If a second letter is specified, it indicates where on that side to place the label. For example, if labelpos is 'w', the label is placed in the center of the left hand side; if it is 'wn', the label is placed at the top of the left hand side; if it is 'ws', the label is placed at the bottom of the left hand side.

If None, a label component is not created. The default is None.

sticky
Initialisation option. A combination of 'n', 's', 'e' and 'w' which is used to position the menubutton component within the hull component. The default is 'ew'.

Components

Components created by this megawidget and its base classes are described below.

hull
This acts as the body for the entire megawidget. Other components are created as children of the hull to further specialise this class. By default, this component is a tkinter.Frame.

label
If the labelpos option is not None, this component is created as a text label for the megawidget. See the labelpos option for details. Note that to set, for example, the text option of the label, you need to use the label_text component option. By default, this component is a tkinter.Label.

menu
The popup menu displayed when the menubutton is pressed. By default, this component is a tkinter.Menu.

menubutton
The menu button displaying the currently selected value. By default, this component is a tkinter.Menubutton.

Methods

Only methods specific to this megawidget are described below. For a description of its inherited methods, see the manual for its base class Pmw.MegaWidget.

getcurselection()
Same as getvalue() method.

getvalue()
Return the currently selected value.

index(index)
Return the numerical index of the menu item corresponding to index. This may be specified in any of the following forms:

name
Specifies the menu item labelled name.

number
Specifies the menu item numerically, where 0 corresponds to the first menu item.

Pmw.END
Specifies the last menu item.

Pmw.SELECT
Specifies the currently selected menu item.

invoke(index = Pmw.SELECT)
Calling this method is the same as selecting the menu item specified by index: the text displayed by the menubutton component is updated and the function specified by the command option is called. index may have any of the forms accepted by the index() method. The value returned by command is returned.

setitems(items, index = None)
Replace all the items in the menu component with those specified by items, which must be a sequence of strings.

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.

setvalue(text)
Set the text displayed by the menubutton component to text.

Example

The image at the top of this manual is a snapshot of the window (or part of the window) produced by the following code.

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