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

I am trying to learn tk so i am writing a simple script to ping hosts and make their names red if they are down and green if they are up. The script works fine except I cannot figure out how to make it refresh the screen. Currently the timer reruns the main subroutine and prints the output below the current output, i would just like it to update what is already there. I tried ->update as you can see on line 35 (it is commented out now) but that did not work. Any help would be appreicated, tk seems pretty cool and i would like to learn it.

#!/usr/bin/perl use strict; use warnings; use Net::Ping; use Tk; my @host_array = qw(server1 server2 server3 server4); my $window = MainWindow->new(); $window->minsize( qw(250 250)); $window->title("Server Status"); $window->configure (-background => 'grey'); my $top = $window->Frame(-background => 'grey',)->pack(-side=>'top',-f +ill=>'x'); #$window->Button(-text => "Refresh", -command => \&pinghost )->pack; my $timer = $window->repeat(2000,\&pinghost); $window->Button(-text => "Quit", -command => \&Quit )->pack; &pinghost; MainLoop(); ######################################################### sub pinghost{ foreach my $host (@host_array) { my $color = ("green"); my $p = Net::Ping->new("icmp"); $color = ("red") unless $p->ping($host, 2); $p->close(); my $t1 = $top->Label(-text=>"$host",-background => "$color")->pack +(); } #$window->update; } sub Quit{ exit; }

Thanks, Sean

Replies are listed 'Best First'.
Re: TK refresh widgit data
by AnomalousMonk (Archbishop) on Jan 22, 2010 at 18:14 UTC
    my $t1 = $top->Label(-text=>"$host",-background => "$color")->pack();

    Rather than repeatedly creating and pack-ing a new  Label widget into its parent as is now done, try re-configuring the options of an existing widget with  configure (see discussion of this method in Tk::options):
        $label_widget->configure(-text => $new_text, -background => $new_color);
    (Again, this assumes the label widget has already been created and packed somewhere else in the code.)

    BTW: It's not necessary to interpolate a scalar into a string (e.g.,  "$host" in the OPed code) in order to use it as a string.

    Also BTW: I share your liking for Tk, all the more since I have some hard-won experience with it. But, sadly, it seems possible it may have become an orphan application. See various discussions on PerlMonks of Tk vis-a-vis Gtk2 and Wx.

Re: TK refresh widgit data
by zentara (Cardinal) on Jan 22, 2010 at 19:05 UTC
    What you want to do is create your labels only 1 time, starting with an empty string of spaces as text. Then in your update sub, fill them in with a configure.
    #!/usr/bin/perl use strict; use warnings; use Tk; my @array = ('red','green','yellow'); my @host_array = qw(server1 server2 server3 server4); my %labs; my $window = MainWindow->new(); $window->minsize( qw(250 250)); $window->title("Server Status"); $window->configure (-background => 'grey'); my $top = $window->Frame(-background => 'grey',)->pack(-side=>'top',-f +ill=>'x'); foreach my $host (@host_array){ $labs{$host} = $top->Label(-text=>"$host",-background => 'white')-> +pack(); } #$window->Button(-text => "Refresh", -command => \&pinghost )->pack; my $timer = $window->repeat(2000,\&pinghost); $window->Button(-text => "Quit", -command => \&Quit )->pack; #&pinghost; MainLoop(); ######################################################### sub pinghost{ foreach my $host (@host_array) { push (@array,shift(@array)); my $color = $array[0]; my $rand = rand 1; $labs{$host}->configure(-text=>"$host-$rand", -background => "$col +or"); } #$window->update; } sub Quit{ exit; }

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