menu
Guide: Menus
Makes a menu: a list of entries that run commands, set variables, or open other menus.
menu PATHNAME ?OPTION VALUE ...?
Description
Makes a menu widget with the name PATHNAME and the options of the call, and makes the command PATHNAME for the widget commands below. A menu holds entries, which add and insert make. A menu shows as the menu bar of a toplevel (its -menu), as the menu of a menubutton, as a cascade of another menu, or as a popup menu of post or tk_popup.
Each entry has a type:
command: runs its-commandscript.checkbutton: changes a variable between-onvalueand-offvalue.radiobutton: sets a variable to its-value.cascade: opens the menu of its-menu.separator: draws a line between groups of entries.tearoff: the entry at index 0 of a menu with-tearoff 1.
Many widget commands take an index of an entry. An index is one of these forms:
- A number from 0. A number past the end is the last entry.
endorlast: the last entry.active: the active entry, ornone.none: no entry.@Y: the entry at the pixel row Y of the menu.- A pattern: the first entry whose label matches the pattern, by the rules of
string match.
The parent of PATHNAME must exist, and PATHNAME must not. A menu is a Menu in CSS and for the option database. On macOS and Windows, the menu bar and the popup menus are the menus of the system. On Wayland, a posted menu is a popup surface. On X11 and the web, ztk renders the menus inside the window.
Widget commands
| Command | Description |
|---|---|
menu activate | Makes the entry at INDEX the active entry. |
menu add | Adds an entry of TYPE at the end of the menu. |
menu clone | Makes a copy of the menu with its entries. |
menu delete | Deletes the entries from FIRST to LAST. |
menu entrycget | Gives the value of an option of the entry at INDEX. |
menu entryconfigure | Gives or sets the options of the entry at INDEX. |
menu index | Gives the number of the entry that INDEX names. |
menu insert | Inserts an entry of TYPE before the entry at INDEX. |
menu invoke | Runs the entry at INDEX, as a click does. |
menu post | Shows the menu at a point of the screen. |
menu postcascade | Shows the menu of the cascade entry at INDEX. |
menu type | Gives the type of the entry at INDEX. |
menu unpost | Hides the menu that post showed. |
menu xposition | Gives the x coordinate of the entry at INDEX. |
menu yposition | Gives the y coordinate of the entry at INDEX. |
menu cget | Gives the value of an option of the menu. |
menu configure | Gives or sets the options of the menu. |
Options
| Option | Type | Default | Database name | Description |
|---|---|---|---|---|
-activebackground | color | #ececec | activeBackground | The background color of the active entry. |
-activeborderwidth | screen distance | 1 | activeBorderWidth | The width of the border of the active entry. |
-activeforeground | color | #000000 | activeForeground | The text color of the active entry. |
-background | color | #d9d9d9 | background | The background color of the menu. |
-borderwidth | screen distance | 1 | borderWidth | The width of the border of the menu. |
-cursor | cursor name | arrow | cursor | The shape of the pointer over the menu. |
-disabledforeground | color | #a3a3a3 | disabledForeground | The color of the label of an entry while its state is disabled. |
-font | font | TkMenuFont | font | The font of the labels and the accelerators of the entries. |
-foreground | color | #000000 | foreground | The color of the labels of the entries. |
-postcommand | script | empty | postCommand | A script that runs before the menu shows. |
-relief | relief | raised | relief | The 3-D look of the border. |
-selectcolor | color | #000000 | selectColor | The color of the indicator of a selected checkbutton or radiobutton entry. |
-takefocus | string | 0 | takeFocus | Whether the Tab key can move the focus to the menu. |
-tearoff | boolean | 1 | tearOff | Whether the menu has a tearoff entry at index 0. |
-tearoffcommand | script | empty | tearOffCommand | A command prefix that runs when the user tears off the menu. |
-title | string | empty | title | The title of the window of a torn-off menu. |
-type | type | normal | type | The kind of the menu: normal, tearoff, or menubar. |
-activebackground COLOR
The background color of the active entry.
The entry under the pointer, or the entry of activate, has this background. A color is a name such as red, or #RGB, #RRGGBB, or #RRRRGGGGBBBB.
% menu .activebackground -activebackground {#336699}
> .activebackground cget -activebackground
#336699% menu .activebackground2 -activebackground nocolor
unknown color name "nocolor"
-errorcode TK LOOKUP COLOR nocolor-activeborderwidth DISTANCE
The width of the border of the active entry.
In Tk, the active entry has a 3-D border of this width. ztk stores the value with no effect.
% menu .activeborderwidth -activeborderwidth 4
> .activeborderwidth cget -activeborderwidth
4% menu .activeborderwidth2 -activeborderwidth x
bad screen distance "x"
-errorcode TK VALUE PIXELS-activeforeground COLOR
The text color of the active entry.
The color of the text while the pointer is over the menu or its state is active.
% menu .activeforeground -activeforeground {#336699}
> .activeforeground cget -activeforeground
#336699% menu .activeforeground2 -activeforeground nocolor
unknown color name "nocolor"
-errorcode TK LOOKUP COLOR nocolor-background COLOR
The background color of the menu.
The color behind the content. -bg is the same option. A color is a name such as red, or #RGB, #RRGGBB, or #RRRRGGGGBBBB.
% menu .background -background {#336699}
> .background cget -background
#336699% menu .background2 -background nocolor
unknown color name "nocolor"
-errorcode TK LOOKUP COLOR nocolor-borderwidth DISTANCE
The width of the border of the menu.
The width of the border around the menu. -bd is the same option. A distance is a count of pixels, or a number with c, i, m, or p for centimeters, inches, millimeters, or points.
% menu .borderwidth -borderwidth 4
> .borderwidth cget -borderwidth
4% menu .borderwidth2 -borderwidth x
bad screen distance "x"
-errorcode TK VALUE PIXELS-cursor CURSOR
The shape of the pointer over the menu.
Each value side by side: the contact sheet of the cursors
A cursor name of Tk, such as hand2 or xterm. A value that starts with @ names a file. An empty value uses the cursor of the parent.
% menu .cursor -cursor hand2
> .cursor cget -cursor
hand2% menu .cursor2 -cursor nocursor
bad cursor spec "nocursor"
-errorcode TK VALUE CURSOR-disabledforeground COLOR
The color of the label of an entry while its state is disabled.
The color of the text of an entry with -state disabled.
% menu .disabledforeground -disabledforeground {#336699}
> .disabledforeground cget -disabledforeground
#336699% menu .disabledforeground2 -disabledforeground nocolor
unknown color name "nocolor"
-errorcode TK LOOKUP COLOR nocolor-font FONT
The font of the labels and the accelerators of the entries.
A font name, such as TkDefaultFont, or a font description, such as {Helvetica 12 bold}. A family that does not exist falls back to a font of the system.
% menu .font -font {Helvetica 12 bold}
> .font cget -font
Helvetica 12 bold% menu .font2 -font {Helvetica big}
expected integer but got "big"
-errorcode TCL VALUE NUMBER-foreground COLOR
The color of the labels of the entries.
The color of the text. -fg is the same option. A color is a name such as red, or #RGB, #RRGGBB, or #RRRRGGGGBBBB.
% menu .foreground -foreground {#336699}
> .foreground cget -foreground
#336699% menu .foreground2 -foreground nocolor
unknown color name "nocolor"
-errorcode TK LOOKUP COLOR nocolor-postcommand SCRIPT
A script that runs before the menu shows.
post, a click on a menubutton, and a cascade run the script before the menu shows, so the script can change the entries.
% menu .postcommand -postcommand {set ::opened 1}
> .postcommand post 0 0
> set opened
1ztk stores each script with no check.
-relief RELIEF
The 3-D look of the border.
Each value side by side: the contact sheet of the reliefs
The style of the border: raised and sunken look 3-D, and groove and ridge are lines. The theme draws the border, and the CSS of the theme can give a relief its own look with border-style.
Values: flat, groove, raised, ridge, solid, sunken.
% menu .relief -relief groove
> .relief cget -relief
groove% menu .relief2 -relief bumpy
bad relief "bumpy": must be flat, groove, raised, ridge, solid, or sunken
-errorcode TCL LOOKUP INDEX relief bumpy-selectcolor COLOR
The color of the indicator of a selected checkbutton or radiobutton entry.
An entry can replace the color with its own -selectcolor.
% menu .selectcolor -selectcolor {#336699}
> .selectcolor cget -selectcolor
#336699% menu .selectcolor2 -selectcolor nocolor
unknown color name "nocolor"
-errorcode TK LOOKUP COLOR nocolor-takefocus TEXT
Whether the Tab key can move the focus to the menu.
1 puts the menu in the order of the Tab key, and 0 leaves it out. An empty value uses the default rule of Tk for the widget. In Tk the value can also be a script, and its result is the answer. ztk stores each value with no check.
% menu .takefocus -takefocus 1
> .takefocus cget -takefocus
1ztk stores each value of the option with no check.
-tearoff BOOLEAN
Whether the menu has a tearoff entry at index 0.
With a true value, the first entry of the menu is a tearoff entry, and the entries of add start at index 1. ztk draws a tearoff entry as a line, and a click on it does not tear off the menu.
# With no tearoff entry, the first entry has the index 0.
% menu .tearoff -tearoff 0
> .tearoff add command -label Open
> .tearoff index Open
0% menu .tearoff2 -tearoff maybe
expected boolean value but got "maybe"
-errorcode TCL VALUE NUMBER-tearoffcommand SCRIPT
A command prefix that runs when the user tears off the menu.
In Tk, the prefix runs with the names of the menu and of its new toplevel. ztk does not tear off a menu, so it stores the value with no effect.
% menu .tearoffcommand -tearoffcommand {set ::done 1}
> .tearoffcommand cget -tearoffcommand
set ::done 1ztk stores each prefix with no check.
-title TEXT
The title of the window of a torn-off menu.
In Tk, the toplevel of a torn-off menu has this title. An empty value uses the label of the menubutton or the cascade.
% menu .title -title Tools
> .title cget -title
ToolsEach string is a valid title.
-type WORD
The kind of the menu: normal, tearoff, or menubar.
menubar is the type of a menu of the -menu of a toplevel, tearoff the type of a torn-off menu, and normal the type of the other menus. clone sets the type of a copy.
Values: normal, tearoff, menubar.
% menu .type -type tearoff
> .type cget -type
tearoff% menu .type2 -type bogus
bad type "bogus": must be normal, tearoff, or menubar
-errorcode TCL LOOKUP INDEX type bogusReturns
PATHNAME.
Examples
% menu .file -tearoff 0
> .file add command -label Open -command {set ::action open}
> .file add separator
> .file add command -label Quit
> .file invoke Open
> set action
open
% menu .menubar
> menu .menubar.edit -tearoff 0
> .menubar add cascade -label Edit -menu .menubar.edit
> . configure -menu .menubar
> .menubar type end
cascade
% winfo class [menu .go]
Menu
Errors
% menu .x -foo 1
unknown option "-foo"
-errorcode TK LOOKUP OPTION -foo
% menu .twice
> menu .twice
window name "twice" already exists in parent
-errorcode TK VALUE WINDOW EXISTS
# The parent .no does not exist.
% menu .no.b
bad window path name ".no"
-errorcode TK LOOKUP WINDOW .no
% menu
wrong # args: should be "menu pathName ?-option value ...?"
-errorcode TCL WRONGARGS
Differences from Tcl
- A tearoff entry shows as a line. A click on it does not tear off the menu, so
-tearoffcommandhas no effect. - A tearoff entry has the one option
-background. In Tk 8.6.16 it also has-state. - The entry options
-image,-bitmap,-compound,-hidemargin, and-columnbreakare stored and have no effect on a menu that ztk renders.-activeborderwidthis stored with no effect. - An error of a widget command, such as a bad index or a wrong count of arguments, has the
-errorcodeNONE. Tk 8.6.16 gives a code such asTK MENU INDEXorTCL WRONGARGS. - The error of a bad type of
cloneisbad type "NAME". Tk 8.6.16 givesbad menu type "NAME".
Facts
- Capability
- None.
- Tcl release
- Tcl 9.1 and 8.6.
- Since
- ztk 1.0.0-beta.1