Radio Buttons

Radio Buttons

Radio buttons are the last kind of buttons supported by Tk. Radio buttons are arranged into groups and provide a way to choose one of a set of options.

The way radio buttons work is as follows: all radio buttons in a group are connected to the same Tcl variable. Each button specifies a value that will be assigned to the Tcl variable when it is selected. When one button in the group is clicked, any other button in that group that was selected becomes deselected. Also, the value of the Tcl variable is set to the value associated with the button just clicked. The example below demonstrates this:


Some things to note: All of the above radio buttons are associated with the same Tcl variable, choice, so they all behave as one group. The white label at the right hand side of the Tclet shows the current value of that variable. Each button has a -value option specifying what value the choice variable should take when this button is selected. And you can set an initial selection by specifying the initial value of the Tcl variable to be one of the choices; in this example I set the initial value to red, which automatically causes the first radio button to be selected when the Tclet is first displayed. Finally, setting the Tcl variable to a value which is not one of those selected by any of the associated radio buttons will be reflected in the Tclet by showing all buttons deselected.

Of course, you can set the value of the associated variable in the application and Tk takes care of updating the user interface to reflect the current value selected:


If the application sets the value of the associated Tcl variable to something that is not one of the values specified by any of the radio buttons in the group, all of the buttons are shown deselected:


You can specify the same value for more than one button in a group, and when one of the buttons specifying a given value is clicked, all buttons with that value are shown selected:


In the above example, I specified that one group of buttons sets the underlying Tcl variable to 1, and the other group sets the value to 0. And, of course, this works from inside the application also:


As you can see, the radio button widget provides quite a lot of features in an easy to use package.

Next, let's look at some options that you can use to modify the appearance and behavior of check buttons and radio buttons.



comments?