Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Tk "hello, world", part 2

by RandomWalk (Beadle)
on Jan 29, 2004 at 20:32 UTC ( [id://325027]=perlquestion: print w/replies, xml ) Need Help??

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

Dear Monks,

I can run hello.pl as in Tk Tutorial but am having a little trouble with the next step. For instance, I would like to get input from an Entry widget instead of STDIN. I thought this should work but I'm not getting assigned as I expected:

my $main = MainWindow->new(); $main->Label(-text=>"Company Name:")->pack(); my $entry = $main->Entry()->pack; $entry->focus(); my $name = $entry->get(); $main->Button(-text=>"Continue", -command=> sub {$main->destroy})->pac +k(); $main->Button(-text=>"Cancel", -command => sub {exit})->pack(); $main->bind('<Return>' => sub {$main->destroy}); MainLoop; die "$name";
Results only in

$ perl new_invoice.pl Died at new_invoice.pl line 38.

I guess when this works I'll try to make it more attractive with fonts and geometry management, and then maybe continue with the tutorial or "Mastering Perl/Tk".

I would appreciate any guidance. Thanks.

Replies are listed 'Best First'.
Re: Tk "hello, world", part 2
by Rhose (Priest) on Jan 29, 2004 at 20:46 UTC
    You could also use -textvariable with your Entry widget.
    #!/usr/bin/perl use strict; use warnings; use Tk; my $name; my $main = MainWindow->new(); $main->Label(-text=>"Company Name:")->pack(); my $entry = $main->Entry(-textvariable => \$name)->pack; $entry->focus(); $main->Button(-text=>"Continue", -command=> sub {$main->destroy;})->pa +ck(); $main->Button(-text=>"Cancel", -command => sub {exit})->pack(); $main->bind('<Return>' => sub {$main->destroy}); MainLoop; die "$name";

    Update
    Oops... I forgot to put in the reason for the first code not working -- the problem is that the "$name = $entry->get();" line is being executed before there is a value in the Entry widget (You can't use the interface before the MainLoop;)

    Here is a "fix" for the first example:

    #!/usr/bin/perl use strict; use warnings; use Tk; my $name; my $main = MainWindow->new(); $main->Label(-text=>"Company Name:")->pack(); my $entry = $main->Entry()->pack; $entry->focus(); $main->Button(-text=>"Continue", -command=> sub {$name = $entry->get() +;$main->destroy;})->pack(); $main->Button(-text=>"Cancel", -command => sub {exit})->pack(); $main->bind('<Return>' => sub {$main->destroy}); MainLoop; die "$name";

    Update 2
    Yeah, what arden said. *Smiles*

Re: Tk "hello, world", part 2
by arden (Curate) on Jan 29, 2004 at 20:51 UTC
    I prefer using -textvariable as Rhose++ pointed out, but if you really want to use ->get(), it needs to happen AFTER something is put into the Entry. Try this:

    my $name; my $main = MainWindow->new(); $main->Label(-text=>"Company Name:")->pack(); my $entry = $main->Entry()->pack; $entry->focus(); $main->Button(-text=>"Continue", -command=> sub {$name = $entry->get() +; $main->destroy();})->pack(); $main->Button(-text=>"Cancel", -command => sub {exit})->pack(); $main->bind('<Return>' => sub {$main->destroy}); MainLoop; die "$name";

    Also, next time, if you're going to include the error perl gave you, include all of the lines leading up to it. You can't have an error at line 38 of a 10 line script. . . :)

      Thank you both.

      I guess I was using the get method as a way to see how MainLoop works. (Plus for a beginner there is comfort in having assignments isolated, explicit, and reference-free--but I'll just have to get used to the new syntax!)

      So it looks like I can't call the "get" method just once if I am to assign the entry text both upon button and return press. Right?

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://325027]
Approved by b10m
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (6)
As of 2024-04-25 08:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found