beetlebug▾
/

menubutton

Makes a menubutton: a button that opens a menu.

menubutton PATHNAME ?OPTION VALUE ...?

Description

Makes a menubutton widget with the name PATHNAME and the options of the call, and makes the command PATHNAME for the widget commands below. A click on the menubutton posts the menu of -menu, at the side of -direction. The menubutton shows its -text, the value of its -textvariable, or its -image.

The menu is usually a child of the menubutton, such as .file.menu for the menubutton .file. The parent of PATHNAME must exist, and PATHNAME must not. A menubutton is a Menubutton in CSS and for the option database, and ztk::class gives it a style class for a stylesheet.

The theme draws the border and the padding, so -borderwidth, -padx, and -pady default to 0, and a value of the script overrides the theme.

Widget commands

CommandDescription
menubutton cgetGives the value of an option of the menubutton.
menubutton configureGives or sets the options of the menubutton.

Options

OptionTypeDefaultDatabase nameDescription
-activebackgroundcolor#ecececactiveBackgroundThe background color of the active state, in the look of Tk on X11.
-activeforegroundcolor#000000activeForegroundThe text color of the active state, in the look of Tk on X11.
-anchoranchorcenteranchorThe place of the content in a menubutton larger than it.
-backgroundcolor#d9d9d9backgroundThe background color of the menubutton.
-bitmapbitmap nameemptybitmapA bitmap name of Tk to show in place of the text.
-borderwidthscreen distance0borderWidthThe width of the border of the menubutton.
-cursorcursor nameemptycursorThe shape of the pointer over the menubutton.
-directiondirectionbelowdirectionWhere the menu opens, from the menubutton.
-disabledforegroundcolor#a3a3a3disabledForegroundThe text color while the state is disabled.
-fontfontTkDefaultFontfontThe font of the text.
-foregroundcolor#000000foregroundThe color of the text.
-heightinteger0heightThe height of the menubutton in lines of text, or in pixels for an image.
-highlightbackgroundcolor#d9d9d9highlightBackgroundThe color of the focus ring while the menubutton has no focus, in the look of Tk on X11.
-highlightcolorcolor#000000highlightColorThe color of the focus ring while the menubutton has the focus, in the look of Tk on X11.
-highlightthicknessscreen distance0highlightThicknessThe width of the focus ring, in the look of Tk on X11.
-imageimage nameemptyimageAn image to show in place of the text.
-indicatoronboolean0indicatorOnWhether the menubutton draws an indicator of its menu.
-justifyjustificationcenterjustifyThe alignment of the lines of the text.
-menuwidget path nameemptymenuThe menu that the menubutton opens.
-padxscreen distance0padXThe space at the left and the right of the content.
-padyscreen distance0padYThe space above and below the content.
-reliefreliefflatreliefThe 3-D look of the border.
-compoundcompoundnonecompoundShows the image and the text together, with the image at that side.
-statestatenormalstateThe state of the menubutton: normal, active, or disabled.
-takefocusstring0takeFocusWhether the Tab key can move the focus to the menubutton.
-textstringemptytextThe text of the menubutton.
-textvariablevariable nameemptytextVariableA global variable whose value is the text of the menubutton.
-underlineinteger-1underlineThe index of a character of the text to underline, for a keyboard shortcut.
-widthinteger0widthThe width of the menubutton in characters of the font, or in pixels for an image.
-wraplengthscreen distance0wrapLengthThe longest line of the text before it breaks into a new line.

-activebackground COLOR

The background color of the active state, in the look of Tk on X11.

The background of the menubutton while the pointer is over it or its state is active. A color is a name such as red, or #RGB, #RRGGBB, or #RRRRGGGGBBBB.

