YUCK! That's some horrible code you found.

The $text_label should be a Frame, not a Label. The whole 'Intialize' should be redone with ->repeat.

Here's how I would do the spirit of this program.

#!/usr/bin/perl use strict; # https://perlmonks.org/?node_id=11163827 use warnings; use Tk; my ($red, $green, $blue) = (0, 50, 100); my %colorhash = (Red => \$red, Green => \$green, Blue => \$blue); my $mw = MainWindow->new; $mw->geometry( '300x100+500+400' ); my $row = 1; for ( qw( Red Green Blue ) ) { $mw->Label(-text => $_, )->grid(-row => $row, -column => 1, -sticky => 'w'); $mw->Label(-textvariable => $colorhash{$_}, )->grid(-row => $row++, -column => 2, -sticky => 'e'); } my $color = 0; $mw->repeat( 1000, \&change ); $mw->repeat( 1777, sub {-M $0 < 0 and exec $0}); # restart when code c +hanges MainLoop; sub change { ( sub { $red = int rand 50 }, sub { $green = 50 + int rand 50 }, sub { $blue = 100 + int rand 50 } )[ $color = ( $color + 1 ) % 3 ](); }

In reply to Re: Basic questions of Tk programming by tybalt89
in thread Basic questions of Tk programming by jmClifford

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.