mld has asked for the wisdom of the Perl Monks concerning the following question:

I want to get the value entered by the user in the "Entry" fiels desinged using Tk module, and pass as param to another script file.

As a first step I tried to get the value and print the value. It is not functioning properly.

Please find the code :
my $input='Enter the site address..';
my $mw = new MainWindow;
my $frm_name= $mw ->Frame()->pack();
my $label =$frm_name -> Label(-text=>"Site Address") -> pack();
my $txt =$frm_name ->Entry()->pack();
my $button = $frm_name -> Button(-text => "Quit", -command => sub{ $inp=$txt->get();
print $inp; })->pack();
my $txt_area= $mw ->Text(-width=>10,-height=>5)->pack();
MainLoop;

Replies are listed 'Best First'.
Re: Retrieving the value of entry fiels
by anna_black (Sexton) on Mar 31, 2009 at 06:38 UTC

    What exactly doesn't function properly? I had to add my before $inp=$txt->get(); to make the script work with use strict, but afterwards, pressing Quit printed out the value I entered in the Entry...

    Of course, if you actually mean the button to quit after printing the text, you should add an exit command in the subroutine... :)

Re: Retrieving the value of entry fiels
by Ish (Acolyte) on Mar 31, 2009 at 06:47 UTC
    This prints the value on STDOUT - just added my $inp and () after MainLoop.
    use strict; use Tk; my $inp; my $input='Enter the site address..'; my $mw = new MainWindow; my $frm_name= $mw ->Frame()->pack(); my $label =$frm_name -> Label(-text=>"Site Address") -> pack(); my $txt =$frm_name ->Entry()->pack(); my $button = $frm_name -> Button(-text => "Quit", -command => sub{ $in +p=$txt->get(); print $inp; })->pack(); my $txt_area= $mw ->Text(-width=>10,-height=>5)->pack(); MainLoop();
    Hope that helps. Update I see someone else is up early... and agree that if you want "quit" to quit then you need to add that as well.