Tcl/Tk 绑定需要全局变量吗?
我有一个控制自动测试器的 Tcl 脚本。到目前为止,它是一个控制台程序,在命令提示符处接受用户输入。一位同事编写了一个可以通过脚本启动的 Tk GUI。我自己从来没有使用过Tk,所以我不明白很多语法。
在测试开始时,脚本必须从操作员处获取单元序列号。这是我的同事为此编写的函数:
proc GetSerialNumber {} {
global gUserInterfaceBarCode
DisplayMessage "Enter serial number:"
.c.serialnumberbox configure -state normal
focus .c.serialnumberbox
bind .c.serialnumberbox <Return> { set gUserInterfaceBarCode [.c.serialnumberbox get] }
tkwait variable gUserInterfaceBarCode
#grid forget .c.serialnumberbox
.c.serialnumberbox configure -state disabled
}
DisplayMessage
是一个简单更新 GUI 上文本标签的过程。
我不喜欢有一个全局变量 gUserInterfaceBarCode 用来保存序列号。有什么方法可以使用局部变量并让过程返回该值吗?
如果我理解正确的话,如果取出 tkwait 变量 gUserInterfaceBarCode 行,则该函数将不会阻塞,直到该变量发生变化。这是从 GUI 元素捕获用户输入的最佳方法吗?
I have a Tcl script controlling an automated tester. So far it was a console program that took user input at the command prompt. A colleague wrote a Tk GUI that can be launched by the script. I have never used Tk myself, so I don't understand a lot of the syntax.
At the beginning of the test the script must get a unit serial number from the operator. This is the function that my colleague wrote to do that:
proc GetSerialNumber {} {
global gUserInterfaceBarCode
DisplayMessage "Enter serial number:"
.c.serialnumberbox configure -state normal
focus .c.serialnumberbox
bind .c.serialnumberbox <Return> { set gUserInterfaceBarCode [.c.serialnumberbox get] }
tkwait variable gUserInterfaceBarCode
#grid forget .c.serialnumberbox
.c.serialnumberbox configure -state disabled
}
DisplayMessage
is a procedure that simply updates a text label on the GUI.
I don't like the fact that there is a global variable gUserInterfaceBarCode
that is used to hold the serial number. Is there any way to use a local variable instead and have the procedure return that value?
If I understand correctly, if the line tkwait variable gUserInterfaceBarCode
is taken out this function will not block until that variable changes. Is this the best way to capture user input from a GUI element?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
从根本上讲,您需要有一个变量来等待。不幸的是,
的代码是在与 proc 内的代码不同的上下文(全局上下文)中执行的,并且该代码无法寻址 proc 中的局部变量。然而,它本身并不需要一个全局变量——它只需要是全局可寻址的,我的意思是你可以使用命名空间变量,如果这让你感觉更好的话:
另一种选择是在返回之前显式删除
gUserInterfaceBarCode
:就其价值而言,Tk 核心实现使用命名空间方法来实现其自己的内置对话框,例如“打开文件”对话框。
Fundamentally you need to have a variable to wait on. Unfortunately, the code for the
<Return>
is executed in a different context (the global context) than the code inside your proc, and there's no way for that code to address the local variables in your proc.However, it doesn't have to a global variable, per se -- it just needs to be globally addressable, by which I mean you could use a namespace variable instead, if that makes you feel better about it:
Another alternative would be to explicitly delete
gUserInterfaceBarCode
before returning:For what it's worth, the Tk core implementation uses the namespace approach for its own built-in dialog implementations, like the "Open File" dialog box.