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;
}
| [reply] [d/l] |