Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Variables Tcl/tk按钮-如何在命令选项上传递变量?_Variables_Button_Tcl_Tk - Fatal编程技术网

Variables Tcl/tk按钮-如何在命令选项上传递变量?

Variables Tcl/tk按钮-如何在命令选项上传递变量?,variables,button,tcl,tk,Variables,Button,Tcl,Tk,我在命令选项上传递变量时遇到问题,例如: package require Tk wm withdraw . destroy .button toplevel .button # button.0: puts 0 set count 0 button .button.$count -text $count -command {puts $count} grid .button.$count -column $count -row 0 # button.1: puts 1 incr count

我在命令选项上传递变量时遇到问题,例如:

package require Tk
wm withdraw .
destroy .button
toplevel .button

# button.0: puts 0
set count 0
button .button.$count -text $count -command {puts $count}
grid .button.$count -column $count -row 0

# button.1: puts 1
incr count
button .button.$count -text $count -command {puts $count}
grid .button.$count -column $count -row 0
但是,按钮0将1而不是0。似乎当调用按钮.0时,它会获取该时刻变量的值,即1

我发现我可以使用一个过程和一个全局变量来实现期望的结果,但我想知道是否有可能不借助过程调用来实现这一点


提前感谢。

如果您想在定义回调时替换变量的当前值,则需要使用不同的引用机制:

button .button.$count -text $count -command [list puts $count]

您可以使用代码创建两个触发命令的按钮:

puts $count
在您的示例中,当您按下按钮时,变量$count等于“1”,因此“puts”并显示该值。为了正确操作,需要创建2个按钮。第一个按钮命令应为“0”。第二个按钮命令应为“puts 1”。我们必须在创建按钮时应用替换。例如:

-command [list puts $count]
-command "puts $count"

注意,对于任何真正复杂的事情,添加过程调用实际上简化了事情。