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

Hi Monks, I am writting a pl/tk script that would read the user input "which is a number " then generate as many entries as the number entered .. somthing like this:
my $left4 = $top->Frame(-background=>'gray', )->pack(-side=>'left',-pady=>2,-padx=>15); my $tax= $left4->Label(-text=>'Time In', -background=>'gray')->pack(); my $userINputNumber; my $x = 1; while($x <= $userINputNumber) { my $timIn = $left4->Entry(-width=>12, -background =>'white', -borderwidth=>2,-relief=>'sunken', -textvariable => \$info{LOCname})->pack(); $timIn->insert('end',""); $x++; }
This works fine, and I see the GUI with all the entries ,however, when I enter somthing in one of the entries , it appears on all other entries . I can't fix that, is there a solution to that issue.

Replies are listed 'Best First'.
Re: PL/Tk question
by dreadpiratepeter (Priest) on May 01, 2004 at 22:29 UTC
    You appear to be using the same variable for all the fields. You probably need something like:  -textvariable => \$info{LOCname}[$x]


    -pete
    "Worry is like a rocking chair. It gives you something to do, but it doesn't get you anywhere."
Re: PL/Tk question
by Popcorn Dave (Abbot) on May 01, 2004 at 22:43 UTC
    The problem is that you're putting the information in every time the code loops through the while statement but you're never changing the hash key. If you don't want to do that, you're going to have to put some kind of conditional check in there to see if you want the information to be there or not.

    The way your code is written, no matter what the key is, since it never changes, you're always going to get the value in every entry.

    Hope that helps!

    There is no emoticon for what I'm feeling now.

Re: PL/Tk question
by Anonymous Monk on May 02, 2004 at 00:25 UTC
    I think this does what you are hoping for. Use an array to store each widget as you create them so you can use it after you create them, creating.
    use Tk; use Tk::Frame; use Tk::Entry; use strict; my %info; my @widgets; $info{LOCname}{1}= "first place"; $info{LOCname}{2}= "second place"; $info{LOCname}{3}= "third place"; my $top = tkinit; my $left4 = $top->Frame(-background=>'gray', )->pack(-side=>'left',-pady=>2,-padx=>15); my $tax= $left4->Label(-text=>'Time In', -background=>'gray')->pack(); my $userINputNumber=3; my $x = 1; while($x <= $userINputNumber) { push @widgets, $_ = $left4->Entry(-width=>12, -background =>'white', -borderwidth=>2,-relief=>'sunken', -textvariable => \$info{LOCname}{$x} )->pack(); $_->insert('end',""); $x++; } MainLoop;

    JamesNC