Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change

tk form textvariables

by liamlrb (Acolyte)
on Mar 03, 2009 at 02:32 UTC ( #747603=perlquestion: print w/replies, xml ) Need Help??
liamlrb has asked for the wisdom of the Perl Monks concerning the following question:

I wrote a tk app that uses a hash to store all the -textvariable variables in the form ( eg -textvariable => \$CURRENT{form_field_name}. I use "store"/retrieve from the storable module to save, recall the hash when I want to redisplay the saved form, The problem is when recalled, nothing shows up in the form...

The hash used to store the form textvariable has the same address as the one I restore to and I can see the correct values in the hash but nothing updates in the form. My understanding is that it should be instantaneous when the textvariable is changed?
$e_pilot = $frm_current->Entry( -textvariable => \$CURRENT{PILOT_FIELD}, )->pack(); store(%\CURRENT, "FEED_FILE"); ... $f = retrieve('FEEDS_FILE') or warn "unable to retrieve FEEDS from FEE +D_FILE$! \n" unless defined $f; %CURRENT = %{$f};
The addresses of %CURRENT stored from and retrieved to are the same, the $CURRENT{PILOT_FIELD} has the correct data, but nothing is displayed in the form.

I was able to get it to display by

$e_cs_search->configure( -textvariable=>\$CURRENT{PILOT_FIELD} + ); $mw->update;
but I shouldn't have to right ?????

thanks for any insights


Replies are listed 'Best First'.
Re: tk form textvariables
by hangon (Deacon) on Mar 03, 2009 at 05:31 UTC

    IIRC Tk uses tied scalars for textvariables, so you need to update each scalar value in the hash separately. See the code below.

    use Tk; my %current = (pilot => 'Harry'); my $mw = MainWindow->new; $mw->geometry('200x200'); my $ent = $mw->Entry( -textvariable => \$current{pilot}, )->pack; my $btn = $mw->Button( -text => 'New Pilot', -command => \&update, )->pack; MainLoop; sub update{ # This works $current{pilot} = 'Tom'; # This does not work %current = (pilot => 'Tom'); # So update the entire hash this way my %newstuff = (pilot => 'Tom', copilot =>'Dick', etc => 'Harry'); for (keys %newstuff){ $current{$_} = $newstuff{$_}; } }
      tried your version but still seems to need the configure and update to work... here is the actually version ( %CURRENT is a globel variable that holds all the form variables
      $frm_current->BrowseEntry( -variable => \$CURRENT{PLATFORM_FIELD}, -width => 11, -font => $font, -background => $bgColor, -state => 'normal', -choices => [@PLATFORM_LIST] ), #etc ... sub displayFeed { # $path is from a Hlist path which is the key into hash my $path = shift; my %C = %{$FEEDS{$path}{formFields}}; # checked %C and %CURRENT and all values are present # after this loop for ( keys %C ){ $CURRENT{$_} = $C{$_}; } # still no update of the form after this update $mw->update; return
      Do I have to do the configure and update for every form field or am I still missing something ? I am just trying to create an easy way to store/recall and redisplay form fields. I am sure that this is a common problem with a well known solution. Am I taking the right track?
        Well I know yours works so I will start looking at any differences I can find.. thanks again

        Try using a hash reference:

        my %CURRENT = (PLATFORM_FIELD => ... my $hashref = \%CURRENT; ... $frm_current->BrowseEntry( -variable => \$hashref->{PLATFORM_FIELD}, -width => 11, -font => $font, -background => $bgColor, -state => 'normal', -choices => [@PLATFORM_LIST] ),
      Got it.. thanks for everyone's response... I chose to learn perl because of Perl community's value of sharing, well represented in all the modules available and this site as well. I am not a full time programmer but I enjoy making mine, and other's jobs easier by trying to do it. Thanks again.
Re: tk form textvariables
by GrandFather (Sage) on Mar 03, 2009 at 02:49 UTC

    A small self contained runnable sample script would help illustrate your problem.

    In general Tk doesn't update UI content unless you tell it to. Simply changing the contents of a variable doesn't constitute telling the UI to update. It may be that calling update on the widget who's content has changed will achieve what you need.

    True laziness is hard work
Re: tk form textvariables
by gnosti (Friar) on Mar 03, 2009 at 03:42 UTC

    You are right to expect a change in the -textvariable to autoupdate the associated field. It works for me.

    Checking the addresses of the hash entries, that they are the same before and after your save/restore, also makes sense.

    In your example, you are using two different widgets $e_pilot and $e_cs_search. Could that be an issue? It would be easier to read your sample code if you could enclosed it in <code> your sample code </code> tags.

    That's all that comes to mind.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://747603]
Approved by planetscape
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (4)
As of 2017-04-25 00:53 GMT
Find Nodes?
    Voting Booth?
    I'm a fool:

    Results (447 votes). Check out past polls.