beetlebug▾
/

menu

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:

Many widget commands take an index of an entry. An index is one of these forms:

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

CommandDescription
menu activateMakes the entry at INDEX the active entry.
menu addAdds an entry of TYPE at the end of the menu.
menu cloneMakes a copy of the menu with its entries.
menu deleteDeletes the entries from FIRST to LAST.
menu entrycgetGives the value of an option of the entry at INDEX.
menu entryconfigureGives or sets the options of the entry at INDEX.
menu indexGives the number of the entry that INDEX names.
menu insertInserts an entry of TYPE before the entry at INDEX.
menu invokeRuns the entry at INDEX, as a click does.
menu postShows the menu at a point of the screen.
menu postcascadeShows the menu of the cascade entry at INDEX.
menu typeGives the type of the entry at INDEX.
menu unpostHides the menu that post showed.
menu xpositionGives the x coordinate of the entry at INDEX.
menu ypositionGives the y coordinate of the entry at INDEX.
menu cgetGives the value of an option of the menu.
menu configureGives or sets the options of the menu.

Options

OptionTypeDefaultDatabase nameDescription
-activebackgroundcolor#ecececactiveBackgroundThe background color of the active entry.
-activeborderwidthscreen distance1activeBorderWidthThe width of the border of the active entry.
-activeforegroundcolor#000000activeForegroundThe text color of the active entry.
-backgroundcolor#d9d9d9backgroundThe background color of the menu.
-borderwidthscreen distance1borderWidthThe width of the border of the menu.
-cursorcursor namearrowcursorThe shape of the pointer over the menu.
-disabledforegroundcolor#a3a3a3disabledForegroundThe color of the label of an entry while its state is disabled.
-fontfontTkMenuFontfontThe font of the labels and the accelerators of the entries.
-foregroundcolor#000000foregroundThe color of the labels of the entries.
-postcommandscriptemptypostCommandA script that runs before the menu shows.
-reliefreliefraisedreliefThe 3-D look of the border.
-selectcolorcolor#000000selectColorThe color of the indicator of a selected checkbutton or radiobutton entry.
-takefocusstring0takeFocusWhether the Tab key can move the focus to the menu.
-tearoffboolean1tearOffWhether the menu has a tearoff entry at index 0.
-tearoffcommandscriptemptytearOffCommandA command prefix that runs when the user tears off the menu.
-titlestringemptytitleThe title of the window of a torn-off menu.
-typetypenormaltypeThe 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.

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
1

ztk stores each script with no check.

-relief RELIEF

The 3-D look of the border.

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
1

ztk 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 1

ztk 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
Tools

Each 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 bogus

Returns

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

Facts

Capability
None.
Tcl release
Tcl 9.1 and 8.6.
Since
ztk 1.0.0-beta.1

See also