Tuesday, April 4, 2017

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

No comments:

Post a Comment