ttk::radiobutton
Guide: Themed widgets (ttk)
Makes a themed radio button: one button of a group that shares a variable, with one button on at a time.
ttk::radiobutton PATHNAME ?OPTION VALUE ...?
Description
Makes a themed radio button with the name PATHNAME and the options of the call, and makes the command PATHNAME for the widget commands below. The buttons of a group share one -variable, and each one has its own -value. A click sets the variable to the -value of the button, and then runs -command.
The button follows its variable: it has the state flag selected while the variable has the value of -value. While the variable does not exist, it has the flag alternate.
The parent of PATHNAME must exist, and PATHNAME must not. The class of the button is TRadiobutton, the name of its style. In ztk the button uses the code and the CSS rules of the classic radiobutton.
Widget commands
| Command | Description |
|---|---|
ttk::radiobutton invoke | Selects the button, as a click does, and runs its command. |
ttk::radiobutton state | Gives the state flags of the widget, or turns flags on and off. |
ttk::radiobutton instate | Tests the state flags of the widget, and runs a script when they match. |
ttk::radiobutton identify | Gives the name of the element of the widget at a point. |
ttk::radiobutton cget | Gives the value of an option of the widget. |
ttk::radiobutton 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. | |
-command | script | empty | command | The script that runs after a click or invoke sets the variable. |
-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. |
-image | image list | empty | image | An image to show, with other images for some states. |
-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 TRadiobutton or a style of the script such as Accent.TRadiobutton. |
-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. |
-value | string | 1 | Value | The value that the button puts in the variable. |
-variable | variable name | ::selectedButton | variable | The global variable that the buttons of a group share. |
-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 TRadiobutton, 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::radiobutton .trcl -class Fancy
> winfo class .trcl
Fancy
% ttk::radiobutton .trcl3
> winfo class .trcl3
TRadiobutton% ttk::radiobutton .trcl2
> .trcl2 configure -class Other
attempt to change read-only option
-errorcode NONE-command SCRIPT
The script that runs after a click or invoke sets the variable.
The script runs at the global level after the button sets its variable. Its result is the result of invoke, and an error of the script is the error of invoke.
% ttk::radiobutton .trcm -variable trcm -value a -command {set ::trgot $::trcm}
> .trcm invoke
> set ::trgot
a% ttk::radiobutton .trcm2 -variable trcm2 -command {error boom}
> .trcm2 invoke
boom
-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.
% image create photo trpic -width 40 -height 20
> ttk::radiobutton .trcpi -image trpic -text Save
> ttk::radiobutton .trcpl -image trpic -text Save -compound left
> expr {[winfo reqwidth .trcpl] > [winfo reqwidth .trcpi]}
1% ttk::radiobutton .trco -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::radiobutton .trcu -cursor hand2
> .trcu cget -cursor
hand2ztk stores each value of the option with no check.
-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 create photo tricon -width 40 -height 20
> ttk::radiobutton .trim -image tricon
> expr {[winfo reqheight .trim] >= 20}
1% ttk::radiobutton .trim3 -image noimage
image "noimage" doesn't exist
-errorcode TK LOOKUP IMAGE noimage-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 4
-padding {16 4}-text Small -variable ::ref::off -value 1.% ttk::radiobutton .trpa -text Hi
> ttk::radiobutton .trpa30 -text Hi -padding 30
> expr {[winfo reqwidth .trpa30] > [winfo reqwidth .trpa]}
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 Small -variable ::ref::off -value 1.% ttk::radiobutton .trst -state disabled
> .trst instate disabled
1
# The `state` command does not change the option.
% ttk::radiobutton .trst2
> .trst2 state disabled
> .trst2 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 TRadiobutton or a style of the script such as Accent.TRadiobutton.
An empty value uses the style TRadiobutton. A style name of the script ends with the name of the base style, such as Accent.TRadiobutton, 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.TRadiobutton -foreground white
> ttk::radiobutton .trsy -style Accent.TRadiobutton
> .trsy cget -style
Accent.TRadiobuttonztk 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::radiobutton .trtf -takefocus 0
> .trtf 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 Small
-text {Extra large}-variable ::ref::off -value 1.% ttk::radiobutton .trtx -text Save
> set w [winfo reqwidth .trtx]
> .trtx configure -text "Save as"
> expr {[winfo reqwidth .trtx] > $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 trtitle Open
> ttk::radiobutton .trtv -textvariable trtitle
> set w [winfo reqwidth .trtv]
> set trtitle "Open a file"
> expr {[winfo reqwidth .trtv] > $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::radiobutton .trun -text Save -underline 0
> .trun cget -underline
0% ttk::radiobutton .trun2 -underline x
expected integer but got "x"
-errorcode TCL VALUE NUMBER-value VALUE
The value that the button puts in the variable.
A click sets the variable to this value. The button has the flag selected while the variable has this value. Each button of a group needs its own value.
% ttk::radiobutton .trva -variable trva -value red
> set trva red
> .trva instate selected
1Each string is a valid value.
-variable VAR
The global variable that the buttons of a group share.
The button sets the variable to its -value, and shows whether the variable has that value. The button does not set the variable before the first click.
% ttk::radiobutton .trvb
> .trvb cget -variable
::selectedButton
% ttk::radiobutton .trvb2 -variable trvb -value a
> ttk::radiobutton .trvb3 -variable trvb -value b
> set trvb b
> list [.trvb2 state] [.trvb3 state]
{} selectedEach name is a valid variable name.
-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::radiobutton .trwi -text Hi
> ttk::radiobutton .trwi10 -text Hi -width 10
> expr {[winfo reqwidth .trwi10] > [winfo reqwidth .trwi]}
1ztk stores each value at configure, as Tk 8.6.16 does.
Bindings
| Event | Description |
|---|---|
<Enter> | The pointer is over the button: the theme draws the hover look (the state active). |
<Leave> | The pointer leaves the button, and the hover look goes. |
<Button-1> | A press sets the variable to the -value of the button and runs the -command script at once, as invoke does. A press on a disabled button changes no value. |
<Key-space> | Sets the variable to the -value of the button and runs the -command script, as invoke does. |
<Key-Return> | The same as the space key. |
Virtual events
| Event | Description |
|---|---|
<<Invoke>> | The same as the space key. event generate .r <<Invoke>> sends it. |
States
| State | Description |
|---|---|
selected | The variable holds the -value of the button: the theme draws a dot. |
alternate | The variable does not exist, or holds an empty string. The modern theme draws the button clear, as with no flag. |
active | The pointer is over the button. hover is the same flag in ztk. |
pressed | Button 1 is down on the button. |
focus | The button has the keyboard focus. |
disabled | The button changes no value, and the theme draws it grayed. |

no state flag
state selected
state active
state focus
state disabled
state selected disabled-text Small -variable ::ref::off -value 1. ::ref::off holds 0, so the button with the value 1 is clear.Returns
PATHNAME.
Examples
% ttk::radiobutton .small -text Small -variable size -value s
> ttk::radiobutton .large -text Large -variable size -value l
> .large invoke
> list $size [.small instate selected] [.large instate selected]
l 0 1
% winfo class [ttk::radiobutton .medium -text Medium]
TRadiobutton
Errors
% ttk::radiobutton .x -foo b
unknown option "-foo"
-errorcode TK LOOKUP OPTION -foo
% ttk::radiobutton .twice
> ttk::radiobutton .twice
window name "twice" already exists in parent
-errorcode TK VALUE WINDOW EXISTS
# The parent .no does not exist.
% ttk::radiobutton .no.c
bad window path name ".no"
-errorcode TK LOOKUP WINDOW .no
% ttk::radiobutton
wrong # args: should be "ttk::radiobutton 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 wrong count of arguments of
invokehas the error codeNONE. Tk 8.6.16 givesTCL WRONGARGS. - A press of button 1 changes the value. In Tk 8.6.16 the release changes it, and a release away from the button changes no value.
Facts
- Capability
- None.
- Tcl release
- Tcl 9.1 and 8.6.
- Since
- ztk 1.0.0-beta.1