http://www.perlmonks.org?node_id=790448

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.

Replies are listed 'Best First'.
Re: Tk Entry not updating when textvariable changes
by ikegami (Patriarch) 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 :)