Tk::HList allows some custom properties, but for a simple list, just store the extra data in a hash. Also, Tk objects let you add hash data to an object, as long as you don't interfere with existing methods.Like: $lb->'my_extra_data' = my $hash; Then dereference the extra data out when you need it, with out actually listing it Here is an example with just a normal hash to store the data
#!/usr/bin/perl
use warnings;
use strict;
use Tk;
my $mw = MainWindow->new;
$mw->title("Listbox");
my %grades = (
student1 => {
sub1 => 10,
sub2 => 20,
},
student2 => {
sub1 => 30,
sub2 => 40,
},
student3 => {
sub1 => 20,
sub2 => 60,
},
student4 => {
sub1 => 50,
sub2 => 50,
},
student5 => {
sub1 => 90,
sub2 => 100,
},
);
print "Student Sub1 Sub2\n", '-' x 22, "\n";
for ( sort { $grades{$b}{sub1} <=> $grades{$a}{sub1} } keys %grades )
+{
printf "%-10s%6d%6d\n", $_, $grades{$_}{sub1}, $grades{$_}{sub2};
}
#Create the text box and insert the list of choices in to it
my $lb = $mw->Scrolled("Listbox", -scrollbars => "osoe",
-height => 10,
-selectmode => "extended")
->pack(-side => "left");
foreach (sort keys %grades){
$lb->insert("end", $_ );
}
$mw->Button(-text => "Exit",
-command => sub{exit; })->pack(-side => "bottom");
$mw->Button(-text=>"View", -command => sub {
foreach ($lb->curselection()) {
my $selected_text = $lb->get($_);
print $selected_text,"\t",'sub2= ',$grades{$select
+ed_text}{'sub2'}, "\n";
}
})->pack(-side => "bottom");
#$lb->selectionSet('end');
$lb->see('end');
MainLoop;
|