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

Dear Monks, I want to be able to use an array element
as the -textvariable argument to
$widget->Entry method.

Here's what I tried ...

#global vars @data = ( "init", "init", "init", "init" ); for ( my $row=0; $row<2; $row++ ) { for( my $col=0; $col<$#headers+1; $col++) { my $e; if( $row == 0 ) { $e = $tableWidget->Button ( -relief => "groove", -justify => "left", -text => $headers[$col], -state => 'active', -width => length($headers[$col]) ); } else { $e = $tableWidget->Entry ( -width => $lens[$col], -textvariable => \$data[$col] ); } $tableWidget->put( $row, $col, $e ); } }
... and I would update the displayed textvariables by calling the sub ...
sub update { ... @data = ( / ... big long regex ... /g); ... }
... the problem I am having is that although @data changes the displayed textvariables in my Entry widgets are not changing. So what I had to do was write this ugly code.
... # global variables my $col_0; my $col_1; my $col_2; my $col_3; ... for ( my $row=0; $row<2; $row++ ) { for( my $col=0;$col<$#headers+1;$col++) { my $e; if( $row == 0 ) { $e = $tableWidget->Button ( -relief => "groove", -justify => "left", -text => $headers[$col], -state => 'active', -width => length($headers[$col]) ); } else { if ( $col == 0 ) { $e = $tableWidget->Entry ( -width => $lens[$col], -textvariable => \$col_0); } elsif ( $col == 1 ) { $e = $tableWidget->Entry ( -width => $lens[$col], -textvariable => \$col_1); } elsif ( $col == 2 ) { $e = $tableWidget->Entry ( -width => $lens[$col], -textvariable => \$col_2); } elsif ( $col == 3 ) { $e = $tableWidget->Entry ( -width => $lens[$col], -textvariable => \$col_3); } $tableWidget->put( $row, $col, $e ); } } } ... sub update { ... @data = ( / ... big long regex ... /g); ... $col_0 = data[0]; $col_1 = data[1]; $col_2 = data[2]; $col_3 = data[3]; }
... this works but I bet there is a better way. Please bestow your wisdom. Thank you

Replies are listed 'Best First'.
Re: Tk::Entry textvariable and array elements
by Baboon (Acolyte) on Jun 12, 2002 at 19:24 UTC
    Actually array elements are binded okay, problem is hidden in other place.

    The point is - when you did -textvariable=>$arr[$i], Tk used those scalars as a references to Entry contents.

    But after your assignment to entire @data something, you have new different scalars in there!

    But if you'll do

    @data[0..$#data] = ( 'a'..'x');
    your code will work as you desired to!

    Below is working code snippet for you to try:

    use strict; use Tk; use Tk::Table; my $mw = tkinit; my @data = ( "init", "init", "init", "init" ); my $tableWidget = $mw->Table->form(-top=>'%0', -bottom=>['%100',-20], +-left=>'%0', -right=>'%100'); my $btn = $mw->Button( -command=>\&update, )->form(-top=>$tableWidget, -bottom=>'%100', -left=>'%0', -right=>'%10 +0'); my @headers = 'a'..'k'; my $e; for ( my $row=0; $row<2; $row++ ) { for( my $col=0; $col<$#headers+1; $col++) { my $e; if( $row == 0 ) { $e = $tableWidget->Button ( -relief => "groove", -justify => "left", -text => $headers[$col], -state => 'active', -width => length($headers[$col]) ); } else { $e = $tableWidget->Entry ( # -width => $lens[$col], -textvariable => \$data[$col] ); } $tableWidget->put( $row, $col, $e ); } } sub update { @data[0..$#data] = ( 'a'..'x'); } MainLoop;
    Best wishes,
    Baboon
    PS.Weasel is stooped.