Tcl/Tk:无法绑定到 Escape 和 Return

发布于 2024-11-19 14:52:20 字数 733 浏览 2 评论 0原文

我无法在 Tcl/Tk 代码中绑定 EscapeReturn 键。以下代码重现了该错误。当我按 EscEnter 键时,我收到以下错误消息:

错误:无法读取“cmd”:没有这样的变量

proc OkCancel { cmd } {
  button .${cmd}.ok -text "OK" -command [list myOk $cmd ]
  button .${cmd}.cancel -text "Cancel" -command [list myCancel .$cmd]
  grid .${cmd}.ok .${cmd}.cancel -sticky e

  bind .$cmd <Return> { myOk $cmd ; break }
  bind .$cmd <Escape> { myCancel .${cmd} ; break }
}

proc myOk { cmd } {
  puts "The command name is = $cmd"
}

proc myCancel { arg } {
  destroy $arg
}

proc test { } {
  set cmd "hello"
  toplevel .$cmd
  OkCancel $cmd
}

test

I can't bind the Escape and Return keys in my Tcl/Tk code. The following code reproduces the bug. When I hit Esc or Enter key I get the following error message:

Error: can't read "cmd": no such variable

proc OkCancel { cmd } {
  button .${cmd}.ok -text "OK" -command [list myOk $cmd ]
  button .${cmd}.cancel -text "Cancel" -command [list myCancel .$cmd]
  grid .${cmd}.ok .${cmd}.cancel -sticky e

  bind .$cmd <Return> { myOk $cmd ; break }
  bind .$cmd <Escape> { myCancel .${cmd} ; break }
}

proc myOk { cmd } {
  puts "The command name is = $cmd"
}

proc myCancel { arg } {
  destroy $arg
}

proc test { } {
  set cmd "hello"
  toplevel .$cmd
  OkCancel $cmd
}

test

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我是有多爱你 2024-11-26 14:52:20

这是因为当事件触发时,绑定到它的脚本会在全局级别进行评估(根据 绑定手册)。也就是说,如果是 绑定,则脚本 myOk $cmd ;从字面上看,break 将被执行。因此,如果脚本运行时全局命名空间中不存在名为“cmd”的变量,则会触发您显示的错误。

要解决此问题,有多种方法:

  • 在创建和绑定脚本时评估 $cmd 替换。为此,只需将 {} 替换为“”以允许变量替换,也就是说,如果您编写 bind .$cmd; " myOk $cmd ; break " 绑定脚本将是 myOK hello ;将
  • 回调脚本转换为过程调用并向其传递显式参数,如下所示:bind .$cmd; [list mycallback $cmd] 然后确保您已定义接受一个参数的 mycallback 过程。通过这种方式,您可以编写通用事件处理程序,这些处理程序由绑定时所需的任何参数进行参数化。
  • 使用命名空间代码或类似工具使脚本在定义了指定变量的指定命名空间中执行。

在所有情况下都要注意,由于您的 $cmd 在某些情况下可能会扩展为奇怪的东西,因此最好保护整个脚本免受这种情况的影响 - 这就是 [list ...] 所做的第二个示例(有关详细信息,请参阅)。

That's because when an event fires, the script bound to it gets evaluated at the global level (as per the bind manual). That is, in case of your <Return> binding, the script myOk $cmd ; break will be executed, literally. Hence, if there exists no variable named "cmd" in the global namespace at the time your script runs, the error you have shown will be triggered.

To fix the problem, there are several ways:

  • Make the $cmd substitution be evaluated at the time the script is created and bound. To do this, just replace {} with "" to allow variable substitution, that is, if you'd write bind .$cmd <Return> " myOk $cmd ; break " the bound script would be myOK hello ; break
  • Turn the callback script into a procedure call and pass it explicit parameters, like this: bind .$cmd <Return> [list mycallback $cmd] and then make sure you have defined mycallback procedure accepting one parameter. This way you can write generic event handlers which are parameterized by whatever parameters you need at the time of the binding.
  • Use namespace code or a similar tool to make your script executed in a specified namespace which has the specified variable defined.

In all cases beware that since your $cmd may in certain cases expand to something odd, it's a good idea to protect the whole script from such a situation--that's what [list ...] does in the second example (see this for more info).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文