Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

perl/Tk Entry widget displays wrong value when -textvariable is shared

by Smile-n-Nod (Initiate)
on Sep 02, 2010 at 16:16 UTC ( #858601=perlquestion: print w/ replies, xml ) Need Help??
Smile-n-Nod has asked for the wisdom of the Perl Monks concerning the following question:

use threads; use threads::shared; use Tk; # If these two hashes are shared (as below), the Entry widget displays # "Original"; if they are not shared, the widget displays "Changed". # %top_hash; # %hash; share(%top_hash); share(%hash); $top_hash{A} = \%hash; $top_hash{A}{B} = 'Original'; $t = MainWindow->new()->Scrolled('Text')->pack(); $e = $t->Entry(-textvariable => \$top_hash{A}{B}); $t->windowCreate('end', -window => $e); $t->insert('end', "\n"); $top_hash{A}{B} = 'Changed'; MainLoop;
If %top_hash and %hash are shared (as above), this script displays an Entry widget which contains "Original". If those hashes are not shared, the Entry widget display "Changed." Why doesn't the Entry widget update its display when I set -textvariable to a reference to a shared hash?

Comment on perl/Tk Entry widget displays wrong value when -textvariable is shared
Download Code
Re: perl/Tk Entry widget displays wrong value when -textvariable is shared
by zentara (Archbishop) on Sep 02, 2010 at 17:30 UTC
    You are ecncountering a problem with threads:shared hashes where only the first level keys get shared by default. When you run as shared, the $top_hash{A}{B} gets ignored as being shared, while it runs fine in normal perl.

    To amplify a bit, the Tk eventloop will never respond to a change in threads shared variables, when they are across thread boundaries. You don't have actual threads here, but you need to setup a timer, to read for any changes in the shared variable. So you can't use tied variables effectively across threads without timers. Try this:

    #!/usr/bin/perl use warnings; use strict; use threads; use threads::shared; use Tk; my %top_hash; share $top_hash{'A'}{'B'}; $top_hash{'A'}{'B'} = 'Original'; my $t = MainWindow->new()->Scrolled('Text')->pack(); my $e = $t->Entry(-textvariable => \$top_hash{'A'}{'B'}); $t->windowCreate('end', -window => $e); $t->insert('end', "\n"); my $timer = $t->after(3000,sub{$top_hash{'A'}{'B'} = 'Changed'}); MainLoop;

    I'm not really a human, but I play one on earth.
    Old Perl Programmer Haiku ................... flash japh

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (11)
As of 2014-10-31 18:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (222 votes), past polls