Tcl/Tk Tutorial on Tk Selection Widgets

selection widgets are used to select different options in a tk application. the list of available selection widgets are as shown below.

sr.no. widgets & description
1 radiobutton

widget that has a set of on/off buttons and labels, one of which may be selected.

2 checkbutton

widget that has a set of on/off buttons and labels, many of which may be selected.

3 menu

widget that acts as holder for menu items.

4 listbox

widget that displays a list of cells, one or more of which may be selected.

a simple tk example is shown below using selection widgets −

#!/usr/bin/wish

grid [frame .gender ]
grid [label .label1  -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
grid [label .mylabel2  -text "range 1 not selected" -textvariable mylabelvalue2 ] 
grid [checkbutton .chk1 -text "range 1" -variable occupied1 -command {if {$occupied1 } {
   set mylabelvalue2 {range 1 selected}
} else {
   set mylabelvalue2 {range 1 not selected}
} }]
proc setlabel {text} {
   .label configure -text $text 
}

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

selection widget example