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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Getting Data from an Entry Widget

Status
Not open for further replies.

MWG

Programmer
May 15, 2002
1
IL
I realize this is a stupid question, but I'm having quite the time turning the text that was typed into an Entry Widget into a usable string for Perl. $string = $entry->get() just doesn't seem to be working for me. Here's some script:

use Tk;
$userentry = $nameframe->Entry(-width=>30);
$userentry->pack();
$ask1 = $userentry->get();
open (TEXT,"text.txt") or die;
print TEXT "User: $ask1";
close TEXT;
MainLoop;

Thanks.
 
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
 
mwg, I noticed you are using Perl/Tk, and I've run into this issue before also. It's doing pretty much what aviatraining suggested, but in Perl/Tk. You might want to try creating a temporary TextUndo widget in a new window, $window, and then using this code:

$textundo->insert('end',$entrywidget->get)->pack();
$textundo->Save("temp.txt");
$window->destroy;

From here, you can save the textudo widget to a temp file, and then open it with a filehandle. From there you can do whatever you want with the data. Keep in mind: It might be helpful to delete the temp file right afterward, using this line:

unlink "temp.txt";

I know this beats around the bush quite a bit, but it works. Hope it does for you.

Mark
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top