11.4. The Menu Widget

There are times when you won't want to use a menubutton widget. Perhaps you need to create some menus that will cascade from your menubutton. You still need to create the menus. You might also think of a way to use a menu that doesn't involve a button. For example, you could set up your application so the user right-clicks on a widget[5] and a related menu pops up, allowing the user to change configuration options.

[5] Use "<Button-3>" with bind.

It is also a good idea to be familiar with the methods for manipulating a menu, whether it's a menu widget by itself or the menu attached to a menubutton.

11.4.1. Creating the Basic Menu

To create a menu widget, invoke Menu from the desired parent of the menu:

$menu = $parent->Menu(options);

The menu widget is the only widget on which one of the geometry managers is not used directly. The menu is displayed to the user via a post directive:

$menu->post( ... );

Different arguments sent to post will determine how the menu is displayed. This method is discussed later in this chapter.

11.4.2. Menu Creation Options

As with any widget, there are options that affect how the menu widget looks and behaves. Many of the options for the menu widget were discussed in the menubutton widget portion of the chapter, so I'll only cover those that perform actions that aren't available with the menubutton widget or whose actions are different.

The following is a list of the options available for the menu widget:

-activebackground => ...

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.