2016-10-21 57 views
0

我試圖設置一系列的檢查按鈕,點擊後將這些按鈕的變量值設置爲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" 
} 
+1

如select所示,每個變量的視在值固定爲按鈕時變量的值。 top.b1已創建。 –

+1

另外,您不需要編寫回調來設置checkbutton變量的值。該變量將被更新以反映按鈕的狀態。 (除非你想做它做的事:讓按鈕無法取消選擇,最好在這種情況下禁用按鈕。) –

+1

爲什麼所有不同的'set_' *過程都不同? –

回答

2

當運行該腳本時,感覺很多事情不起作用,因爲我認爲它是有意的。我改變了一些東西,並從按鈕命令中刪除了變量,因爲變量被傳遞給要在開始時運行的命令,而不是在按下按鈕時運行。

我也刪除了所有set check_HX 1,因爲他們強制檢查按鈕爲單一狀態。也就是說,我沒有觸及任何其他功能。

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 select 
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 {} { 
    global check_H1 check_H2 check_H3 check_H4 check_H5 check_H6 
    puts "Value of H1 is $check_H1" 
    puts "Value of H2 is $check_H2" 
    puts "Value of H3 is $check_H3" 
    puts "Value of H4 is $check_H4" 
    puts "Value of H5 is $check_H5" 
    puts "Value of H6 is $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 
    puts "H1 is set to $check_H1" 
} 

proc set_h2 {} { 
    global check_H2 
    puts "H2 is set to $check_H2" 
} 

proc set_h3 {} { 
    global check_H3 
    puts "H3 is set to $check_H3" 
} 

proc set_h4 {} { 
    global check_H4 
    puts "H4 is set to $check_H4" 
} 

proc set_h5 {} { 
    global check_H5 
    puts "H5 is set to $check_H5" 
} 

proc set_h6 {} { 
    global check_H6 
    puts "H6 is set to $check_H6" 
} 

proc run_h1 {} { 
    global check_H1 
    puts "This loop is for H1 $check_H1" 
} 

proc run_h2 {} { 
    global check_H2 
    puts "this loop is for H2 $check_H2" 
} 

proc run_h3 {} { 
    global check_H3 
    puts "this loop is for H3 $check_H3" 
} 

proc run_h4 {} { 
    global check_H4 
    puts "this loop is for H4 $check_H4" 
} 

proc run_h5 {} { 
    global check_H5 
    puts "this loop is for H5 $check_H5" 
} 

proc run_h6 {} { 
    global check_H6 
    puts "this loop is for H6 $check_H6" 
} 
+0

它看起來主要的變化是運行按鈕和選擇過程,是嗎? –

+0

是的,謝謝傑裏和格倫,我最近意識到這個列表不再需要,並且我能夠運行的變化很少,我明白我不需要,所以可能set_proc,但是我可以刪除checkbuttons的-command選項,如果是的話,那麼我不需要那些可能set_ * procs,我只能查詢變量 –

+0

@glennjackman是的,那些和proc中的干擾'set'。 – Jerry