Previous
Frame, Text, Scrollbar, Scale
Frame, Text, Scrollbar, Scale
Next
Listbox
Listbox
Tcl/Tk Tutorial - Create GUI using Tk with Tcl Language
Radiobutton, Checkbutton
Radiobutton, Checkbutton
Widgets 3 : Radiobutton, Checkbutton
radiobutton
Radiobutton is an input where any one of many choices MUST be chosen. If one is chosen and another button is clicked, the last chosen will lose its state and the clicked button will be chosen. A graphic example(in HTML) is given below.Some Options
-command COMMAND | Specifies a Tcl command to associate with the button. This command is typically invoked when mouse button 1 is released over the button window. |
-variable VARIABLE | Specifies name of global variable to set to indicate whether or not this button is selected. |
-value VALUE | Specifies value to store in the button's associated variable whenever this button is selected. |
Syntax | Description | Example |
path deselect | Deselects the checkbutton and sets the associated variable to its ``off'' value. | .rdb_m deselect |
path select | Selects the checkbutton and sets the associated variable to its ``on'' value. | .rdb_m select |
Example
#This function will be exected when the button is pushed
proc push_button {} {
global age gender
set name [.ent get]
.txt insert end "$name\($gender\) is $age years old."
}
#Global Variables
set age 10
set gender "Male"
#GUI building
frame .frm_name
label .lab -text "Name:"
entry .ent
#Age
scale .scl -label "Age :" -orient h -digit 1 -from 10 -to 50 \
-variable age -tickinterval 10
#Gender
frame .gender
label .lbl_gender -text "Sex "
radiobutton .gender.rdb_m -text "Male" -variable gender -value "Male"
radiobutton .gender.rdb_f -text "Female" -variable gender -value "Female"
.gender.rdb_m select
button .but -text "Push Me" -command "push_button"
#Text Area
frame .textarea
text .txt -yscrollcommand ".srl_y set" -xscrollcommand ".srl_x set" \
-width 40 -height 10
scrollbar .srl_y -command ".txt yview" -orient v
scrollbar .srl_x -command ".txt xview" -orient h
#Geometry Management
grid .frm_name -in . -row 1 -column 1 -columnspan 2
grid .lab -in .frm_name -row 1 -column 1
grid .ent -in .frm_name -row 1 -column 2
grid .scl -in . -row 2 -column 1 -columnspan 2
grid .gender -in . -row 3 -column 2
grid .lbl_gender -in .gender -row 1 -column 1
grid .gender.rdb_m -in .gender -row 1 -column 2
grid .gender.rdb_f -in .gender -row 1 -column 3
grid .but -in . -row 4 -column 1 -columnspan 2
grid .txt -in .textarea -row 1 -column 1
grid .srl_y -in .textarea -row 1 -column 2 -sticky ns
grid .srl_x -in .textarea -row 2 -column 1 -sticky ew
grid .textarea -in . -row 5 -column 1 -columnspan 2
This time the program is subjected to even more change - the geometry manager is fully grid now. There is no instances of pack. You will find this necessary when the layout becomes more complicated. I hope you can stay with me in such trying times.
checkbutton
Checkbotton is a input with two options - Off or On - it has to be either one. The state can be changed by clicking on it. An example is shown below.Some Options
-offvalue VALUE | Specifies value to store in the button's associated variable whenever this button is deselected. Defaults to ``0''. |
-onvalue VALUE | Specifies value to store in the button's associated variable whenever this button is selected. Defaults to ``1''. |
-command COMMAND | Specifies a Tcl command to associate with the button. This command is typically invoked when mouse button 1 is released over the button window. |
-variable VARABLE | Specifies name of global variable to set to indicate whether or not this button is selected. |
Syntax | Description | Example |
path deselect | Deselects the checkbutton and sets the associated variable to its ``off'' value. | .chk deselect |
path select | Selects the checkbutton and sets the associated variable to its ``on'' value. | .chk select |
path toggle | Toggles the selection state of the button, redisplaying it and modifying its associated variable to reflect the new state. | .chk toggle |
Example
#This function will be executed when the button is pushed
proc push_button {} {
global age occupied gender
set name [.ent get]
.txt insert end "$name\($gender\) is $age years old and is "
if { $occupied == 1 } {
.txt insert end "occupied."
} else {
.txt insert end "unemployed."
}
}
#Global Variables
set age 10
set occupied 1
set gender "Male"
#GUI building
frame .frm_name
label .lab -text "Name:"
entry .ent
#Age
scale .scl -label "Age :" -orient h -digit 1 -from 10 -to 50 \
-variable age -tickinterval 10
checkbutton .chk -text "Occupied" -variable occupied
#Gender
frame .gender
label .lbl_gender -text "Sex "
radiobutton .gender.rdb_m -text "Male" -variable gender -value "Male"
radiobutton .gender.rdb_f -text "Female" -variable gender -value "Female"
.gender.rdb_m select
button .but -text "Push Me" -command "push_button"
#Text Area
frame .textarea
text .txt -yscrollcommand ".srl_y set" -xscrollcommand ".srl_x set" \
-width 40 -height 10
scrollbar .srl_y -command ".txt yview" -orient v
scrollbar .srl_x -command ".txt xview" -orient h
#Geometry Management
grid .frm_name -in . -row 1 -column 1 -columnspan 2
grid .lab -in .frm_name -row 1 -column 1
grid .ent -in .frm_name -row 1 -column 2
grid .scl -in . -row 2 -column 1
grid .chk -in . -row 2 -column 2
grid .gender -in . -row 3 -column 2
grid .lbl_gender -in .gender -row 1 -column 1
grid .gender.rdb_m -in .gender -row 1 -column 2
grid .gender.rdb_f -in .gender -row 1 -column 3
grid .but -in . -row 4 -column 1 -columnspan 2
grid .txt -in .textarea -row 1 -column 1
grid .srl_y -in .textarea -row 1 -column 2 -sticky ns
grid .srl_x -in .textarea -row 2 -column 1 -sticky ew
grid .textarea -in . -row 5 -column 1 -columnspan 2