in reply to Basic questions of Tk programming

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 ](); }

Replies are listed 'Best First'.
Re^2: Basic questions of Tk programming
by jmClifford (Beadle) on Jan 25, 2025 at 05:15 UTC

    Hi. A great response by all. And thanks for the re-write of the code (tybalt89). I appreciated all of this except for the "# restart when code changes" line.

    It can be a problem when you suspect you are missing key issues for bad code.

    Regards JC......