I've not spent any time trying to learn the Perl port of Tk, so I can't give you any in-depth advice. However, it appears that you create the entry widget, then immediately query its value. As the user's not had the opportunity to interact with the widget, it's still empty and so you get an empty string as the return value.
Tk programming is
event-driven programming. In an event-driven application you:[ol][li]Create your interface components (widgets)[/li][li]Register
event handlers (also known as
callbacks), which are actions to perform in response to user actions[/li][li]Then finally enter the event loop (apparently
MainLoop in PerlTk)[/li][/ol]I don't know how you register event handlers in PerlTk. But you're going to need another widget in the picture to somehow indicate to your program that it's time to read the contents of the entry widget. A simple example would be to create a button widget that, when the user clicks on it, performs the action you want. With a button widget (at least in Tcl/Tk), you use its
-command attribute to register an event handler.
Here's a roughly equivalent Tcl/Tk version of your example, with a button to trigger reading the entry's contents and writing it to a file:
Code:
entry .userentry -width 30
button .b -text "Save text" -command {
if {![catch {open text.txt a} text]} {
set ask1 [.userentry get]
puts $text "User: $ask1"
close $text
}
}
pack .userentry .b
(Note that when running a Tcl/Tk application using the
wish interpreter, you don't need to explicitly enter the event loop. The
wish interpreter does so automatically once it's read and executed the entire script.) - Ken Jones, President
Avia Training and Consulting
866-TCL-HELP (866-825-4357) US Toll free
415-643-8692 Voice
415-643-8697 Fax