Tcl/Tk:抽象菜单按钮和命令的创建:无法调用命令

发布于 2024-11-24 09:06:27 字数 1559 浏览 0 评论 0原文

我试图将菜单按钮和与其关联的函数抽象为单个 proc 调用(下面的 addMenus 函数)。以下代码正确构建了菜单按钮,但在按下按钮(例如“打开”)时,它会出错:

错误:无效命令名“myputs Open”

  • 我认为我没有正确使用引号。有关解决此问题的任何指示吗?
  • 还有关于改进代码的建议,特别是如果我想将参数传递给 menubuttonmenu 命令?
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 or menu 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 技术交流群。

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

发布评论

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

评论(2

伴梦长久 2024-12-01 09:06:27

该命令是一个在回调时评估的脚本。您的代码将“打开”菜单项的命令回调设置为 [list myputs "Open"],如果您在 shell 中输入,则会给出相同的错误消息。

在像这样的小部件回调中使用 [list] 通常是很好的做法,因为它使您无需在纯字符串中进行各种反斜杠转义。但在这里,这是没有必要的。 myargs 可以简单地表示

....
File {
"Open ..."     { myputs "Open" }
"New  ..."     { myputs "New" }
....

如果您希望命令包含一些要扩展的变量或要运行的命令,那么在某个时候您需要在正确的时间和正确的范围内导致该扩展发生。例如,如果您的菜单定义类似于

File {
"Open ..."     { myputs [getString Open] }
"New  ..."     { myputs [getString New] }
}

其中 getString 是返回字符串的命令,那么您可以将其作为菜单添加行

    $mdropoff add command -label $label -command [uplevel #0 subst $command]

具体如何执行此操作取决于变量类型您想要传递(它们是本地的、命名空间的还是全局的)以及何时应该展开它们(您希望它们在定义菜单时展开还是在调用菜单时展开?)

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 simply

....
File {
"Open ..."     { myputs "Open" }
"New  ..."     { myputs "New" }
....

If 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

File {
"Open ..."     { myputs [getString Open] }
"New  ..."     { myputs [getString New] }
}

where getString is some command to return a string, then you could put as your menu add line

    $mdropoff add command -label $label -command [uplevel #0 subst $command]

The 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?)

春夜浅 2024-12-01 09:06:27

问题恰恰在于 Tcl 不会对嵌套在此类数据结构中的脚本进行任何扩展(它不能;除非您告诉它,否则它不知道它们是什么)。有几种处理它的可能性:

  1. 编写不需要它的代码(在数据中使用普通的 myputs "Open" 而不是 [list myputs "Open"] )。
  2. 始终使用 list 构建数据:

    set myargs [list [list "文件" \
        [列表“打开...”[列表myputs“打开”]] \
        [列表“新...”[列表myputs“新”]] \
        [列表“保存...”[列表myputs“保存”]] \
        [列表“另存为...”[列表myputs“另存为”]] \
    ] [列表“编辑”\
        ...
    

    好吧,这会给你带来反斜杠(或很长的行)。

  3. uplevelsubst 使用一些技巧。从addMenus内部...

    foreach { 标签命令 } $options {
      设置命令 [uplevel 1 [list subst $command]]
      $mdropoff 添加命令 -label $label -command $command
    }
    

    这将使您的代码看起来像您所期望的那样(并扩展调用上下文中的任何嵌入变量,这通常是您想要的;如果您从不在菜单描述中使用变量 - 或复杂的命名空间处理 - 您可以使用更简单的设置命令[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:

  1. Write your code to not need it (using plain myputs "Open" instead of [list myputs "Open"] in your data).
  2. Construct your data with list throughout:

    set myargs [list [list "File" \
        [list "Open ..." [list myputs "Open"]] \
        [list "New ..." [list myputs "New"]] \
        [list "Save ..." [list myputs "Save"]] \
        [list "Save As ..." [list myputs "Save As"]] \
    ] [list "Edit" \
        ...
    

    OK, it's going to give you backslashitis (or very long lines).

  3. Use a bit of trickery with uplevel and subst. From inside addMenus

    foreach { label command } $options {
      set command [uplevel 1 [list subst $command]]
      $mdropoff add command -label $label -command $command
    }
    

    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.

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