contact@solamen.fr has asked for the wisdom of the Perl Monks concerning the following question:

Dear all. I would like to link dynamically the textvariable of an Entry with the choice made in a BrowseEntry widget. I have tried the code below but changing the value of the BrowseEntry does not refresh the content of the following Entry as wanted. I have also tried "bind" and "update" methods wihtout success. Any help would be much appreciated.
use strict; use Tk; use Tk::BrowseEntry; my $AssignMet='\Input\0InputMeteo.txt'; my $AssignTmy='\Input\TMY2\FR-Trappes-71450.tm2'; my $type='Site'; my $simuTl = new MainWindow(); my $f0 = $simuTl->Frame(); my $l="Type de fichier m\xE9t\xE9o"; my $lb0 = $f0->BrowseEntry( -label => $l, -choices => ['Site','TMY2'], -variable => \$type, )->pack(); $f0->pack(); my $f1 = $simuTl->Frame(); my $l="Fichier de donn\xE9es m\xE9t\xE9o"; my $lab1 = $f1->Label( -text => $l, ); if ($type eq "Site") { my $ent1 = $f1->Entry(-textvariable=>\$AssignMet); $lab1->pack(-side => 'left'); $ent1->pack(); $f1->pack(); } else { my $ent1 = $f1->Entry(-textvariable=>\$AssignTmy); $lab1->pack(-side => 'left'); $ent1->pack(); $f1->pack(); } MainLoop;

Replies are listed 'Best First'.
Re: perl/Tk Entry updating
by zentara (Cardinal) on Jul 30, 2012 at 09:54 UTC
    In addition to the textvariable problem which has been pointed out, what you need to do is add a -browsecmd to make your program work.
    #!/usr/bin/perl use strict; use Tk; use Tk::BrowseEntry; my $AssignMet='\Input\0InputMeteo.txt'; my $AssignTmy='\Input\TMY2\FR-Trappes-71450.tm2'; my $type='Site'; my $simuTl = new MainWindow(); my $f0 = $simuTl->Frame(); my $l="Type de fichier m\xE9t\xE9o"; my $lb0 = $f0->BrowseEntry( -label => $l, -choices => ['Site','TMY2'], -variable => \$type, -browsecmd => \&detect , )->pack(); $f0->pack(); my $f1 = $simuTl->Frame(); my $l="Fichier de donn\xE9es m\xE9t\xE9o"; my $lab1 = $f1->Label( -text => $l, ); my $ent1; if ($type eq "Site") { $ent1 = $f1->Entry(-text => $AssignMet); $lab1->pack(-side => 'left'); $ent1->pack(); $f1->pack(); } else { $ent1 = $f1->Entry(-text => $AssignTmy); $lab1->pack(-side => 'left'); $ent1->pack(); $f1->pack(); } MainLoop; sub detect{ if ($type eq "Site") { $ent1-> configure (-text => $AssignMet); } else { $ent1->configure(-text => $AssignTmy); } }
    As an oddity, I did find that your erroneous use of textvariable did work. This is somewhat bad programming, so I show the code here in the READMORE. Do not expect this switching of textvariable to be reliable, but somehow it works.

    I'm not really a human, but I play one on earth.
    Old Perl Programmer Haiku ................... flash japh
Re: perl/Tk Entry updating
by Anonymous Monk on Jul 30, 2012 at 09:46 UTC

    I have tried the code belo

    You have written

    -variable => \$type, my $ent1 = $f1->Entry(-textvariable=>\$AssignMet); my $ent1 = $f1->Entry(-textvariable=>\$AssignTmy);

    But \$type is not \$AssignMet is not \$AssignTmy --- at least two of those should be the same variable

    I have also tried "bind" and "update" methods wihtout success.

    Maybe show what you tried?