Version 15 of radiobutton

Updated 2012-08-20 10:57:06 by RLE

Tk's radiobutton widget documentation can be found at http://purl.org/tcl/home/man/tcl8.5/TkCmd/radiobutton.htm while Ttk's radiobutton widget documentation can be found at http://www.tcl.tk/man/tcl8.5/TkCmd/ttk_radiobutton.htm .

Using a radiobutton as a radiobutton generally means that there are two or more of them. In the old days (pre-cassettes and CDs), an automobile's radio often had this idea of a set of mutually exclusive buttons. Each button would (if the owner could figure out how to do it) have a radio station associated with it. When someone would select a station by pressing one of the buttons, it would stay down, and the radio would be tuned to the station that had been associated with that button. When the next radio button was selected by being pressed, the previous one would return to the unselected state. Thus, in theory (if the hardware was working) only one button would be selected at a time.

Tk's radiobutton concept works similarly. One creates one or more buttons, and ties them together by supplying a variable name via the -variable argument. Each button with the same variable toggles on or off. For the programmer to know which button is selected, they provide a unique string to the -value argument. That value is assigned to the variable when the button has been selected.

If one wishes, they can also associate a command to be executed at the time a radiobutton has been selected.

The radiobutton itself consists of an "image". This image changes depending on the mouse actions within the group. For instance, the radiobutton has an initial state. Then moving the mouse over the radiobutton, but not pressing, changes the look of the default radiobutton. Finally, pressing the radiobutton changes the look yet again.


Tk Example:

Radio buttons are linked using a common variable, which will have the value of the selected button.

 package require Tk

 set ::onetwothree "3"

 pack [radiobutton .one   -value "WNBC" -text "New York"    -variable onetwothree -command { puts "Play $::onetwothree" }]
 pack [radiobutton .two   -value "KDKA" -text "Pittsburgh"   -variable onetwothree -command { puts "Play $::onetwothree" }]
 pack [radiobutton .three -value "WGN"  -text "Chicago"     -variable onetwothree -command { puts "Play $::onetwothree" }]

The results will be that, on stdout, the number of the radiobutton pressed is output.

By setting the onetwothree global variable to 3, the 3rd radio button is selected at the beginning of the code.

The global variable used with radiobuttons probably should exist, even if just set to an empty string, to ensure the initial state of the buttons is set as expected.


LV Would someone mind adding a simple example, with explanation, of the use of radiobutton's new tri-value state? Just curious about the conditions that might lead to using it. http://tip.tcl.tk/110 doesn't really provide background to recognize situations where it comes in handy.


LV In http://tip.tcl.tk/109 , the TIP talks about the appearance of radiobuttons on Unix. The description of the new state, however, is confusing to me. It says:

The radiobutton shall (when the indicator is turned on) display an dot mark or other distinguishing icon/symbol that clearly indicates a selected or On state. The unselected or Off state shall be displayed with an empty diamond. The state values will not be indicated by changing relief or background color.

So, if I were to take this statement literally, I would expect that by default on unix the radiobutton would change from an empty diamond to a dot mark or other distinguising icon/symbol.

What I see, in Tk 8.5, is that by default on unix the radiobutton is not an empty diamond but a dim dot mark and when the state changes, it becomes a bright dot mark. Is there some sort of option or flag to actually get the diamond indicator with the default Tk radiobutton? I notice that on unix, the default ttk::radiobutton is in fact a diamond, both for unselected and selected state.


LV I see in the radiobutton man page that there is an option to set the background color to use when the button is selected. How would you set the background color of the selection image when it is not selected?


See the vertical command, which allows to place the text below the checkfield, on the checkbutton page.


See also

  • Lightbutton a pure Tcl package mimicking the radio and the check buttons, with bright and pretty colors.