You sort of have the right idea, but there is one thing to watch out for: do not try to
use the widget as a hashkey, as it will result in an error. You are doing it right by assigning a counter. Here is an example based on my code example above.
P.S. To make your code look better, put your listboxes into Scrolled Panes. This example needs some work to make it look right, but it demonstrates the hash idea quite nicely.
#!/usr/bin/perl
use strict;
use warnings;
use Tk;
use Tk::Pane;
my $mw = MainWindow->new;
$mw->geometry(q/+100+100/);
$mw->title("Test Script");
#need exportselection=>0 for using multiple selection boxes
my %lbhash;
# you want to use the listbox to hold your
# data, so store the widget's actual object
# with the name you want to give it
my @choices = ('afg', 'dfg', 'erty', 'etc');
foreach my $choice(@choices){
my $pane = $mw->Scrolled('Pane')->pack(-side => 'left');
my $label = $pane->Label(-text=> $choice, -bg=>'lightseagreen')
->pack(-side=>'top');
$lbhash{ $choice }{'widget'} = $pane->Listbox(-bg => 'white',
-selectmode => 'extended',
-exportselection=> 0 )->pack(-side=>'bottom');
my @random_data;
for(1..20){push @random_data ,int rand(100) }
$lbhash{ $choice }{'widget'} ->insert( 'end', @random_data );
}
$mw->Button( -text => "Selections", -command =>
sub{
foreach my $key( keys %lbhash ){
my @selects = $lbhash{ $key }{'widget'}->curselection;
print "$key: ";
foreach my $select (@selects){
print $lbhash{ $key }{'widget'}->get($select),' ';
}
print "\n\n";
}
}
)->pack( -side => 'left' );
$mw->Button( -text => "Done", -command => \&exit )->pack;
MainLoop;
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.