Tcl/Tk:抽象菜单按钮和命令的创建:无法调用命令
我试图将菜单按钮和与其关联的函数抽象为单个 proc
调用(下面的 addMenus
函数)。以下代码正确构建了菜单按钮,但在按下按钮(例如“打开”)时,它会出错:
错误:无效命令名“myputs Open”
- 我认为我没有正确使用引号。有关解决此问题的任何指示吗?
- 还有关于改进代码的建议,特别是如果我想将参数传递给
menubutton
或menu
命令?
proc myputs { label } {
puts $label
}
proc addMenus { mbar myargs } {
foreach { arg } $myargs {
foreach { button options } $arg {
set x ${mbar}.[string tolower ${button}]
set y ${x}.menu
menubutton $x -text $button -menu $y
pack $x -side left
set mdropoff [menu $y -tearoff 0]
foreach { label command } $options {
$mdropoff add command -label $label -command $command
}
}
}
}
#----------------------------------------
# main script
#----------------------------------------
wm title . "My Gui"
# build the frame which contains menu options
set mbar .mbar
frame $mbar -relief raised -bd 2
pack $mbar -side top -fill x
# text box as a filler
text .myout -width 40 -height 20
pack .myout -side top -fill both -expand true
# file menu
set myargs {
{
File {
"Open ..." { [list myputs "Open"] }
"New ..." { [list myputs "New"] }
"Save ..." { [list myputs "Save"] }
"Save As ..." { [list myputs "Save As"] }
}
}
{
Edit {
"Cut" { [list myputs "Cut"] }
"Copy" { [list myputs "Copy"] }
"Paste" { [list myputs "Paste"] }
}
}
}
addMenus $mbar $myargs
I am trying to abstract my menu buttons and functions associated with it to a single proc
call (addMenus
function below). The following code builds the menu buttons correctly but on pressing the buttons (say Open) it errors out as:
Error: invalid command name "myputs Open"
- I think I am not using the quotes properly. Any pointers on fixing this?
- Also any suggestions on improving the code especially if I want to pass arguments to
menubutton
ormenu
command?
proc myputs { label } {
puts $label
}
proc addMenus { mbar myargs } {
foreach { arg } $myargs {
foreach { button options } $arg {
set x ${mbar}.[string tolower ${button}]
set y ${x}.menu
menubutton $x -text $button -menu $y
pack $x -side left
set mdropoff [menu $y -tearoff 0]
foreach { label command } $options {
$mdropoff add command -label $label -command $command
}
}
}
}
#----------------------------------------
# main script
#----------------------------------------
wm title . "My Gui"
# build the frame which contains menu options
set mbar .mbar
frame $mbar -relief raised -bd 2
pack $mbar -side top -fill x
# text box as a filler
text .myout -width 40 -height 20
pack .myout -side top -fill both -expand true
# file menu
set myargs {
{
File {
"Open ..." { [list myputs "Open"] }
"New ..." { [list myputs "New"] }
"Save ..." { [list myputs "Save"] }
"Save As ..." { [list myputs "Save As"] }
}
}
{
Edit {
"Cut" { [list myputs "Cut"] }
"Copy" { [list myputs "Copy"] }
"Paste" { [list myputs "Paste"] }
}
}
}
addMenus $mbar $myargs
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
该命令是一个在回调时评估的脚本。您的代码将“打开”菜单项的命令回调设置为
[list myputs "Open"]
,如果您在 shell 中输入,则会给出相同的错误消息。在像这样的小部件回调中使用
[list]
通常是很好的做法,因为它使您无需在纯字符串中进行各种反斜杠转义。但在这里,这是没有必要的。 myargs 可以简单地表示如果您希望命令包含一些要扩展的变量或要运行的命令,那么在某个时候您需要在正确的时间和正确的范围内导致该扩展发生。例如,如果您的菜单定义类似于
其中
getString
是返回字符串的命令,那么您可以将其作为菜单添加行具体如何执行此操作取决于变量类型您想要传递(它们是本地的、命名空间的还是全局的)以及何时应该展开它们(您希望它们在定义菜单时展开还是在调用菜单时展开?)
The command is a script which is evaluated at callback time. Your code is setting the command callback of the "open" menu item to
[list myputs "Open"]
which, if you enter in the shell will give you the same error message.Using
[list]
in widget callbacks like this is frequently good practice, since it eaves you from needing to do all sorts of backslash escaping in a plain string. But here, it's not necessary. myargs can be simplyIf you want the command to include some variable to be expanded or command to be run, then at some point you need to cause that expansion to happen, at the right time and in the correct scope. For example, if your menu definition was something like
where
getString
is some command to return a string, then you could put as your menu add lineThe specifics of how you would do this depends on what kind of variables you want to pass (are they local, namespaced, or global) and when they should be expanded (do you want them expanded at the time your menu is defined, or when it is invoked?)
问题恰恰在于 Tcl 不会对嵌套在此类数据结构中的脚本进行任何扩展(它不能;除非您告诉它,否则它不知道它们是什么)。有几种处理它的可能性:
myputs "Open"
而不是[list myputs "Open"]
)。始终使用
list
构建数据:好吧,这会给你带来反斜杠(或很长的行)。
对
uplevel
和subst
使用一些技巧。从addMenus
内部...这将使您的代码看起来像您所期望的那样(并扩展调用上下文中的任何嵌入变量,这通常是您想要的;如果您从不在菜单描述中使用变量 - 或复杂的命名空间处理 - 您可以使用更简单的
设置命令[subst $command]
)。但是,当您从简单的调用转向更加基于模板的调用时,它比您之前使用的任何方法都要复杂得多。如果您希望一次进行一些替换,另一次进行其他替换,那么是时候使用辅助程序了:您的大脑(以及维护代码的任何人)都会感谢您。
The issue is exactly that Tcl doesn't do any expansion of scripts nested inside data structures like that (it can't; it doesn't know what they are until you tell it). There are a few possibilities for dealing with it:
myputs "Open"
instead of[list myputs "Open"]
in your data).Construct your data with
list
throughout:OK, it's going to give you backslashitis (or very long lines).
Use a bit of trickery with
uplevel
andsubst
. From insideaddMenus
…That'll make your code otherwise look like what you expect (and expand any embedded variables in the calling context, which is usually what you want; if you never use variables in the menu description – or complex namespace handling – you can use a simpler
set command [subst $command]
). But it is significantly trickier than anything you had before as you're moving from simple calls to something more template-based.And if you're wanting some substitutions to happen at one time and others at another, it's time to use helper procedures: your brain (and anyone maintaining the code) will thank you.