我是TCL中的一个完整的菜鸟,我确信这个问题非常简单,但因为我在试图找到答案时感到困惑,所以我决定在这里问一下。所以,对不起,如果答案真的很明显!
我手动创建了9个按钮,如下所示:
set fr1 [frame $ttt_w.fr1]
button $fr1.one -text "one" -background green
button $fr1.two -text "two" -background green
button $fr1.three -text "three" -background green
现在我想将这9个按钮添加到2D矩阵,列表或数组中,并在for循环中配置它们的属性。例如,改变他们的背景颜色。因此,lst(1,1)
代表按钮1,lst(1,1).background
允许我改变它。
我怎样才能实现这个目标?
由于
答案 0 :(得分:2)
您可以使用关联数组来保存小部件名称。
set lst(1,1) [button $fr1.one -text "one" -background green]
set lst(1,2) [button $fr1.two -text "dos" -background green]
set lst(2,1) [button $fr1.three -text "drei" -background green]
set lst(2,2) [button $fr1.four -text "quattro" -background green]
现在让我们安排一下:
foreach y {1 2} {
foreach x {1 2} {
# There's many ways to do this...
grid $lst($x,$y) -x $x -y $y
}
}
好的,现在让我们配置其中一个
# The *old* classic Tk color from waaaaay back when...
$lst(2,1) configure -background bisque
使lst(2,1).background
直接用于配置上述的背景是可能的,但我真的不会建议它,因为我上面写的内容是可用的。如果你坚持这样做,你可以这样做:
proc lst(2,1).background {{color "\u0000"}} {
global lst
if {$color eq "\u0000"} {
# Illegal sentinel value, so we read instead
return [$lst(2,1) cget -background]
}
$lst(2,1) configure -background $color
}
(那是对的。那个 是一个合法的程序名称。)但这真的相当难看,需要付出很多努力才能扩展到所有事情......而且,不要......做到了,是吗?