我想抽取我的菜單按鈕和與之相關的功能給一個單獨的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