[英]TCL/TK How to generate comboboxs/buttons in the for loop and call the function?
我想在for循環中生成幾個組合框和按鈕,而button命令將調用該函數並檢查組合框的內容,如何獲取變量“ com $ num”並傳遞給“ get_optimizer”函數? 如何更正以下腳本? 請幫助,謝謝!
set num 1
foreach SQ {1 2 3 4 5} {
ttk::combobox $twind.frame.dpcom$num -textvariable com$num -values {Global Definitive Adaptive Cmaes}
button $twind.frame.but$num -text "Optimizer Setting" -command [list get_optimizer]
grid $twind.frame.dpcom$num -row $num -column 0
grid $twind.frame.but$num -row $num -column 1
incr num}
proc get_optimizer {} {
global [set com$num]
if {[set com$num]=='Global'} {
...
} elseif {[set com$num]=='Definitive'} {
...
} elseif {...} {
...}
...
}
您應該將變量的全名傳遞給get_optimizer
,並使用upvar #0
在過程內部指定一個固定的本地別名。
# backslash-newline for readability only
button $twind.frame.but$num -text "Optimizer Setting" \
-command [list get_optimizer com$num]
proc get_optimizer {varname} {
upvar #0 $varname theVar
if {$theVar=='Global'} {
...
} elseif {$theVar=='Definitive'} {
...
} elseif {...} {
...
}
...
}
同樣,使用eq
運算符進行字符串相等更有效。 並考慮使用數組(例如, com(1)
而不是com1
)是否更好。
采用
global com$num
(例如,給您全局com1)
代替
global [set com$num]
(例如,為您提供全局權威性)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.