5.4 Menus, Menuitems, and Menubuttons

Menu widgets serve as containers for menu entries. A menu entry can be one of the following:

separator

displays a horizontal line

command

similar to button widgets

radiobutton

similar to radiobutton widgets

checkbutton

similar to checkbutton widgets

cascade

displays sub menus

Menu entries are not widgets. In particular, menu entries are not managed by a geometry manager. Instead as soon as a menu entry is created it is displayed in its parent menu. To configure a menu entry after it has been created, one needs to use the entryconfigure command rather than the configure command.

tear off entry

The program shown in Figure 5.4 creates two menu widgets M1 and M2. The first cascade entry of the menu widget M1 is configured such that it displays the menu M2 when the menu is traversed. The option tearoff determines that the first default so-called ``tear off'' entry is not created. Selecting a tear off entry displays the menu in a window on its own.


Cs =['Wheat' 'Firebrick' 'Navy' 'Darkorange']
M1 ={New Tk.menu tkInit(parent:W  tearoff:false)}
M2 ={New Tk.menu tkInit(parent:M1 tearoff:false)}
E1 ={New Tk.menuentry.cascade
     tkInit(parent:M1 label:'Background Color' menu:M2)}
E2 ={New Tk.menuentry.separator tkInit(parent:M1)}
E3 ={New Tk.menuentry.command
     tkInit(parent:M1 label:'Quit' action: W#tkClose)}
V  ={New Tk.variable tkInit(Cs.1)}
CEs={Map Cs fun {$ C}
               {New Tk.menuentry.radiobutton
                tkInit(parent:M2 label:C var:V val:C
                       action: W#tk(configure bg:C))}
            end}

Figure 5.4: A menu with entries, including a cascaded sub menu.


posting menus

Usually menus are not visible. Only when needed a menu appears on the screen, we say that it is posted. After the user has traversed the menu and has selected an entry, the menu is made invisible again: it is unposted. Posting the menu M1 at the upper left edge of the screen can be done by

{M1 tk(post 0 0)}

menubuttons

From menus one can compose menu bars and popup menus. A menu bar consists of several menubutton widgets. A menubutton widget can display text, bitmaps, or images. To a menubutton a menu can be attached such that pressing the button makes the menu widget appear on the screen. We do not discuss menu bars here in detail, since the TkTools module provides an abstraction that supports the creation of menu bars (see Section 8.3).

popup menus

The command tk_popup can be used to display popup menus. It takes as arguments the menu widget and the coordinates where the widget should appear on the screen. Ideally, we want the menu widget to appear after pressing the mouse button when the mouse pointer is over some widget. The next section introduces events which allows to mattach actions to abitrary widgets.

Reference information can be found in menu and menubutton.


Christian Schulte
Version 1.4.0 (20080702)