TCL/TK如何在for循环中生成组合框/按钮并调用该函数?
我想在for循环中生成几个combobox和按钮,button命令将调用函数并检查combobox内容,如何获取变量“com$num”并传递给“get_optimizer”函数?如何更正下面的脚本?请帮忙,谢谢TCL/TK如何在for循环中生成组合框/按钮并调用该函数?,tcl,tk,Tcl,Tk,我想在for循环中生成几个combobox和按钮,button命令将调用函数并检查combobox内容,如何获取变量“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}
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 {...} {
...}
...
}
使用
(比如说,给你全球通信1)
而不是
global [set com$num]
(比如说,为您提供全局限定)您应该将变量的全名传递到get\u optimizer
,并使用upvar\0
在过程中为该变量提供一个固定的本地别名
# backslash-newline for readability only
button $twind.frame.but$num -text "Optimizer Setting" \
-command [list get_optimizer com$num]
此外,使用
eq
运算符实现字符串相等更有效。并考虑是否最好使用一个数组(即,com(1)
而不是com1
)。这样可以工作。。。但这不是发问者想要的。太好了!这正是我想要的,非常感谢!
# 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 {...} {
...
}
...
}