假设我们有一些变量x,最初是x=0。
每当x将其值从0更改为1时,单击某个按钮A
每当x将其值从1更改为2时,单击某个按钮B
每当x将其值从2更改为0时,单击某个按钮C
..。
怎么做?
发布于 2020-11-23 10:05:20
下面是一个简单的示例,它每秒更新一次x,并在更新时使用变量上的trace调用按钮按下,具体按钮取决于新值。
#!/usr/bin/env wish
grid [ttk::button .a -text A -command {puts "Buttom A pushed"} -state active]
grid [ttk::button .b -text B -command {puts "Button B pushed"}]
grid [ttk::button .c -text C -command {puts "Button C pushed"}]
set x 0
proc update_gui {name _ _} {
upvar $name v
switch -- $v {
0 { .c state !active; .a state active; .a invoke }
1 { .a state !active; .b state active; .b invoke }
2 { .b state !active; .c state active; .c invoke }
}
}
proc update_x {} {
variable x
set x [expr {($x + 1) % 3}]
after 1000 update_x
}
trace add variable x write update_gui
after 1000 update_xhttps://stackoverflow.com/questions/64965887
复制相似问题