menu
-
NAME
- menu, tk_menuSetFocus — Create and manipulate 'menu' widgets and menubars
- SYNOPSIS
- STANDARD OPTIONS
-
-activebackground, activeBackground, Foreground
- -activeborderwidth, activeBorderWidth, BorderWidth
- -activeforeground, activeForeground, Background
- -background or -bg, background, Background
- -borderwidth or -bd, borderWidth, BorderWidth
- -cursor, cursor, Cursor
- -disabledforeground, disabledForeground, DisabledForeground
- -font, font, Font
- -foreground or -fg, foreground, Foreground
- -relief, relief, Relief
- -takefocus, takeFocus, TakeFocus
- -activeborderwidth, activeBorderWidth, BorderWidth
- WIDGET-SPECIFIC OPTIONS
-
-postcommand, postCommand, Command
- -selectcolor, selectColor, Background
- -tearoff, tearOff, TearOff
- -tearoffcommand, tearOffCommand, TearOffCommand
- -title, title, Title
- -type, type, Type
- -selectcolor, selectColor, Background
- INTRODUCTION
- TYPES OF ENTRIES
- COMMAND ENTRIES
- SEPARATOR ENTRIES
- CHECKBUTTON ENTRIES
- RADIOBUTTON ENTRIES
- CASCADE ENTRIES
- TEAR-OFF ENTRIES
- MENUBARS
- SPECIAL MENUS IN MENUBARS
- CLONES
- WIDGET COMMAND
- MENU ENTRY OPTIONS
- MENU CONFIGURATIONS
- DEFAULT BINDINGS
- BUGS
- SEE ALSO
- KEYWORDS
Name
menu, tk_menuSetFocus — Create and manipulate 'menu' widgets and menubarsSynopsis
menu pathName ?options?tk_menuSetFocus pathName
Standard options
-
-activebackground, activeBackground, Foreground
- -activeborderwidth, activeBorderWidth, BorderWidth
- -activeforeground, activeForeground, Background
- -background or -bg, background, Background
- -borderwidth or -bd, borderWidth, BorderWidth
- -cursor, cursor, Cursor
- -disabledforeground, disabledForeground, DisabledForeground
- -font, font, Font
- -foreground or -fg, foreground, Foreground
- -relief, relief, Relief
- -takefocus, takeFocus, TakeFocus
- -activeborderwidth, activeBorderWidth, BorderWidth
Widget-specific options
- Command-Line Name: -postcommand
- Database Name: postCommand
- Database Class: Command
- Database Name: postCommand
- If this option is specified then it provides a Tcl command to execute each time the menu is posted. The command is invoked by the post widget command before posting the menu. Note that in Tk 8.0 on Macintosh and Windows, all post-commands in a system of menus are executed before any of those menus are posted. This is due to the limitations in the individual platforms' menu managers.
- Command-Line Name: -selectcolor
- Database Name: selectColor
- Database Class: Background
- Database Name: selectColor
- For menu entries that are check buttons or radio buttons, this option specifies the color to display in the indicator when the check button or radio button is selected.
- Command-Line Name: -tearoff
- Database Name: tearOff
- Database Class: TearOff
- Database Name: tearOff
- This option must have a proper boolean value, which specifies whether or not the menu should include a tear-off entry at the top. If so, it will exist as entry 0 of the menu and the other entries will number starting at 1. The default menu bindings arrange for the menu to be torn off when the tear-off entry is invoked. This option is ignored under Aqua/Mac OS X, where menus cannot be torn off.
- Command-Line Name: -tearoffcommand
- Database Name: tearOffCommand
- Database Class: TearOffCommand
- Database Name: tearOffCommand
- If this option has a non-empty value, then it specifies a Tcl command to invoke whenever the menu is torn off. The actual command will consist of the value of this option, followed by a space, followed by the name of the menu window, followed by a space, followed by the name of the name of the torn off menu window. For example, if the option's value is “a b” and menu .x.y is torn off to create a new menu .x.tearoff1, then the command “a b .x.y .x.tearoff1” will be invoked. This option is ignored under Aqua/Mac OS X, where menus cannot be torn off.
- Command-Line Name: -title
- Database Name: title
- Database Class: Title
- Database Name: title
- The string will be used to title the window created when this menu is torn off. If the title is NULL, then the window will have the title of the menubutton or the text of the cascade item from which this menu was invoked.
- Command-Line Name: -type
- Database Name: type
- Database Class: Type
- Database Name: type
- This option can be one of menubar, tearoff, or normal, and is set when the menu is created. While the string returned by the configuration database will change if this option is changed, this does not affect the menu widget's behavior. This is used by the cloning mechanism and is not normally set outside of the Tk library.
Introduction
The menu command creates a new top-level window (given by the pathName argument) and makes it into a menu widget. That menu widget can either be used as a pop-up window or applied to a toplevel (with its -menu option) to make it into the menubar for that toplevel. Additional options, described above, may be specified on the command line or in the option database to configure aspects of the menu such as its colors and font. The menu command returns its pathName argument. At the time this command is invoked, there must not exist a window named pathName, but pathName's parent must exist.A menu is a widget that displays a collection of one-line entries arranged in one or more columns. There exist several different types of entries, each with different properties. Entries of different types may be combined in a single menu. Menu entries are not the same as entry widgets. In fact, menu entries are not even distinct widgets; the entire menu is one widget.
Menu entries are displayed with up to three separate fields. The main field is a label in the form of a text string, a bitmap, or an image, controlled by the -label, -bitmap, and -image options for the entry. If the -accelerator option is specified for an entry then a second textual field is displayed to the right of the label. The accelerator typically describes a keystroke sequence that may be typed in the application to cause the same result as invoking the menu entry. The third field is an indicator. The indicator is present only for checkbutton or radiobutton entries. It indicates whether the entry is selected or not, and is displayed to the left of the entry's string.
In normal use, an entry becomes active (displays itself differently) whenever the mouse pointer is over the entry. If a mouse button is released over the entry then the entry is invoked. The effect of invocation is different for each type of entry; these effects are described below in the sections on individual entries.
Entries may be disabled, which causes their labels and accelerators to be displayed with dimmer colors. The default menu bindings will not allow a disabled entry to be activated or invoked. Disabled entries may be re-enabled, at which point it becomes possible to activate and invoke them again.
Whenever a menu's active entry is changed, a <<MenuSelect>> virtual event is send to the menu. The active item can then be queried from the menu, and an action can be taken, such as setting context-sensitive help text for the entry.
Types of entries
Command entries
The most common kind of menu entry is a command entry, which behaves much like a button widget. When a command entry is invoked, a Tcl command is executed. The Tcl command is specified with the -command option.Separator entries
A separator is an entry that is displayed as a horizontal dividing line. A separator may not be activated or invoked, and it has no behavior other than its display appearance.Checkbutton entries
A checkbutton menu entry behaves much like a checkbutton widget. When it is invoked it toggles back and forth between the selected and deselected states. When the entry is selected, a particular value is stored in a particular global variable (as determined by the -onvalue and -variable options for the entry); when the entry is deselected another value (determined by the -offvalue option) is stored in the global variable. An indicator box is displayed to the left of the label in a checkbutton entry. If the entry is selected then the indicator's center is displayed in the color given by the -selectcolor option for the entry; otherwise the indicator's center is displayed in the background color for the menu. If a -command option is specified for a checkbutton entry, then its value is evaluated as a Tcl command each time the entry is invoked; this happens after toggling the entry's selected state.Radiobutton entries
A radiobutton menu entry behaves much like a radiobutton widget. Radiobutton entries are organized in groups of which only one entry may be selected at a time. Whenever a particular entry becomes selected it stores a particular value into a particular global variable (as determined by the -value and -variable options for the entry). This action causes any previously-selected entry in the same group to deselect itself. Once an entry has become selected, any change to the entry's associated variable will cause the entry to deselect itself. Grouping of radiobutton entries is determined by their associated variables: if two entries have the same associated variable then they are in the same group. An indicator diamond is displayed to the left of the label in each radiobutton entry. If the entry is selected then the indicator's center is displayed in the color given by the -selectcolor option for the entry; otherwise the indicator's center is displayed in the background color for the menu. If a -command option is specified for a radiobutton entry, then its value is evaluated as a Tcl command each time the entry is invoked; this happens after selecting the entry.Cascade entries
A cascade entry is one with an associated menu (determined by the -menu option). Cascade entries allow the construction of cascading menus. The postcascade widget command can be used to post and unpost the associated menu just next to of the cascade entry. The associated menu must be a child of the menu containing the cascade entry (this is needed in order for menu traversal to work correctly).A cascade entry posts its associated menu by invoking a Tcl command of the form
menu post x ywhere menu is the path name of the associated menu, and x and y are the root-window coordinates of the upper-right corner of the cascade entry. On Unix, the lower-level menu is unposted by executing a Tcl command with the form
menu unpostwhere menu is the name of the associated menu. On other platforms, the platform's native code takes care of unposting the menu.
If a -command option is specified for a cascade entry then it is evaluated as a Tcl command whenever the entry is invoked. This is not supported on Windows.
Tear-off entries
A tear-off entry appears at the top of the menu if enabled with the -tearoff option. It is not like other menu entries in that it cannot be created with the add widget command and cannot be deleted with the delete widget command. When a tear-off entry is created it appears as a dashed line at the top of the menu. Under the default bindings, invoking the tear-off entry causes a torn-off copy to be made of the menu and all of its submenus.Menubars
Any menu can be set as a menubar for a toplevel window (see toplevel command for syntax). On the Macintosh, whenever the toplevel is in front, this menu's cascade items will appear in the menubar across the top of the main monitor. On Windows and Unix, this menu's items will be displayed in a menubar across the top of the window. These menus will behave according to the interface guidelines of their platforms. For every menu set as a menubar, a clone menu is made. See the CLONES section for more information.As noted, menubars may behave differently on different platforms. One example of this concerns the handling of checkbuttons and radiobuttons within the menu. While it is permitted to put these menu elements on menubars, they may not be drawn with indicators on some platforms, due to system restrictions.
Special menus in menubars
Certain menus in a menubar will be treated specially. On the Macintosh, access to the special Application, Window and Help menus is provided. On Windows, access to the Windows System menu in each window is provided. On X Windows, a special right-justified help menu may be provided if Motif menu compatibility is enabled. In all cases, these menus must be created with the command name of the menubar menu concatenated with the special name. So for a menubar named .menubar, on the Macintosh, the special menus would be .menubar.apple, .menubar.window and .menubar.help; on Windows, the special menu would be .menubar.system; on X Windows, the help menu would be .menubar.help.When Tk sees a .menubar.apple menu as the first menu in a menubar on the Macintosh, that menu's contents make up the first items of the Application menu whenever the window containing the menubar is in front. After all of the Tk-defined items, the menu will have a separator, followed by all standard Application menu items. Such a .apple menu must be present in a menu when that menu is first configured as a toplevel's menubar, otherwise a default application menu (hidden from Tk) will be inserted into the menubar at that time and subsequent addition of a .apple menu will no longer result in it becoming the Application menu.
When Tk sees a .menubar.window menu on the Macintosh, the menu's contents are inserted into the standard Window menu of the user's menubar whenever the window's menubar is in front. The first items in the menu are provided by Mac OS X, and the names of the current toplevels are automatically appended after all the Tk-defined items and a separator.
When Tk sees a .menubar.help menu on the Macintosh, the menu's contents are appended to the standard Help menu of the user's menubar whenever the window's menubar is in front. The first items in the menu are provided by Mac OS X.
When Tk sees a System menu on Windows, its items are appended to the system menu that the menubar is attached to. This menu is tied to the application icon and can be invoked with the mouse or by typing Alt+Spacebar. Due to limitations in the Windows API, any font changes, colors, images, bitmaps, or tearoff images will not appear in the system menu.
When Tk sees a Help menu on X Windows and Motif menu compatibility is enabled the menu is moved to be last in the menubar and is right justified. Motif menu compatibility is enabled by setting the Tk option *Menu.useMotifHelp to true or by calling tk::classic::restore menu.
Clones
When a menu is set as a menubar for a toplevel window, or when a menu is torn off, a clone of the menu is made. This clone is a menu widget in its own right, but it is a child of the original. Changes in the configuration of the original are reflected in the clone. Additionally, any cascades that are pointed to are also cloned so that menu traversal will work right. Clones are destroyed when either the tearoff or menubar goes away, or when the original menu is destroyed.Widget command
The menu command creates a new Tcl command whose name is pathName. This command may be used to invoke various operations on the widget. It has the following general form:pathName option ?arg arg ...?Option and the args determine the exact behavior of the command.
Many of the widget commands for a menu take as one argument an indicator of which entry of the menu to operate on. These indicators are called indexes and may be specified in any of the following forms:
If the index could match more than one of the above forms, then the form earlier in the above list takes precedence.
The following widget commands are possible for menu widgets:
Menu entry options
The following options are allowed on menu entries. Most options are not supported by all entry types.Menu configurations
The default bindings support four different ways of using menus:Default bindings
Tk automatically creates class bindings for menus that give them the following default behavior:- When the mouse enters a menu, the entry underneath the mouse cursor activates; as the mouse moves around the menu, the active entry changes to track the mouse.
- When the mouse leaves a menu all of the entries in the menu deactivate, except in the special case where the mouse moves from a menu to a cascaded submenu.
- When a button is released over a menu, the active entry (if any) is invoked. The menu also unposts unless it is a torn-off menu.
- The Space and Return keys invoke the active entry and unpost the menu.
- If any of the entries in a menu have letters underlined with the -underline option, then pressing one of the underlined letters (or its upper-case or lower-case equivalent) invokes that entry and unposts the menu.
- The Escape key aborts a menu selection in progress without invoking any entry. It also unposts the menu unless it is a torn-off menu.
- The Up and Down keys activate the next higher or lower entry in the menu. When one end of the menu is reached, the active entry wraps around to the other end.
- The Left key moves to the next menu to the left. If the current menu is a cascaded submenu, then the submenu is unposted and the current menu entry becomes the cascade entry in the parent. If the current menu is a top-level menu posted from a menubutton, then the current menubutton is unposted and the next menubutton to the left is posted. Otherwise the key has no effect. The left-right order of menubuttons is determined by their stacking order: Tk assumes that the lowest menubutton (which by default is the first one created) is on the left.
- The Right key moves to the next menu to the right. If the current entry is a cascade entry, then the submenu is posted and the current menu entry becomes the first entry in the submenu. Otherwise, if the current menu was posted from a menubutton, then the current menubutton is unposted and the next menubutton to the right is posted.
Disabled menu entries are non-responsive: they do not activate and they ignore mouse button presses and releases.
Several of the bindings make use of the command tk_menuSetFocus. It saves the current focus and sets the focus to its pathName argument, which is a menu widget.
The behavior of menus can be changed by defining new bindings for individual widgets or by redefining the class bindings.
Bugs
At present it is not possible to use the option database to specify values for the options to individual entries.See also
bind, menubutton, ttk::menubutton, toplevelCopyright © 1990-1994 The Regents of the University of California.
Copyright © 1994-1997 Sun Microsystems, Inc.
Licensed under Tcl/Tk terms
https://www.tcl.tk/man/tcl/TkCmd/menu.htm