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

In a normal TK listbox you use the ->get() method to actually "get" the data. In TK::Hlist how can I go through and get the data from each row on each column?

Replies are listed 'Best First'.
Re: TK::Hlist getting the data
by zentara (Cardinal) on May 29, 2008 at 13:08 UTC
    This shows a couple of ways. The problem with it is the built-in bindings for mouse button 1 press and release in hlist ( a double listing). But you should be able to work around it, by not using browsecommand. Or you could try to subclass HList and make the ButtonRelease-1 a no-op, or use a right mouse click binding instead.
    #!/usr/bin/perl use Tk; use Tk::HList; use YAML; $top = new MainWindow; $hlist = $top->Scrolled("HList", -header => 1, -columns => 4, -scrollbars => 'osoe', -command => sub{print 1}, -width => 70, -selectbackground => 'SeaGreen3', -browsecmd => \&browseThis, )->pack(-expand => 1, -fill => 'both'); $hlist->header('create', 0, -text => 'From'); $hlist->header('create', 1, -text => 'Subject'); $hlist->header('create', 2, -text => 'Date'); $hlist->header('create', 3, -text => 'Size'); $hlist->add(0); $hlist->itemCreate(0, 0, -text => "eserte\@cs.tu-berlin.de"); $hlist->itemCreate(0, 1, -text => "Re: HList?"); $hlist->itemCreate(0, 2, -text => "1999-11-20"); $hlist->itemCreate(0, 3, -text => "1432"); $hlist->add(1); $hlist->itemCreate(1, 0, -text => "dummy\@foo.com"); $hlist->itemCreate(1, 1, -text => "Re: HList?"); $hlist->itemCreate(1, 2, -text => "1999-11-21"); $hlist->itemCreate(1, 3, -text => "2335"); MainLoop; sub browseThis{ for my $column (0..3){ print $hlist->itemCget( $hlist->selectionGet, $column, 'text' ), "\n"; } } =head1 # a dumper alternative sub browseThis{ my $listArrayRef = []; my @selectedindices = $hlist->info('selection'); foreach my $r (@selectedindices) { push @{$listArrayRef}, getRowArrayRef($hlist, $r); print Dump(@{$listArrayRef}),"\n"; } sub getRowArrayRef { my ($hlist, $r) = @_; my @row; foreach my $c (0 .. $hlist->cget(-columns) -1) { push @row, $hlist->itemCget($r, $c, '-text'); } return \@row; } } =cut

    I'm not really a human, but I play one on earth CandyGram for Mongo
Re: TK::Hlist getting the data
by psini (Deacon) on May 29, 2008 at 12:59 UTC

    Use ->get(index) to retrieve the value of a single cell, ->get(index1, index2) to retrieve the values from a range of cells.

    See Tk::Listbox

    Updated oh right, I should learn to read before writing :(

    Rule One: Do not act incautiously when confronting a little bald wrinkly smiling man.