ttk::menubutton
Guide: Menus
Makes a themed menubutton: a button that opens a menu.
ttk::menubutton PATHNAME ?OPTION VALUE ...?
Description
Makes a themed menubutton with the name PATHNAME and the options of the call, and makes the command PATHNAME for the widget commands below. A click on the button, or the Space or Return key, posts the menu of -menu at the side of -direction. The menu runs the commands of its entries.
Make the menu a child of the menubutton, such as .file.menu for the menubutton .file. The parent of PATHNAME must exist, and PATHNAME must not. The class of the menubutton is TMenubutton, the name of its style.
Widget commands
| Command | Description |
|---|---|
ttk::menubutton state | Gives the state flags of the widget, or turns flags on and off. |
ttk::menubutton instate | Tests the state flags of the widget, and runs a script when they match. |
ttk::menubutton identify | Gives the name of the element of the widget at a point. |
ttk::menubutton cget | Gives the value of an option of the widget. |
ttk::menubutton configure | Gives or sets the options of the widget. |
Options
| Option | Type | Default | Database name | Description |
|---|---|---|---|---|
-class | class name | empty | The class of the widget for the option database and for the bindings, set only at creation. | |
-compound | compound | empty | compound | Which of the text and the image show, and the place of the image. |
-cursor | cursor name | empty | cursor | The shape of the pointer over the widget. |
-direction | direction | below | direction | The side of the button where the menu opens. |
-image | image list | empty | image | An image to show, with other images for some states. |
-menu | widget path | empty | menu | The path of the menu that the button opens. |
-padding | list of distances | empty | padding | The space between the border and the content, in place of the padding of the style. |
-state | string | normal | state | A state for compatibility with the classic widgets: normal or disabled. |
-style | style name | empty | style | The style of the widget, such as TMenubutton or a style of the script such as Accent.TMenubutton. |
-takefocus | boolean, empty, or script | ttk::takefocus | takeFocus | Whether the Tab key can move the focus to the widget. |
-text | string | empty | text | The text of the widget. |
-textvariable | variable name | empty | textVariable | A global variable whose value is the text of the widget. |
-underline | integer | -1 | underline | The index of a character of the text to underline, for a keyboard shortcut. |
-width | integer | empty | width | The width of the text area in average characters of the font. |
-class CLASS
The class of the widget for the option database and for the bindings, set only at creation.
The class name replaces the class of the style, such as TMenubutton, in winfo class and in the bind tags of the widget. Give it in the call that makes the widget. A later configure of the option gives an error.
% ttk::menubutton .tmbcl -class Fancy
> winfo class .tmbcl
Fancy
% ttk::menubutton .tmbcl3
> winfo class .tmbcl3
TMenubutton% ttk::menubutton .tmbcl2
> .tmbcl2 configure -class Other
attempt to change read-only option
-errorcode NONE-compound PLACE
Which of the text and the image show, and the place of the image.
An empty value or none shows the image when there is one, else the text. text shows only the text, and image shows only the image. center, top, bottom, left, and right show the two together, with the image at that side of the text, or under it with center.
Values: none, text, image, center, top, bottom, left, right.

