Tk - Radio Button Widget



Radio button widget implements a multiple-choice button, which is a way to offer many possible selections to the user and lets user choose only one of them. The syntax for radio button widget is shown below −

radiobutton radiobuttonName options

Options

The options available for the radio button widget are listed below in the following table −

Sr.No. Syntax & Description
1

-font fontDescriptor

Used to set font for widget.

2

-height number

Used to set height for widget.

3

-command action

Sets the command action for button.

4

-text text

Sets the text for the widget.

5

-width number

Sets the width for widget.

6

-variable variableName

Sets the variable for widget.

7

-value variableValue

Sets the variable with variable value.

A simple radio button widget example is shown below −

#!/usr/bin/wish

grid [frame .gender ]
grid [label .myLabel  -text "Male" -textvariable myLabel1 ] 
grid [radiobutton .gender.maleBtn -text "Male"   -variable gender -value "Male"
   -command "set  myLabel1 Male"] -row 1 -column 2
grid [radiobutton .gender.femaleBtn -text "Female" -variable gender -value "Female"
   -command "set  myLabel1 Female"] -row 1 -column 3
.gender.maleBtn select

When we run the above program, we will get the following output −

Radio Button Example
tk_selection_widgets.htm
Advertisements