2014-01-23 12 views
0

我有以下proc會根據輸入框中當前路徑是否存在來改變選擇文件按鈕的顏色。第一,如果環路工程,第二個在執行‘$按鈕來配置-background紅’,我沒有線索,爲什麼...Tcl configure - 按鈕失效的後臺

proc ::CheckGUIPaths { } { 

set FilePathList [list $::GUI_DB_path $::GUI_BDF_path $::GUI_ALLOW_path $::GUI_EXCEL_path $::GUI_HM_path] 
set Buttons [list .dsm.nb.f1.btn_DBfile .dsm.nb.f1.btn_BDFfile .dsm.nb.f1.btn_ALLOWfile .dsm.nb.f1.btn_HMfile .dsm.nb.f1.btn_XLfile] 

for { set n 0 } { $n <= 5 } { incr n } { 

    set Path [lindex $FilePathList $n] 
    set Button [lindex $Buttons $n] 

    if { [ file exists $Path ] == 1 } { 
     $Button configure -background green 
     }  
    if { [ file exists $Path ] == 0 } { 
     $Button configure -background red 
     } 
    } 
return 0 

} 
+0

這些是什麼樣的按鈕?他們是[按鈕](http://tcl.tk/man/tcl8.5/TkCmd/button.htm)s還是[ttk :: button](http://tcl.tk/man/tcl8.5/TkCmd) /ttk_button.htm)s? –

+0

只是簡單的按鈕,而不是ttk :: – Lumpi

回答

1

我得到「無效的命令名稱‘’你只有5 。在這些列表元素,但你的循環迭代6次你可能想{$n < 5}{$n < [llength $FilePathList]}


大約風格/效率的說明:你並不需要測試所有腦幹兩次

if {[ file exists $Path ]} { 
    $Button configure -background green 
} else { 
    $Button configure -background red 
} 

$Button configure -background [expr {[ file exists $Path ] ? "green" : "red"}] 
+0

哎呀!愚蠢的錯誤。 – Lumpi

+1

對於增加的黑客能力,'[lindex {red green} [文件存在$ path]]' –

+0

@Donal,+1爲混淆,-1爲混淆! –

2

你應該能夠與這個結構來代替循環:

foreach Path $FilePathList Button $Buttons { 
    if {[file exists $Path]} { 
     $Button configure -background green 
    } else { 
     $Button configure -background green 
    } 
} 

寫作這種方式意味着你不需要跟蹤項目的數量。但是,如果任何列表中的項目應該比其他項目多,則其他迭代變量將獲得空值以匹配。