State_Space has asked for the wisdom of the Perl Monks concerning the following question:

Hello, I wrote a PERL TK program that creates 3 Labels with text variables inside frames. A snippet is below. I use other frames for other things.
my $red; my $green; my $blue; my $mw = MainWindow->new; $mw->title(); my $right_frame = $mw->Label->pack(); my $variable_frame = $right_frame->Label->pack(); my $red_frame = $variable_frame->Label(-textvariable =>\$red)->pack(); my $green_frame = $variable_frame->Label(-textvariable => \$green)->pa +ck(); my $blue_frame = $variable_frame->Label(-textvariable => \$blue)->pack +(); $variable_frame->Label(-text =>"------")->pack();
I removed what was inside pack();, so the code was single lined.
I then proceed to scan a file and update the variables red, green, and blue. When updating the frame.
$red_frame->update; $greeen_frame->update; $blue_frame->update;

The last updated variable shows up, but the previously updated ones disappear. So from the code above blue would be visible but red and green labels would be blank, in the GUI. How can I have all variables showing in the GUI?

Some psuedo code:
Create GUI
LOOP
scan new document
find $red
update gui
find $green
update gui
$find $blue
update gui
END Loop
I would like my variables updating as soon as the change and not wait till they're all found at the end to update in the GUI. The last Label as well containing no textvariable just a string "------" goes blank when the loop resets.

Replies are listed 'Best First'.
Re: PERL Tk label(-textvariable)) (Tk::after repeat while(1))
by Anonymous Monk on Aug 27, 2015 at 00:04 UTC
    This is how you infinite loop in tk, see Tk::after Just add to fishmonger version :)
    my $colorChanger = sub { $red = floor(5*rand()); $green = floor(5*rand()+10); $blue = floor(5*rand()+20); }; $colorChanger->(); ## Initizalizezazazabanana $mw->repeat( 2000, $colorChanger ); MainLoop;

    This way you also don't need an ->update with textvariables

Re: PERL Tk label(-textvariable))
by fishmonger (Chaplain) on Aug 26, 2015 at 18:26 UTC

    Please post a short but complete script that demonstrates your problem. With that, we will be able to tell you what you're doing wrong.

      use strict; use warnings; use Tk; use POSIX; my $red; my $green; my $blue; my $mw = MainWindow->new; $mw->title('Raw Renamer'); my $right_frame = $mw->Label->pack(-side => 'left', -anchor => 'n', -e +xpand => 1); my $text_frame = $right_frame->Label->pack(-side => 'left', -anchor => + 'n', -expand => 1); $text_frame->Label(-text =>"Red")->pack(-side => 'top', -anchor => 'w' +); $text_frame->Label(-text =>"Green")->pack(-side => 'top', -anchor => ' +w'); $text_frame->Label(-text =>"Blue")->pack(-side => 'top', -anchor => 'w +'); my $variable_frame = $right_frame->Label->pack(-side => 'left', -ancho +r => 'n', -expand => 1); my $red_frame = $variable_frame->Label(-textvariable =>\$red)->pack(-s +ide => 'top'); my $green_frame = $variable_frame->Label(-textvariable =>\$green)->pac +k(-side => 'top'); my $blue_frame = $variable_frame->Label(-textvariable =>\$blue)->pack( +-side => 'top'); Intialize(); MainLoop; sub Intialize { while(1) { $red = floor(5*rand()); $red_frame->update; sleep 1; $green = floor(5*rand()+10); $green_frame->update; sleep 1; $blue = floor(5*rand()+20); $blue_frame->update; sleep 1; } }

        How many frames to you believe you created? It's not as many as you think. The answer is 0.

        Try this version.

        #!/usr/bin/perl use strict; use warnings; use Tk; use POSIX; my $red; my $green; my $blue; my $mw = MainWindow->new; $mw->geometry('300x300'); $mw->title('Raw Renamer'); my $right_frame = $mw->Frame->pack(-side => 'left', -anchor => 'n', -expand => 1); my $text_label = $right_frame->Label->pack(-side => 'left', -anchor => 'n', -expand => 1); $text_label->Label(-text =>"Red")->pack(-side => 'top', -anchor => 'w'); $text_label->Label(-text =>"Green")->pack(-side => 'top', -anchor => 'w'); $text_label->Label(-text =>"Blue")->pack(-side => 'top', -anchor => 'w'); my $variable_frame = $mw->Frame->pack(-side => 'left', -anchor => 'n', -expand => 1); my $red_label = $variable_frame->Label(-textvariable =>\$red)->pack(- +side => 'top'); my $green_label = $variable_frame->Label(-textvariable =>\$green)->pac +k(-side => 'top'); my $blue_label = $variable_frame->Label(-textvariable =>\$blue)->pack +(-side => 'top'); Intialize(); MainLoop; sub Intialize { while(1) { $red = floor(5*rand()); $red_label->update; sleep 2; $green = floor(5*rand()+10); $green_label->update; sleep 2; $blue = floor(5*rand()+20); $blue_label->update; sleep 2; } }