Version 19 of radiobutton

Updated 2014-12-14 02:34:46 by PeterDH

radiobutton , a Tk command, creates and manipulates radiobutton widgets.

See Also

checkbutton
includes vertical, a proc that places text below the checkfield
Lightbutton
a pure Tcl package mimicking the radio and the check buttons, with bright and pretty colors.

Documentation

official reference
TIP 109 , New Look for Checkbutton and Radiobutton on Unix
included as of Tcl 8.5
TIP 110
describes tri-state options, which are included as of Tcl 8.5

Description

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. Thus a radiobutton in accordance with the traditional meaning generally means that there are two or more of them. 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.

Example

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

package require Tk

set onetwothree KDKA
foreach {station city} {WNBC {New York} KDKA Pittsburg WGN Chicago} {
    pack [radiobutton .[incr i] -value $station -text $city \
        -variable [namespace current]::onetwothree -command [namespace code {
            puts "Playing $onetwothree"
        }]]
}

This example displays the station sign on the console. Setting $onetwothree to KDKA causes the corresponding radio button to be selected.

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

Want conventional buttons instead of the little diamonds or circles? Use option '-indicatoron 0' for the array to be composed of ordinary buttons, which have width and height.