Tk::HList dient der Darstellung hierarischer Daten. Das muss nicht zwangsläufig nur ein Baum sein, sondern es kann auch eine Tabelle sein. Dabei enthält jede Zeile mehrere Kindelemente.
#!perl
use strict;
use warnings;
use utf8;
use Tk;
use Tk::HList;
my $mw = tkinit();
my $hlist = $mw->HList->pack(-fill => 'x');
foreach (qw/1 1.1 1.2 haus haus.eingang haus.türe haus.dach/) {
$hlist->add($_, -text => $_);
}
$mw->MainLoop();
#!perl
use strict;
use warnings;
use utf8;
use Tk;
use Tk::HList;
=comment
Dieses Skript demonstriert eine HList mit Widgets als Element
in einer Zeile. Das Beispiel zeigt, dass auch etwas anderes als
reiner Text in eine HList eingefügt werden kann.
=cut
my $mw = MainWindow->new();
my $hlist = $mw->Scrolled(
'HList',
-scrollbars => 'osoe',
-columns => 3,
-header => 1,
-width => 100,
-height => 30,
)->pack();
$hlist->header(
'create',0,
-text => 'Name',
);
$hlist->header(
'create',1,
-text => 'Nummer',
);
$hlist->header(
'create',2,
-text => 'Wohnort',
);
my $entry_name = $hlist->Entry(-text => 'Name suchen...');
my $entry_number = $hlist->Entry(-text => 'Nummer suchen...');
my $entry_place = $hlist->Entry(-text => 'Ort suchen...');
$hlist->add(0);
$hlist->item('create',0, 0, -window => $entry_name, -itemtype => 'window');
$hlist->item('create',0, 1, -window => $entry_number, -itemtype => 'window');
$hlist->item('create',0, 2, -window => $entry_place, -itemtype => 'window');
$hlist->add(1);
$hlist->item('create',1, 0, -text => 'Otto');
$hlist->item('create',1, 1, -text => '04545-25455');
$hlist->item('create',1, 2, -text => 'Berlin');
$hlist->add(2);
$hlist->item('create',2, 0, -text => 'Hans');
$hlist->item('create',2, 1, -text => '04539-49448');
$hlist->item('create',2, 2, -text => 'Frankfurt');
$mw->MainLoop();
Standardmäßig ist die HList von einem schwarzen Rahmen umgeben, wenn sie den Fokus erhält (z.B. wenn man drauf klickt). Dieser Rahmen kann mit der Option -highlightthickness
gesteuert werden. Der Wert 0
versteckt den Rahmen.
#!perl
use strict;
use warnings;
use utf8;
use Tk;
use Tk::HList;
my $mw = tkinit();
# -- create a Tk::HList
my $scrolled_hlist = $mw->Scrolled('HList',
-scrollbars => 'se',
-columns => 2,
-header => 1,
-width => 50,
-height => 20,
# hide black border around HList when it's active
-highlightthickness => 0,
-selectborderwidth => 1,
-selectmode => 'single',
)->pack(-fill => 'y', -expand => 1,);
my $real_hlist = $scrolled_hlist->Subwidget('scrolled');
$real_hlist->configure(
-browsecmd => [ sub{ $_[0]->anchorClear(); }, $real_hlist],
);
# -- add HList header colums
$real_hlist->header(
'create', 0,
-text => 'first column',
);
$real_hlist->header(
'create', 1,
-text => 'second column',
);
# -- add some entries to the HList
$real_hlist->add(1);
$real_hlist->item('create', 1, 0, -text => 'first row, 1st col');
$real_hlist->item('create', 1, 1, -text => 'first row, 2nd col');
$real_hlist->add(2);
$real_hlist->item('create', 2, 0, -text => '2nd row, 1st col');
$real_hlist->item('create', 2, 1, -text => 'second row, 2nd col');
# -- set selection *** without dashed line border ***
$real_hlist->selectionSet(2);
$mw->MainLoop();
exit(0);
Die Einträge der HList, die vom Benutzer ausgewählt wurden, können über die info
-Methode abgerufen werden.
#!perl
use strict;
use warnings;
use utf8;
use Tk;
use Tk::HList;
use Data::Dumper qw/Dumper/;
my $mw = tkinit();
# -- create a Tk::HList
my $scrolled_hlist = $mw->Scrolled('HList',
-scrollbars => 'se',
-columns => 2,
-header => 1,
-width => 50,
-height => 20,
-selectmode => 'extended',
)->pack(-fill => 'both', -expand => 1,);
my $real_hlist = $scrolled_hlist->Subwidget('scrolled');
# -- configure callback to show the currently selected items
$real_hlist->configure(-browsecmd => sub{
my @selected_items = $real_hlist->info('selection');
print Dumper \@selected_items;
},);
# -- add HList header colums
$real_hlist->header(
'create', 0,
-text => 'first column',
);
$real_hlist->header(
'create', 1,
-text => 'second column',
);
# -- add some entries to the HList
$real_hlist->add(1);
$real_hlist->item('create', 1, 0, -text => 'first row, 1st col');
$real_hlist->item('create', 1, 1, -text => 'first row, 2nd col');
$real_hlist->add(2);
$real_hlist->item('create', 2, 0, -text => '2nd row, 1st col');
$real_hlist->item('create', 2, 1, -text => 'second row, 2nd col');
$real_hlist->add(3);
$real_hlist->item('create', 3, 0, -text => '3rd row, 1st col');
$real_hlist->item('create', 3, 1, -text => 'third row, 2nd col');
# -- set selection *** without dashed line border ***
$real_hlist->selectionSet(2);
$mw->MainLoop();
exit(0);