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

Hi, I am looking for code examples (or snippets) of Perl/TK Hlists or tables that perform periodic auto-refreshing of table rows (e.g., perhaps using the $widget->repeat/entryconfigure functions?)

I'm interested in displaying a table that auto-updates every N seconds. For example, here is a trivial TK/HList that produces a STATIC display of the current set of running processes. I'd like to explore versions of this code where the Hlist rows autoupdate every N seconds (e.g., row attributes update, rows disappear, rows are added). One could do this by cycling through MainWindow->new / MainWindow->destroy / MainWindow->new, but that's very clunky and causes table flashing.

use Tk; use Tk::HList; use Win32::ToolHelp; my @plist; my $col = 0; &getprocHash(); my $mw = MainWindow->new; $mw->resizable(1,1); $mw->title("static process list"); $mw->geometry("400x250+80+100"); my $hlist = $mw->HList(-columns=>5, -header=>1, -background=>"white", -command=>, -browsecmd=>,-selectmode=> 'single')->pack(-fill=>'both', -expand=>'yes'); foreach my $label (qw/PID Threads AID Priority Name/) { $hlist->header('create',$col++,-itemtype=>'text',-text=>$label) ; } foreach my $p (@plist) { my $e = $hlist->addchild(""); $col = 0; foreach $i (1,4,5,6,8) { $hlist->itemCreate($e,$col++, -itemtype=>'text',-text=>"$$p[$i]"); } } MainLoop; exit (0); sub getprocHash { @plist = Win32::ToolHelp::GetProcesses(); }

Replies are listed 'Best First'.
Re: Auto-refreshing Perl TK HLists/Tables
by zentara (Cardinal) on Mar 25, 2011 at 14:14 UTC
    Here is a test I ran awhile back to see if HList leaked memory with rapid updates. It passed the test. I don't have a Tk:repeat in this code, but it shows how to do updates without memory gains. If you want to add and remove rows dynamically, I suggest you use the $HLIST->hide( ) method, rather than attemping to delete and recreate the rows and columns, for the same reason of internal memory buildup of crud left over from old rows and columns.

    All I'm saying, is watch for memory gains, and do it often, before your script gets too complex.

    #!/usr/bin/perl use strict; use Tk; use Tk::HList; use YAML; #use MeM; #demonstrates the internal counter of HList #and why it is better to reconfigure entries #rather than deleting them and adding new my $mw = MainWindow->new(); #create some sample data my %data; foreach (0..100) { $data{$_}{'name'} = 'name'.$_.rand(1000);; $data{$_}{'id'} = rand(time); } #get random list of keys my @keys = keys %data; ################# my $h = $mw->Scrolled( 'HList', -header => 1, -columns => 2, -width => 50, -height => 60, -takefocus => 1, -background => 'steelblue', -foreground =>'snow', -selectmode => 'single', -selectforeground => 'pink', -selectbackground => 'black', # -browsecmd => \&browseThis, )->pack(-side => "left", -anchor => "n"); my $nameh = $h->header('create', 0, -text => ' Name ', -borderwidth => 3, -headerbackground => 'steelblue', -relief => 'raised'); my $idh = $h->header('create', 1, -text => ' ID ', -borderwidth => 3, -headerbackground => 'lightsteelblue', -relief => 'raised'); foreach (@keys) { my $e = $h->addchild(""); #will add at end $h->itemCreate ($e, 0, -itemtype => 'text', -text => $data{$_}{'name'}, ); $h->itemCreate($e, 1, -itemtype => 'text', -text => $data{$_}{'id'}, ); } my $button = $mw->Button(-text => 'exit', -command => sub{exit})->pack; my $buildbut = $mw->Button(-text => 'rebuild list', -command => [\&rebuild] )->pack; my $dumpbut = $mw->Button(-text => 'Dump', -command => sub{ my @entries = $h->info('children'); foreach my $entry(@entries){ print Dump([\$entry]),"\n"; } } )->pack; MainLoop; ######################################################### sub rebuild{ my @entries = $h->info('children'); foreach my $entry(@entries){ $h->delete('entry',$entry); } foreach (0..100) { $data{$_}{'name'} = 'name'.$_.rand(1000); $data{$_}{'id'} = rand(time); } #get random list of keys my @keys = keys %data; foreach (@keys) { my $e = $h->addchild(""); #will add at end $h->itemCreate ($e, 0, -itemtype => 'text', -text => $data{$_}{'name'}, ); $h->itemCreate($e, 1, -itemtype => 'text', -text => $data{$_}{'id'}, ); } $mw->update; }

    I'm not really a human, but I play one on earth.
    Old Perl Programmer Haiku ................... flash japh
Re: Auto-refreshing Perl TK HLists/Tables
by stefbv (Priest) on Mar 25, 2011 at 07:07 UTC