我試圖設置一系列的檢查按鈕,點擊後將這些按鈕的變量值設置爲1. 運行按鈕只會運行檢查按鈕被選中。 單擊按鈕時,全局變量值例如:check_H1不會更新。TCL:單擊檢查按鈕後變量值未更新
#!/usr/local/bin/wish
package require Tk
frame .top -width 50 -height 30 -borderwidth 5 -padx 5 -pady 5 -relief raised
checkbutton .top.c1 -text H1 -variable "check_H1" -command {set_h1}
checkbutton .top.c2 -text H2 -variable "check_H2" -command {set_h2}
checkbutton .top.c3 -text H3 -variable "check_H3" -command {set_h3}
checkbutton .top.c4 -text H4 -variable "check_H4" -command {set_h4}
checkbutton .top.c5 -text H5 -variable "check_H5" -command {set_h5}
checkbutton .top.c6 -text H6 -variable "check_H6" -command {set_h6}
button .top.b1 -text "RUN" -command [list select $check_H1 $check_H2 $check_H3 $check_H4 $check_H5 $check_H6]
grid .top
grid .top.c1 -row 2 -column 2
grid .top.c2 -row 2 -column 3
grid .top.c3 -row 3 -column 2
grid .top.c4 -row 3 -column 3
grid .top.c5 -row 4 -column 2
grid .top.c6 -row 4 -column 3
grid .top.b1 -row 5 -column 5
proc select {check_H1 check_H2 check_H3 check_H4 check_H5 check_H6} {
#upvar check_H1 check_H2 check_H3 sa3 check_H4 sa4 check_H5 sa5 check_H6 sa6
puts "Value of H1 is $check_H1\n"
puts $check_H2
puts $check_H3
puts $check_H4
puts $check_H5
puts $check_H6
if {$check_H1 == 1} {
run_h1
}
if {$check_H2 ==1} {
run_h2
}
if {$check_H3 ==1} {
run_h3
}
if {$check_H4 ==1} {
run_h4
}
if {$check_H5 ==1} {
run_h5
}
if {$check_H6 ==1} {
run_h6
}
}
proc set_h1 {} {
global check_H1
set check_H1 1
puts $check_H1
puts "H1 is set\n"
}
proc set_h2 {} {
global check_H2
set check_H2 1
puts "H2 is set\n"
}
proc set_h3 {} {
global check_H3
set check_H3 1
puts "H3 is set\n"
}
proc set_h4 {} {
set check_H4 1
puts "H4 is set\n"
}
proc set_h5 {} {
set check_H5 1
puts "H5 is set\n"
}
proc set_h6 {} {
set check_H6 1
puts "H6 is set\n"
}
proc run_h1 {} {
global check_H1
puts "this loop is for H1\n"
}
proc run_h2 {} {
global check_H2
puts "this loop is for H2\n"
}
如select所示,每個變量的視在值固定爲按鈕時變量的值。 top.b1已創建。 –
另外,您不需要編寫回調來設置checkbutton變量的值。該變量將被更新以反映按鈕的狀態。 (除非你想做它做的事:讓按鈕無法取消選擇,最好在這種情況下禁用按鈕。) –
爲什麼所有不同的'set_' *過程都不同? –