beetlebug▾
/

ttk::menubutton

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

CommandDescription
ttk::menubutton stateGives the state flags of the widget, or turns flags on and off.
ttk::menubutton instateTests the state flags of the widget, and runs a script when they match.
ttk::menubutton identifyGives the name of the element of the widget at a point.
ttk::menubutton cgetGives the value of an option of the widget.
ttk::menubutton configureGives or sets the options of the widget.

Options

OptionTypeDefaultDatabase nameDescription
-classclass nameemptyThe class of the widget for the option database and for the bindings, set only at creation.
-compoundcompoundemptycompoundWhich of the text and the image show, and the place of the image.
-cursorcursor nameemptycursorThe shape of the pointer over the widget.
-directiondirectionbelowdirectionThe side of the button where the menu opens.
-imageimage listemptyimageAn image to show, with other images for some states.
-menuwidget pathemptymenuThe path of the menu that the button opens.
-paddinglist of distancesemptypaddingThe space between the border and the content, in place of the padding of the style.
-statestringnormalstateA state for compatibility with the classic widgets: normal or disabled.
-stylestyle nameemptystyleThe style of the widget, such as TMenubutton or a style of the script such as Accent.TMenubutton.
-takefocusboolean, empty, or scriptttk::takefocustakeFocusWhether the Tab key can move the focus to the widget.
-textstringemptytextThe text of the widget.
-textvariablevariable nameemptytextVariableA global variable whose value is the text of the widget.
-underlineinteger-1underlineThe index of a character of the text to underline, for a keyboard shortcut.
-widthintegeremptywidthThe 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.

ttk::menubutton with -text File -image ::ref::icon and -compound none-compound nonettk::menubutton with -text File -image ::ref::icon and -compound text-compound textttk::menubutton with -text File -image ::ref::icon and -compound image-compound imagettk::menubutton with -text File -image ::ref::icon and -compound center-compound centerttk::menubutton with -text File -image ::ref::icon and -compound top-compound topttk::menubutton with -text File -image ::ref::icon and -compound bottom-compound bottomttk::menubutton with -text File -image ::ref::icon and -compound left-compound leftttk::menubutton with -text File -image ::ref::icon and -compound right-compound right
Each with -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.

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
hand2

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

ValueDescription
aboveThe menu opens above the button.
belowThe menu opens below the button.
leftThe menu opens at the left of the button.
rightThe menu opens at the right of the button.
flushThe 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.

ttk::menubutton with -text File -compound left and -image {}-image {}ttk::menubutton with -text File -compound left and -image ::ref::icon-image ::ref::icon
Each with -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.m

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

ttk::menubutton with -text File and -padding {}-padding {}ttk::menubutton with -text File and -padding 2-padding 2ttk::menubutton with -text File and -padding {16 4}-padding {16 4}
Each with -text File.
% ttk::menubutton .tmbpa -text Hi
> ttk::menubutton .tmbpa30 -text Hi -padding 30
> expr {[winfo reqwidth .tmbpa30] > [winfo reqwidth .tmbpa]}
1

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

ttk::menubutton with -text File and -state normal-state normalttk::menubutton with -text File and -state disabled-state disabled
Each with -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
normal

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

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

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

ttk::menubutton with -text File-text Filettk::menubutton with -text {Recent files}-text {Recent files}
% ttk::menubutton .tmbtx -text Save
> set w [winfo reqwidth .tmbtx]
> .tmbtx configure -text "Save as"
> expr {[winfo reqwidth .tmbtx] > $w}
1

Each 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}
1

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

ttk::menubutton with -text File and -width {}-width {}ttk::menubutton with -text File and -width 4-width 4ttk::menubutton with -text File and -width 14-width 14
Each with -text File.
% ttk::menubutton .tmbwi -text Hi
> ttk::menubutton .tmbwi10 -text Hi -width 10
> expr {[winfo reqwidth .tmbwi10] > [winfo reqwidth .tmbwi]}
1

ztk stores each value at configure, as Tk 8.6.16 does.

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.

States

StateDescription
activeThe pointer is over the button. hover is the same flag in ztk.
focusThe button has the keyboard focus.
disabledThe button posts no menu, and the theme draws it grayed.
ttk::menubutton with -text File and no state flagno state flagttk::menubutton with -text File and state activestate activettk::menubutton with -text File and state focusstate focusttk::menubutton with -text File and state disabledstate disabled
Each with -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

Facts

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

See also