Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Saving the contents of a text widget

Status
Not open for further replies.

BiggerT

Programmer
Jan 20, 2004
10
0
0
NL
Hello I'm trying to automatic save the contents of a textwidget. Thisis the code I'm fighting with:

proc Vraagstukoverzicht {} {
set f [toplevel .overzicht]
frame $f.one
label $f.titel -text "Vraagstuktitel"
text $f.invoertitel -width 20 -height 1 -relief sunken
button $f.one.bevestig -text Bevestig -command {save}
button $f.one.verdwijn -text Verdwijn -command [list destroy $f]
pack $f.one -side bottom
pack $f.titel -side top
pack $f.invoertitel -fill x -expand true
pack $f.one.bevestig -side right
pack $f.one.verdwijn -side left


proc save { } {
global filename
set data [$f.invoertitel get 1.0 {end -1c}]
set file_types {
{"Text Files" { .txt .TXT} }
{"Tcl Files" { .tcl .TCL .tk .TK} }
{"All Files" * }
}
set filename [tk_getSaveFile -filetypes $file_types -initialdir pwd -initialfile $filename -defaultextension .tcl]
wm title . $filename
set fileid [open $filename w]
puts -nonewline $fileid $data
close $fileid
}

The error I'm receiving is:

Error: can't read "f": no such variable

I don't understand it I do declare F earlier in the procedure
 
I think the problem is that you set the variable, f, in the proc, Vraagstukoverzicht, then try to use it in the proc, save.

You need either to use "upvar" [This command arranges for one or more local variables in the current procedure to refer to variables in an enclosing procedure call or to global variables. Level may have any of the forms permitted for the uplevel command, and may be omitted if the first letter of the first otherVar isn't # or a digit (it defaults to 1). For each otherVar argument, upvar makes the variable by that name in the procedure frame given by level (or at global level, if level is #0) accessible in the current procedure by the name given in the corresponding myVar argument. The variable named by otherVar need not exist at the time of the call; it will be created the first time myVar is referenced, just like an ordinary variable. There must not exist a variable by the name myVar at the time upvar is invoked. MyVar is always treated as the name of a variable, not an array element. Even if the name looks like an array element, such as a(b), a regular variable is created. OtherVar may refer to a scalar variable, an array, or an array element. Upvar returns an empty string.

The upvar command simplifies the implementation of call-by-name procedure calling and also makes it easier to build new control constructs as Tcl procedures. For example, consider the following procedure:

proc add2 name {
upvar $name x
set x [expr $x+2]
}

Add2 is invoked with an argument giving the name of a variable, and it adds two to the value of that variable. Although add2 could have been implemented using uplevel instead of upvar, upvar makes it simpler for add2 to access the variable in the caller's procedure frame.]

or declar "f" as global in both procs, or pass "f" as an argument to "save".

Bob Rashkin
rrashkin@csc.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top