1
我期待建立一个功能近期开启文件添加到菜单和命令(事件)时,点击打开它,但一个错误出现错误无法读取“文件名”:没有这样的变量
错误:
Error can't read "filename" : no such variable
代码:
proc add_Recentfiles {filename} {
global recentFiles
...
$m insert $recentFiles(index) \
command -label $labelText -command {fileOpen $filename}
...
}
proc fileOpen { filename } {
#refresh textbox
.textarea.txt delete 0.0 end
set fileid [open $filename r]
set data [read $fileid]
.textarea.txt insert end $data
.textarea.l configure -text $filename
addRecentFile $filename
close $fileid
}
谢谢,你能解释一下为什么我们需要[list ..]使它工作吗? –
嗯,不完全对吧?该命令在全局范围内进行评估,所以第一种情况下的问题是没有这样的全局变量。如果存在的话,它的价值将被使用。在第二种情况下,命令调用已经有了替代变量,所以它就像在全局范围中一样被执行。 –