我想抽取我的菜单按钮和与之相关的功能给一个单独的proc
调用(addMenus
功能如下)。下面的代码生成菜单按钮正常,但在按下按钮(说开)就出现了错误为:Tcl/Tk:抽象菜单按钮和命令的创建:不能调用命令
错误:无效的命令名称“myputs打开”
- 我想我没有使用正确引用。任何关于解决这个问题的指针?
- 还有关于改进代码的任何建议,特别是如果我想传递参数到
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
感谢您的详细解释。有没有什么文章可以链接到哪个解释这个和相关材料的细节?我没有很好的处理这个变量替代。我通常被困在“引用地狱”中。 – Anand