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!

How to save the contents of an entry widget

Status
Not open for further replies.

BiggerT

Programmer
Jan 20, 2004
10
0
0
NL
Does sombody know how to save the contents of an entry widget. I can save the contets of a text widget but with an entry widget it doesn't work.

This is how I do it with a text widget:

text .branche.t -wrap none
button .branche.menubalk.save -text "save" -command {save}

proc save { } {
set str "branche"
set filename [concat [string range $str 0 2][clock format [clock seconds] -format %d%m%y]]
set data [.branche.t 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 "C:\\usr\\local\\Temp\\$::uniek" -initialfile $filename -defaultextension .txt]
wm title . $filename
set fileid [open $filename w]
puts -nonewline $fileid $data
close $fileid
}
 
OK. What you're doing in your save proc is "scraping" the text widget from position 1.0 to the end minus 1 character. For an entry the task is, comparitively, trivial. When you build your entry, you should assign it a "textvariable" (entry .yourNameHere -textvariable yourVar). Then, whatever is entered in the entry is the value of "yourVar". You can puts $yourVar just like you did $data.

Bob Rashkin
rrashkin@csc.com
 
An alternative to excellent Bong's solution when you don't have the -textvariable option is:
Code:
  set value [.entry get]
HTH

ulis
 
Sorry but it doesn't work. This is the script I'm using, try to run it if you want:

proc datumveld {} {
toplevel .datumvak
entry .datumvak.datum -textvariable var
button .datumvak.bevestig -text "dit is goed" -command save
pack .datumvak.datum
pack .datumvak.bevestig
proc save { } {
set str "branche"
set filename [concat [string range $str 0 2] [clock format [clock seconds] -format %d%m%y]]
set file_types {
{"Text Files" { .txt .TXT} }
{"Tcl Files" { .tcl .TCL .tk .TK} }
{"All Files" * }
}
set filename [tk_getSaveFile -filetypes $file_types -initialdir "H:\\usr\\local\\temp\\tryout" -initialfile $filename -defaultextension .txt]
wm title . $filename
set fileid [open $filename w]
puts -nonewline $fileid $var
close $fileid
}
}

I'm sourcing it from the menu I've mentioned before. I've tried to get it working whole day but without succes :(

The message I'm getting is: error: can't read "var": no such variable

Hopefully you want to help me (again)
 
I'm not exactly sure about how scope works in tcl/tk. But my suggestion to you is to try declaring 'var' as global in the "save" function.

yup, just tested that, the scope of your textvariable doesnt reach the inside function. Just declase var as global in the inner proc.


good luck
 
Thank you, sometimes I can be a real stupid idiot. It's great that there are people on this earth who do understand how things work!

Yes it works!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top