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

Heys all,

I'm trying to write a script which involves a large number of pairs of Tk labels and entry boxes. I've written a sub to create each pair, pointing the textvariable to a hash, meaning to create a hash of entry box data keyed on a given key.

However, I do not seem to be able to get information out of the entry box hash - it seems that the data is not being assigned there in the first place. Can anyone see a way to solve this, or if what I am trying to do is not possible, is there an alternative solution? (would a global hash work?)

My code to create each label/entry box pair is below:

sub labelledEntry { my ($_objects, $objkey, $label, $width, $container) = @_; my %objects = %$_objects; $container -> Label( -text => $label ) -> pack( -side => 'left' ); $container -> Entry( -width => $width, -textvariable => \$objects{ +$objkey} ) -> pack( -side => 'right' ); return \%objects; }

and the code I'm using to get data out of the hash (the button code and subroutine to output it) is as follows:

$searchFrame -> Button( -text => 'search', -command => [ \&search, \$o +bjects{searchstr} ] )->pack( -side => 'left' ); sub search { my $text = shift; print STDERR $text; }

Any advice would be appreciated.

Replies are listed 'Best First'.
Re: Tk Entry Widgets & Subs
by Courage (Parson) on Aug 21, 2002 at 15:38 UTC
    Your problem is in that line:
    $container -> Entry( -width => $width, -textvariable => \$objects{ +$objkey} ) -> pack( -side => 'right' );
    You reference to a freshly created hash, which will be *overwrited* by a next call to your sub. Throw away a line my %objects = %$_objects; Instead write
    $container -> Entry( -width => $width, -textvariable => \$_objects +->{$objkey} ) -> pack( -side => 'right' );
    Try to follow which reference to which scalar you want your widget to refer, and you'll understand what I mean

    BTW why you're not using just "LabEntry" widget?

    Courage, the Cowardly Dog
    things, I do for love to perl...

      LabEntry widget? What's that then?

      That code seems to have done the trick, thanks for your help :)

      Foxcub