大家早,
我正在运行以下代码
foreach rs_lb {kl15_lb din1_lb din2_lb din3_lb din4_lb \
dinnc_lb ain1_lb ain2_lb ain3_lb ain4_lb a_lb \
b_lb u_lb v_lb w_lb sin_lb cos_lb th1_lb th2_lb hvil_lb} \
rs_lb_txt {KL15 DIN1 DIN2 DIN3 DIN4 DINNC AIN1 AIN2 AIN3 AIN4 \
A B U V W SIN COS TH1 TH2 HVIL} \
rs_lb_rw {0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9} \
rs_lb_cm {0 0 0 0 0 0 0 0 0 0 2 2 2 2 2 2 2 2 2 2} \
rs_cb {kl15_cb din1_cb din2_cb din3_cb din4_cb dinnc_cb ain1_cb \
ain2_cb ain3_cb ain4_cb a_cb \
b_cb u_cb v_cb w_cb sin_cb cos_cb th1_cb th2_cb hvil_cb} \
rs_cb_rw {0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9} \
rs_cb_cm {1 1 1 1 1 1 1 1 1 1 3 3 3 3 3 3 3 3 3 3} \
ds_out_i {0 0 2 0 4 0 6 0 8 0 10 0 12 0 14 0 16 0 18 0 20 0 \
22 0 24 0 26 0 28 0 30 0 32 0 34 0 36 0 38 0} \
{
label .rs.$rs_lb -text "$rs_lb_txt"
checkbutton .rs.$rs_cb -variable $rs_cb -command {
if {$rs_cb} {
set ds_out($ds_out_i) 1
set ds_out([expr $ds_out_i + 1]) 1
} else {
set ds_out($ds_out_i) 0
set ds_out([expr $ds_out_i + 1]) 0
}
}
grid .rs.$rs_lb -row $rs_lb_rw -column $rs_lb_cm
grid .rs.$rs_cb -row $rs_cb_rw -column $rs_cb_cm
}
我收到错误:
窗口名称“”已存在于父级中
当我点击一个复选框时,我收到了应用程序错误:
预期的布尔值,但得到“”预期的布尔值,但在执行“if {$ rs_cb} {set ds_out($ ds_out_i)1 set ds_out([expr $ ds_out_i + 1])时得到”“否则{set ds_out($ ds_out_i) )0 set ds_out([expr $ ds_out_i + ...“从”。上级#0 [list $ w invoke]“中调用的”.rs.u_cb invoke“(”uplevel“body line 1)中调用)(程序”tk“ :: ButtonUp“第24行”从“tk :: ButtonUp .rs.u_cb”(绑定到事件的命令)中调用
谁能告诉我为什么会这样呢?
window name "" already exists in parent
是因为喂食ds_out_i
的列表比喂养所有其他变量的列表更长(两倍长!); foreach
继续前进,直到它遍历每个项目的所有元素,将列表已耗尽的变量分配给空字符串。我猜你要为目前正在提供ds_out_i
的列表使用双变量列表迭代,或者更有可能迭代一对对列表。 (注意,这会使你的回调中的代码多一点;我马上就会解决这个问题。)
…
ds_out_i {{0 0} {2 0} {4 0} {6 0} {8 0} {10 0} {12 0} {14 0} {16 0} {18 0} {20 0} \
{22 0} {24 0} {26 0} {28 0} {30 0} {32 0} {34 0} {36 0} {38 0}} \
…
您的另一个错误是因为您当前正在为-command
选项使用复杂的嵌入式脚本(对checkbutton
)。不要那样做!这真的很难合作。强烈推荐的方法是制作一个小帮助程序,然后使-command
选项成为一个简单的脚本,调用该程序并使用list
命令生成。
我们的意思是。如果我能把它变成闪烁的横幅,我愿意。这是非常困难的,很容易出错。使用帮助程序非常容易。这可能是你的看法。
proc rs_checkbutton_callback {varname index} {
# "parse" the arguments
global ds_out; # We're working with a global variable here
upvar "#0" $varname rs_cb; # Alias the named global variable as rs_cb
lassign $index a b; # This splits the two-part ds_out_i into two variables, a and b
if {$rs_cb} {
set ds_out($a) 1
set ds_out($b) 1
} else {
set ds_out($a) 0
set ds_out($b) 0
}
}
…
checkbutton .rs.$rs_cb -variable $rs_cb \
-command [list rs_checkbutton_callback $rs_cb $ds_out_i]
我不确定该回调程序是否正在执行您真正想要的操作。相反,最好还是做得更好:
proc rs_checkbutton_callback {varname index} {
# "parse" the arguments
global ds_out; # We're working with a global variable here
upvar "#0" $varname rs_cb; # Alias the named global variable as rs_cb
lassign $index a b; # This splits the two-part ds_out_i into two variables, a and b
if {$rs_cb} {
set ds_out([list $a $b]) 1
set ds_out([list $a [expr {$b + 1}]) 1
} else {
set ds_out([list $a $b]) 0
set ds_out([list $a [expr {$b + 1}]) 0
}
}
一个很好的编程原则是Occam的Razor,即“实体不得在没有必要的情况下成倍增加”。您可以从简单的名称列表中创建窗口小部件路径和复选框标签:
foreach name {foo bar baz} {
lappend lbnames ${name}_lb
lappend cbnames ${name}_cb
lappend texts [string toupper $name]
}
list $lbnames $cbnames $texts
# => {foo_lb bar_lb baz_lb} {foo_cb bar_cb baz_cb} {FOO BAR BAZ}
在这种情况下,最好将名称列表分成两个相等的部分:
set names1 {
kl15 din1 din2 din3 din4 dinnc ain1 ain2 ain3 ain4
}
set names2 {
a b u v w sin cos th1 th2 hvil
}
然后我们就可以开始创建小部件了。不幸的是,我无法弄清楚你希望命令如何工作,但是在这里集成回调调用可能也很容易。由于我们并行创建小部件列,因此我们不需要跟踪grid
行和列:
toplevel .rs
foreach name1 $names1 name2 $names2 {
grid \
[label .rs.${name1}_lb -text [string toupper $name1]] \
[checkbutton .rs.${name1}_cb -variable ${name1}_cb -command [list ...]] \
[label .rs.${name2}_lb -text [string toupper $name2]] \
[checkbutton .rs.${name2}_cb -variable ${name2}_cb -command [list ...]]
}
这样,设置小部件数组变得更加容易,代码也更加清晰。
顺便说一下,你知道你可以在checkbutton
小部件中设置标签吗?
foreach name1 $names1 name2 $names2 {
grid \
[checkbutton .rs.${name1}_cb -text [string toupper $name1] -variable ${name1}_cb -command [list ...]] \
[checkbutton .rs.${name2}_cb -text [string toupper $name2] -variable ${name2}_cb -command [list ...]] \
-sticky w
}