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


In reply to TK refresh widgit data by siffland

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.