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

Tk Entry not updating when textvariable changes

by smithbr (Initiate)
on Aug 21, 2009 at 19:22 UTC ( #790448=perlquestion: print w/ replies, xml ) Need Help??
smithbr has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I am just starting with Tk and I'll admit, I don't get a chance to write a lot of Perl. But, this seems very simple to me so I can't figure out why it won't work.

#!/usr/bin/perl -w use Tk; use strict; my $text = "foo"; my $mw = MainWindow->new; my $button = $mw->Button(-text=>'Change entry', -command=>\&change)->p +ack(); my $entry = $mw->Entry(-textvariable=>$text)->pack(); MainLoop; sub change { print "\$text before: $text\n"; $text = "bar"; print "\$text after: $text\n"; $mw->update; }

I assume I must be doing something really stupid because my entry never gets updated, but $text changes from the prints.

If I add $entry->configure(-textvariable=>$text) before the $mw->update() it works ok. However, all the documentation suggests if I use textvariable, changing the variable anywhere should magically update the widget text.

I searched around, but the similar problems I saw were much more complicated than this so I didn't get a lot of help from them.

Thanks.

Comment on Tk Entry not updating when textvariable changes
Download Code
Re: Tk Entry not updating when textvariable changes
by ikegami (Pope) on Aug 21, 2009 at 20:16 UTC
    On a hunch, I tried
    my $entry = $mw->Entry(-textvariable => \$text)->pack(); ^ |

    And it worked.

      grrr, that was it, thanks!

      In my actual code, about half of my entry textvariables are using references and half aren't (because I was cutting-and-pasting chunks basically). Of course, the ones I was just now testing were NOT references and the ones that are references aren't in something I expect to update, so I wasn't seeing anything update.

      I then just grabbed the chunk of code without the references and tried it stand-alone and didn't see/think to make the textvariables point to references so I assumed I was doing something really dumb..... Which I was :)

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://790448]
Approved by ikegami
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (11)
As of 2014-09-18 19:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (121 votes), past polls