-compound none
-compound text
-compound image
-compound center
-compound top
-compound bottom
-compound left
-compound right-text File -image ::ref::icon. none shows the image when the widget has one, as image does, and as in Tk.% image create photo tmbpic -width 40 -height 20
> ttk::menubutton .tmbcpi -image tmbpic -text Save
> ttk::menubutton .tmbcpl -image tmbpic -text Save -compound left
> expr {[winfo reqwidth .tmbcpl] > [winfo reqwidth .tmbcpi]}
1% ttk::menubutton .tmbco -compound sideways
bad compound "sideways": must be none, text, image, center, top, bottom, left, or right
-errorcode TCL LOOKUP INDEX compound sideways-cursor CURSOR
The shape of the pointer over the widget.
Each value side by side: the contact sheet of the cursors
A cursor name of Tk, such as hand2 or xterm. An empty value uses the cursor of the parent.
% ttk::menubutton .tmbcu -cursor hand2
> .tmbcu cget -cursor
hand2ztk stores each value of the option with no check.
-direction DIRECTION
The side of the button where the menu opens.
The menu opens at that side of the button when the user clicks it.
| Value | Description |
|---|---|
above | The menu opens above the button. |
below | The menu opens below the button. |
left | The menu opens at the left of the button. |
right | The menu opens at the right of the button. |
flush | The menu opens over the button, with its corner at the corner of the button. |
% ttk::menubutton .tmbdi -direction above
> .tmbdi cget -direction
above% ttk::menubutton .tmbdi2 -direction bogus
bad direction "bogus": must be above, below, flush, left, or right
-errorcode TCL LOOKUP INDEX direction bogus-image IMAGESPEC
An image to show, with other images for some states.
The first word is the name of an image of image create. More words are pairs of a state spec and an image, such as {pic disabled pic2}. With an image and -compound none, the widget shows the image and no text. The image must exist when the option is set.

-image {}
-image ::ref::icon-text File -compound left.# The text does not show, so the two widgets have the same width.
% image create photo tmbicon -width 40 -height 20
> ttk::menubutton .tmbim -image tmbicon -text Save
> ttk::menubutton .tmbim2 -image tmbicon
> expr {[winfo reqwidth .tmbim] == [winfo reqwidth .tmbim2]}
1% ttk::menubutton .tmbim3 -image noimage
image "noimage" doesn't exist
-errorcode TK LOOKUP IMAGE noimage-menu MENU
The path of the menu that the button opens.
The menu must be a child of the menubutton. The menu does not need to exist when the option is set, but it must exist when the user clicks the button.
% ttk::menubutton .tmbme -text View -menu .tmbme.m
> .tmbme cget -menu
.tmbme.mztk stores each value of the option with no check, as Tk 8.6.16 does.
-padding PADDING
The space between the border and the content, in place of the padding of the style.
A list of 1 to 4 distances: left, top, right, and bottom. One value is the space at each side. Two values are the left and right space and the top and bottom space. A missing third value is the first, and a missing fourth value is the second. An empty value uses the padding of the style.

-padding {}
-padding 2
-padding {16 4}-text File.% ttk::menubutton .tmbpa -text Hi
> ttk::menubutton .tmbpa30 -text Hi -padding 30
> expr {[winfo reqwidth .tmbpa30] > [winfo reqwidth .tmbpa]}
1ztk stores each value at configure, as Tk 8.6.16 does.
-state STATE
A state for compatibility with the classic widgets: normal or disabled.
disabled sets the state flag disabled of the widget, and normal clears it. The state command changes the flags with no change of this option. ztk stores each other value with no check.

-state normal
-state disabled-text File.% ttk::menubutton .tmbst -state disabled
> .tmbst instate disabled
1
# The `state` command does not change the option.
% ttk::menubutton .tmbst2
> .tmbst2 state disabled
> .tmbst2 cget -state
normalztk stores each value of the option with no check, as Tk 8.6.16 does.
-style STYLE
The style of the widget, such as TMenubutton or a style of the script such as Accent.TMenubutton.
An empty value uses the style TMenubutton. A style name of the script ends with the name of the base style, such as Accent.TMenubutton, and ttk::style configure and ttk::style map give it its options. A style with no settings looks like its base style.
% ttk::style configure Accent.TMenubutton -foreground white
> ttk::menubutton .tmbsy -style Accent.TMenubutton
> .tmbsy cget -style
Accent.TMenubuttonztk stores each value of the option with no check, as Tk 8.6.16 does at creation.
-takefocus VALUE
Whether the Tab key can move the focus to the widget.
1 puts the widget in the order of the Tab key, and 0 leaves it out. An empty value uses the default rule of Tk. A script, such as the default ttk::takefocus, runs with the path of the widget appended, and its result is the answer. ttk::takefocus gives 1 when the widget is not disabled and is viewable.
% ttk::menubutton .tmbtf -takefocus 0
> .tmbtf cget -takefocus
0ztk stores each value of the option with no check.
-text TEXT
The text of the widget.
The text that the widget shows. A newline starts a new line. A longer text makes a wider widget, unless -width sets the width. With -textvariable, the variable sets the text.

