Reputation: 573
I am looking to create a function to add the recent opening files to menu and command (event) to open it when clicking, but an error appears
Error :
Error can't read "filename" : no such variable
Code :
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
}
Upvotes: 1
Views: 1078
Reputation: 246764
Looks like you're delaying evaluation on the variable too long. Instead of
... - command {fileOpen $filename}
Try
... - command [list fileOpen $filename]
When you use {braces}
, all variable substitution is suppressed, so the command becomes the literal string fileOpen $filename
and then later you will try to open a file literally named "$filename".
Using [list fileOpen $filename]
, you allow the variable to be expanded but also ensure that, even if the filename contains spaces, you are going to pass exactly one argument to the fileOpen proc.
Upvotes: 2