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

Hi Monks, I have a question about using the entry widget. The number of entry widgets created depends on feedback from the user. This is stored in array @lb. The entry widget is created for every option in @lb. But I have not been able to enter values to these entry widgets. Suppose I have 3 variables in @lb, 3 entry widgets are created and all 3 take the same value. How do I modify my code to have each entry widget with its own value?
sub myListBox1{ my $var; my @arr123; my @lb = @_; my $mw = new MainWindow; $mw->title("Custom Adjustment"); foreach my $lab (@lb) { my $label = $mw->Label(-text=>"$lab")->pack(); my $entry = $mw->Entry( -textvariable => \ $var, -width=>30)->pack(); push @arr123, $var; } my $tx1= $mw->Label(-text=>" ")->pack(-anchor=>'center'); my $frame = $mw->Frame->pack( -anchor => 'w' ); $frame->Button(-text => "Exit", -width=>15, -command => sub{exit, $mw->destroy; })->pack(-side => "rig +ht", -anchor=>'cent +er', -fill => 'y'); $frame->Button(-text=>"Select", -command => sub{ $mw->destroy()}, )->pack(-side => "right", -anchor=>'center', -fill => 'y'); + MainLoop; return @arr123; }

Replies are listed 'Best First'.
Re: Using Entry widget in a loop
by beech (Parson) on Mar 30, 2016 at 00:28 UTC
    Take reference to array element, each entry gets its own
    #!/usr/bin/perl -- use strict; use warnings; use Tk; print $_,$/ for myListBox1(qw/ ro sham bo /); sub myListBox1 { my @lb = @_; my $omw = Tk::MainWindow->new; $omw->withdraw; ## no show my $mw = $omw->Dialog( -buttons => [qw/ Select Exit / ]); $mw->transient(undef); ## let dialog show even if MainWindow withd +rawn $mw->title("Custom Adjustment"); my @entries; foreach my $lab (@lb) { push @entries, ""; ## add a new one my $label = $mw->Label( -text => "$lab" )->pack(); my $entry = $mw->Entry( -textvariable => \$entries[-1], ## a reference to the last + entry -width => 30, )->pack(); } my $selectOrExit = $mw->Show; return $selectOrExit , @entries; }