beetlebug▾
/

ttk::radiobutton

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

CommandDescription
ttk::radiobutton invokeSelects the button, as a click does, and runs its command.
ttk::radiobutton stateGives the state flags of the widget, or turns flags on and off.
ttk::radiobutton instateTests the state flags of the widget, and runs a script when they match.
ttk::radiobutton identifyGives the name of the element of the widget at a point.
ttk::radiobutton cgetGives the value of an option of the widget.
ttk::radiobutton 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.
-commandscriptemptycommandThe script that runs after a click or invoke sets the variable.
-compoundcompoundemptycompoundWhich of the text and the image show, and the place of the image.
-cursorcursor nameemptycursorThe shape of the pointer over the widget.
-imageimage listemptyimageAn image to show, with other images for some states.
-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 TRadiobutton or a style of the script such as Accent.TRadiobutton.
-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.
-valuestring1ValueThe value that the button puts in the variable.
-variablevariable name::selectedButtonvariableThe global variable that the buttons of a group share.
-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 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.

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
hand2

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

ttk::radiobutton with -text Small -variable ::ref::off -value 1 and -padding {}-padding {}ttk::radiobutton with -text Small -variable ::ref::off -value 1 and -padding 4-padding 4ttk::radiobutton with -text Small -variable ::ref::off -value 1 and -padding {16 4}-padding {16 4}
Each with -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]}
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::radiobutton with -text Small -variable ::ref::off -value 1 and -state normal-state normalttk::radiobutton with -text Small -variable ::ref::off -value 1 and -state disabled-state disabled
Each with -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
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 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.TRadiobutton

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::radiobutton .trtf -takefocus 0
> .trtf 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::radiobutton with -variable ::ref::off -value 1 and -text Small-text Smallttk::radiobutton with -variable ::ref::off -value 1 and -text {Extra large}-text {Extra large}
Each with -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}
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 trtitle Open
> ttk::radiobutton .trtv -textvariable trtitle
> set w [winfo reqwidth .trtv]
> set trtitle "Open a file"
> expr {[winfo reqwidth .trtv] > $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::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
1

Each 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]
{} selected

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

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

Bindings

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

EventDescription
<<Invoke>>The same as the space key. event generate .r <<Invoke>> sends it.

States

StateDescription
selectedThe variable holds the -value of the button: the theme draws a dot.
alternateThe variable does not exist, or holds an empty string. The modern theme draws the button clear, as with no flag.
activeThe pointer is over the button. hover is the same flag in ztk.
pressedButton 1 is down on the button.
focusThe button has the keyboard focus.
disabledThe button changes no value, and the theme draws it grayed.
ttk::radiobutton with -text Small -variable ::ref::off -value 1 and no state flagno state flagttk::radiobutton with -text Small -variable ::ref::off -value 1 and state selectedstate selectedttk::radiobutton with -text Small -variable ::ref::off -value 1 and state activestate activettk::radiobutton with -text Small -variable ::ref::off -value 1 and state focusstate focusttk::radiobutton with -text Small -variable ::ref::off -value 1 and state disabledstate disabledttk::radiobutton with -text Small -variable ::ref::off -value 1 and state selected disabledstate selected disabled
Each with -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

Facts

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

See also