I have the following proc which changes the select file button colour depending on if the current path in the entrybox exists or not. The first if loop works, for the second I get "invalid command name "" while executing "$Button configure -background red" And I don't have a clue why...

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

}
有帮助吗?

解决方案 2

You only have 5 elements in those lists, but your loop iterates 6 times. You probably want {$n < 5} or {$n < [llength $FilePathList]}


A note about style/efficiency: You don't need to test the existance twice

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

or

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

其他提示

You should be able to replace the loop with this construct:

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

Writing it this way means you don't need to keep track of the number of items. However, if any of the lists should ever have more items than the other, the other iteration variable will get empty values to match.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top