11.5. Optionmenu Widget

The optionmenu is a specific implementation of a menubutton widget. The difference between the two is that the optionmenu automatically sets the -indicatoron option to 1, removes the tear-off menu item, and handles the display of the menu in a slightly different way.

You can use an optionmenu when you want to give the user a choice between several different items but don't want to waste space with a listbox and scrollbar or with several radiobuttons. To add items, use the -options command instead of -menuitems or the other methods that allowed you to add to a menu or menubutton.

11.5.1. Creating and Configuring an Optionmenu

The optionmenu is created by using the Optionmenumethod:

$optionmenu = $mw->Optionmenu( ... );

All the options that are available with a menubutton widget are also available for the optionmenu widget. The following options are specific to the optionmenu: -textvariable, -options, -variable, and -command.

Instead of using a -menuitems option or other methods to add items to an optionmenu, use the -options option. It takes an anonymous list that can contain either strings or other anonymous lists. The idea behind an optionmenu is to select one item from a list of items. The text displayed is the currently selected menu item. The -textvariable option determines where the displayed text is stored. There is also a -variable option, which you ...

Get Learning Perl/Tk now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.