-text File
-text {Recent files}% ttk::menubutton .tmbtx -text Save
> set w [winfo reqwidth .tmbtx]
> .tmbtx configure -text "Save as"
> expr {[winfo reqwidth .tmbtx] > $w}
1Each string is a valid text.
-textvariable VAR
A global variable whose value is the text of the widget.
The widget shows the value of the global variable VAR in place of -text, and changes its text when the variable changes. The variable stays after the widget is destroyed.
% set tmbtitle Open
> ttk::menubutton .tmbtv -textvariable tmbtitle
> set w [winfo reqwidth .tmbtv]
> set tmbtitle "Open a file"
> expr {[winfo reqwidth .tmbtv] > $w}
1Each name is a valid variable name.
-underline INDEX
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. The underline is a hint for a shortcut key, and the script binds the key itself.
% ttk::menubutton .tmbun -text Save -underline 0
> .tmbun cget -underline
0% ttk::menubutton .tmbun2 -underline x
expected integer but got "x"
-errorcode TCL VALUE NUMBER-width CHARS
The width of the text area in average characters of the font.
A positive value sets the width of the text area. A negative value sets a minimum width, and a longer text makes the widget wider. An empty value makes the widget as wide as its content.

-width {}
-width 4
-width 14-text File.% ttk::menubutton .tmbwi -text Hi
> ttk::menubutton .tmbwi10 -text Hi -width 10
> expr {[winfo reqwidth .tmbwi10] > [winfo reqwidth .tmbwi]}
1ztk stores each value at configure, as Tk 8.6.16 does.
Bindings
| Event | Description |
|---|---|
<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. |
States
| State | Description |
|---|---|
active | The pointer is over the button. hover is the same flag in ztk. |
focus | The button has the keyboard focus. |
disabled | The button posts no menu, and the theme draws it grayed. |

no state flag
state active
state focus
state disabled-text File.Returns
PATHNAME.
Examples
% ttk::menubutton .file -text File -menu .file.menu
> menu .file.menu -postcommand {set ::posted 1}
> .file.menu add command -label Open
> pack .file
> update
> event generate .file <Button-1> -x 5 -y 5
> update
> set ::posted
1
% winfo class [ttk::menubutton .edit -text Edit]
TMenubutton
Errors
# A menubutton has no command. The entries of its menu have the commands.
% ttk::menubutton .x -command {puts hi}
unknown option "-command"
-errorcode TK LOOKUP OPTION -command
% ttk::menubutton .twice
> ttk::menubutton .twice
window name "twice" already exists in parent
-errorcode TK VALUE WINDOW EXISTS
# The parent .no does not exist.
% ttk::menubutton .no.m
bad window path name ".no"
-errorcode TK LOOKUP WINDOW .no
% ttk::menubutton
wrong # args: should be "ttk::menubutton pathName ?-option value ...?"
-errorcode TCL WRONGARGS
Differences from Tcl
-cursoraccepts each value with no check. Tk 8.6.16 gives an error for a cursor name that does not exist.- The errors of
state,instate,identify, and of an unknown widget command have the error codeNONE. Tk 8.6.16 gives a code such asTTK VALUE STATEorTCL LOOKUP INDEX. The error of-underlinehas the codeTCL VALUE NUMBER, where Tk 8.6.16 givesTCL VALUE INTEGER. - The states
activeandhoverare one flag, sostate activeturns on both. - The error of a bad
-directionlists the values in the orderabove, below, flush, left, or right. Tk 8.6.16 givesabove, below, left, right, or flush.
Facts
- Capability
- None.
- Tcl release
- Tcl 9.1 and 8.6.
- Since
- ztk 1.0.0-beta.1