menubutton with -text File -state active and -activebackground {#fde68a}-activebackground {#fde68a}menubutton with -text File -state active and -activebackground {#bfdbfe}-activebackground {#bfdbfe}
Each with -text File -state active.
% menubutton .activebackground -activebackground {#336699}
> .activebackground cget -activebackground
#336699
% menubutton .activebackground2 -activebackground nocolor
unknown color name "nocolor"
-errorcode TK LOOKUP COLOR nocolor

-activeforeground COLOR

The text color of the active state, in the look of Tk on X11.

The color of the text while the pointer is over the menubutton or its state is active.

menubutton with -text File -state active and -activeforeground {#b91c1c}-activeforeground {#b91c1c}menubutton with -text File -state active and -activeforeground {#2563eb}-activeforeground {#2563eb}
Each with -text File -state active.
% menubutton .activeforeground -activeforeground {#336699}
> .activeforeground cget -activeforeground
#336699
% menubutton .activeforeground2 -activeforeground nocolor
unknown color name "nocolor"
-errorcode TK LOOKUP COLOR nocolor

-anchor ANCHOR

The place of the content in a menubutton larger than it.

When the menubutton is larger than its text or image, the anchor puts the content at that side or corner. center puts it in the middle.

Values: n, ne, e, se, s, sw, w, nw, center.

menubutton with -text File -width 14 and -anchor w-anchor wmenubutton with -text File -width 14 and -anchor center-anchor centermenubutton with -text File -width 14 and -anchor e-anchor e
Each with -text File -width 14.
% menubutton .anchor -anchor w
> .anchor cget -anchor
w
% menubutton .anchor2 -anchor middle
bad anchor "middle": must be n, ne, e, se, s, sw, w, nw, or center
-errorcode TCL LOOKUP INDEX anchor middle

-background COLOR

The background color of the menubutton.

The color behind the content. -bg is the same option. A color is a name such as red, or #RGB, #RRGGBB, or #RRRRGGGGBBBB.

menubutton with -text File and -background {#fde68a}-background {#fde68a}menubutton with -text File and -background {#bfdbfe}-background {#bfdbfe}
Each with -text File.
% menubutton .background -background {#336699}
> .background cget -background
#336699
% menubutton .background2 -background nocolor
unknown color name "nocolor"
-errorcode TK LOOKUP COLOR nocolor

-bitmap BITMAP

A bitmap name of Tk to show in place of the text.

The name of a built-in bitmap of Tk, such as error, info, or question. A value that starts with @ names a file. ztk stores the value for cget and shows no bitmap.

% menubutton .bitmap -bitmap error
> .bitmap cget -bitmap
error
% menubutton .bitmap2 -bitmap nobitmap
bitmap "nobitmap" not defined
-errorcode TK LOOKUP BITMAP nobitmap

-borderwidth DISTANCE

The width of the border of the menubutton.

The width of the border around the menubutton. -bd is the same option. The default 0 leaves the border to the theme, and a value of the script replaces it. A distance is a count of pixels, or a number with c, i, m, or p for centimeters, inches, millimeters, or points.

menubutton with -text File -relief solid and -borderwidth 0-borderwidth 0menubutton with -text File -relief solid and -borderwidth 1-borderwidth 1menubutton with -text File -relief solid and -borderwidth 4-borderwidth 4
Each with -text File -relief solid.
% menubutton .borderwidth -borderwidth 4
> .borderwidth cget -borderwidth
4
% menubutton .borderwidth2 -borderwidth x
bad screen distance "x"
-errorcode TK VALUE PIXELS

-cursor CURSOR

The shape of the pointer over the menubutton.

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.

% menubutton .cursor -cursor hand2
> .cursor cget -cursor
hand2
% menubutton .cursor2 -cursor nocursor
bad cursor spec "nocursor"
-errorcode TK VALUE CURSOR

-direction WORD

Where the menu opens, from the menubutton.

below opens the menu under the menubutton, above over it, and left and right at that side. flush puts the menu on the menubutton, with its first entry at the place of the menubutton.

Values: above, below, flush, left, right.

% menubutton .direction -direction above
> .direction cget -direction
above
% menubutton .direction2 -direction bogus
bad direction "bogus": must be above, below, flush, left, or right
-errorcode TCL LOOKUP INDEX direction bogus

-disabledforeground COLOR

The text color while the state is disabled.

The color of the text while -state is disabled.

menubutton with -text File -state disabled and -disabledforeground {#888888}-disabledforeground {#888888}menubutton with -text File -state disabled and -disabledforeground {#b91c1c}-disabledforeground {#b91c1c}
Each with -text File -state disabled.
% menubutton .disabledforeground -disabledforeground {#336699}
> .disabledforeground cget -disabledforeground
#336699
% menubutton .disabledforeground2 -disabledforeground nocolor
unknown color name "nocolor"
-errorcode TK LOOKUP COLOR nocolor

-font FONT

The font of the text.

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.

menubutton with -text File and -font TkDefaultFont-font TkDefaultFontmenubutton with -text File and -font TkFixedFont-font TkFixedFontmenubutton with -text File and -font {Helvetica 16 bold}-font {Helvetica 16 bold}
Each with -text File.
% menubutton .font -font {Helvetica 12 bold}
> .font cget -font
Helvetica 12 bold
% menubutton .font2 -font {Helvetica big}
expected integer but got "big"
-errorcode TCL VALUE NUMBER

-foreground COLOR

The color of the text.

The color of the text. -fg is the same option. A color is a name such as red, or #RGB, #RRGGBB, or #RRRRGGGGBBBB.

menubutton with -text File and -foreground {#b91c1c}-foreground {#b91c1c}menubutton with -text File and -foreground {#2563eb}-foreground {#2563eb}
Each with -text File.
% menubutton .foreground -foreground {#336699}
> .foreground cget -foreground
#336699
% menubutton .foreground2 -foreground nocolor
unknown color name "nocolor"
-errorcode TK LOOKUP COLOR nocolor

-height LINES

The height of the menubutton in lines of text, or in pixels for an image.

For a menubutton with text, the height in lines of the font. For a menubutton with an image, the height in pixels. 0 makes the menubutton as tall as its content.

% menubutton .height -height 5
> .height cget -height
5
% menubutton .height2 -height x
expected integer but got "x"
-errorcode TCL VALUE NUMBER

-highlightbackground COLOR

The color of the focus ring while the menubutton has no focus, in the look of Tk on X11.

The color of the highlight ring of -highlightthickness while the menubutton does not have the focus. ztk stores the value with no effect.

% menubutton .highlightbackground -highlightbackground {#336699}
> .highlightbackground cget -highlightbackground
#336699
% menubutton .highlightbackground2 -highlightbackground nocolor
unknown color name "nocolor"
-errorcode TK LOOKUP COLOR nocolor

-highlightcolor COLOR

The color of the focus ring while the menubutton has the focus, in the look of Tk on X11.

The color of the highlight ring of -highlightthickness while the menubutton has the focus. ztk stores the value with no effect.

% menubutton .highlightcolor -highlightcolor {#336699}
> .highlightcolor cget -highlightcolor
#336699
% menubutton .highlightcolor2 -highlightcolor nocolor
unknown color name "nocolor"
-errorcode TK LOOKUP COLOR nocolor

-highlightthickness DISTANCE

The width of the focus ring, in the look of Tk on X11.

The width of the ring around the menubutton that shows the focus. The modern theme draws the focus with the stylesheet.

% menubutton .highlightthickness -highlightthickness 4
> .highlightthickness cget -highlightthickness
4
% menubutton .highlightthickness2 -highlightthickness x
bad screen distance "x"
-errorcode TK VALUE PIXELS

-image IMAGE

An image to show in place of the text.

The name of an image of image create. With an image and -compound none, the menubutton shows the image and no text.

menubutton with -text File -compound left and -image {}-image {}menubutton with -text File -compound left and -image ::ref::icon-image ::ref::icon
Each with -text File -compound left. ::ref::icon is a photo image of 16 by 16 pixels.
% image create photo pic -width 8 -height 8
> menubutton .image -image pic
> .image cget -image
pic
% menubutton .image2 -image noimage
image "noimage" doesn't exist
-errorcode TK LOOKUP IMAGE noimage

-indicatoron BOOLEAN

Whether the menubutton draws an indicator of its menu.

In Tk, a true value draws a small box at the right side of the menubutton, as in an option menu. ztk stores the value with no effect.

% menubutton .indicatoron -indicatoron 1
> .indicatoron cget -indicatoron
1
% menubutton .indicatoron2 -indicatoron maybe
expected boolean value but got "maybe"
-errorcode TCL VALUE NUMBER

-justify SIDE

The alignment of the lines of the text.

How the lines of a text with more than one line line up with each other. A text breaks into lines at each newline and at -wraplength.

Values: left, right, center.

menubutton with -text Short
A longer line and -justify left-justify leftmenubutton with -text Short
A longer line and -justify center-justify centermenubutton with -text Short
A longer line and -justify right-justify right
Each with -text Short A longer line.
% menubutton .justify -justify left
> .justify cget -justify
left
% menubutton .justify2 -justify middle
bad justification "middle": must be left, right, or center
-errorcode TCL LOOKUP INDEX justification middle

-menu MENU

The menu that the menubutton opens.

The path name of a menu widget. A click on the menubutton posts the menu at the place of -direction. In Tk the menu must be a child of the menubutton, so that the keyboard can move through it.

% menubutton .menu -text File -menu .menu.items
> menu .menu.items
> .menu.items add command -label Open
> .menu cget -menu
.menu.items

ztk stores each value with no check, as Tk 8.6.16 does.

-padx DISTANCE

The space at the left and the right of the content.

The default 0 leaves the padding to the theme, and a value of the script replaces the padding of the theme at both sides.

menubutton with -text File and -padx 1-padx 1menubutton with -text File and -padx 4-padx 4menubutton with -text File and -padx 16-padx 16
Each with -text File.
% menubutton .padx -padx 4
> .padx cget -padx
4
% menubutton .padx2 -padx x
bad screen distance "x"
-errorcode TK VALUE PIXELS

-pady DISTANCE

The space above and below the content.

The default 0 leaves the padding to the theme, and a value of the script replaces the padding of the theme above and below.

menubutton with -text File and -pady 1-pady 1menubutton with -text File and -pady 2-pady 2menubutton with -text File and -pady 10-pady 10
Each with -text File.
% menubutton .pady -pady 4
> .pady cget -pady
4
% menubutton .pady2 -pady x
bad screen distance "x"
-errorcode TK VALUE PIXELS

-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.

menubutton with -text File -borderwidth 4 and -relief flat-relief flatmenubutton with -text File -borderwidth 4 and -relief raised-relief raisedmenubutton with -text File -borderwidth 4 and -relief sunken-relief sunkenmenubutton with -text File -borderwidth 4 and -relief groove-relief groovemenubutton with -text File -borderwidth 4 and -relief ridge-relief ridgemenubutton with -text File -borderwidth 4 and -relief solid-relief solid
Each with -text File -borderwidth 4. The modern theme draws each relief but flat with the same border.
% menubutton .relief -relief groove
> .relief cget -relief
groove
% menubutton .relief2 -relief bumpy
bad relief "bumpy": must be flat, groove, raised, ridge, solid, or sunken
-errorcode TCL LOOKUP INDEX relief bumpy

-compound WORD

Shows the image and the text together, with the image at that side.

With none, a menubutton with an -image shows only the image. Each other value shows the image and the text, with the image at that side of the text, or under the text with center.

Values: bottom, center, left, none, right, top.

menubutton with -text File -image ::ref::icon and -compound none-compound nonemenubutton with -text File -image ::ref::icon and -compound center-compound centermenubutton with -text File -image ::ref::icon and -compound top-compound topmenubutton with -text File -image ::ref::icon and -compound bottom-compound bottommenubutton with -text File -image ::ref::icon and -compound left-compound leftmenubutton with -text File -image ::ref::icon and -compound right-compound right
Each with -text File -image ::ref::icon.
% menubutton .compound -compound bottom
> .compound cget -compound
bottom
% menubutton .compound2 -compound bogus
bad compound "bogus": must be bottom, center, left, none, right, or top
-errorcode TCL LOOKUP INDEX compound bogus

-state STATE

The state of the menubutton: normal, active, or disabled.

disabled draws the text with -disabledforeground, and a click does not open the menu. active uses the colors of -activebackground and -activeforeground.

Values: active, disabled, normal.

menubutton with -text File and -state normal-state normalmenubutton with -text File and -state active-state activemenubutton with -text File and -state disabled-state disabled
Each with -text File.
% menubutton .state -state disabled
> .state cget -state
disabled
% menubutton .state2 -state busy
bad state "busy": must be active, disabled, or normal
-errorcode TCL LOOKUP INDEX state busy

-takefocus TEXT

Whether the Tab key can move the focus to the menubutton.

1 puts the menubutton 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.

% menubutton .takefocus -takefocus 1
> .takefocus cget -takefocus
1

ztk stores each value of the option with no check.

-text TEXT

The text of the menubutton.

The text that the menubutton shows. A newline starts a new line.

menubutton with -text File-text Filemenubutton with -text {Recent files}-text {Recent files}
% menubutton .text -text Save
> .text cget -text
Save

Each string is a valid text.

-textvariable VAR

A global variable whose value is the text of the menubutton.

The menubutton shows the value of the global variable VAR, and changes its text when the variable changes. The variable stays after the menubutton is destroyed.

% menubutton .textvariable -textvariable v
> .textvariable cget -textvariable
v

Each name is a valid variable name.

-underline N

The index of a character of the text to underline, for a keyboard shortcut.

The index from 0 of the character of -text to underline. -1 underlines no character.

% menubutton .underline -underline 5
> .underline cget -underline
5
% menubutton .underline2 -underline x
expected integer but got "x"
-errorcode TCL VALUE NUMBER

-width CHARS

The width of the menubutton in characters of the font, or in pixels for an image.

For a menubutton with text, the width in average characters of the font. For a menubutton with an image, the width in pixels. 0 makes the menubutton as wide as its content.

menubutton with -text File and -width 0-width 0menubutton with -text File and -width 8-width 8menubutton with -text File and -width 14-width 14
Each with -text File.
% menubutton .width -width 5
> .width cget -width
5
% menubutton .width2 -width x
expected integer but got "x"
-errorcode TCL VALUE NUMBER

-wraplength DISTANCE

The longest line of the text before it breaks into a new line.

A text longer than this distance breaks at a space into more lines. 0 breaks no line.

menubutton with -text A text that is long enough to wrap and -wraplength 0-wraplength 0menubutton with -text A text that is long enough to wrap and -wraplength 60-wraplength 60
Each with -text A text that is long enough to wrap.
% menubutton .wraplength -wraplength 4
> .wraplength cget -wraplength
4
% menubutton .wraplength2 -wraplength x
bad screen distance "x"
-errorcode TK VALUE PIXELS

Bindings

EventDescription
<Button-1>A press gives the button the focus and posts its -menu next to it, on the side of -direction. A disabled button posts no menu.
<B1-Motion>With button 1 down, the entry of the posted menu under the pointer becomes active.
<ButtonRelease-1>The release over an entry of the menu invokes the entry. A release near the point of the press keeps the menu posted, so a click opens the menu and a second click picks an entry.
<Key-space>Posts the menu and makes its first entry active, for the arrow keys.
<Key-Return>The same as the space key.

Returns

PATHNAME.

Examples

% menubutton .file -text File -menu .file.menu
> menu .file.menu
> .file.menu add command -label Open
> .file cget -menu
.file.menu

% winfo class [menubutton .go]
Menubutton

Errors

% menubutton .x -foo 1
unknown option "-foo"
-errorcode TK LOOKUP OPTION -foo

% menubutton .twice
> menubutton .twice
window name "twice" already exists in parent
-errorcode TK VALUE WINDOW EXISTS

# The parent .no does not exist.
% menubutton .no.b
bad window path name ".no"
-errorcode TK LOOKUP WINDOW .no

% menubutton
wrong # args: should be "menubutton 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