#!/usr/bin/perl # Multiple Widgets with one scrollbar.pl use Tk; require Tk::Pane; $mw = MainWindow->new(); $mw->title("One Scrollbar/Three Listboxes"); $mw->Button(-text => "Exit", -command => sub { exit })->pack(-side => 'bottom'); $scroll =$mw->Scrollbar(); # Anonymous array of the three Listboxes $listboxes = [ $mw->Scrolled(Pane, -scrollbars => 'ose', -sticky => 'nsew', -background => 'green'), $mw->Scrolled(Pane, -scrollbars => 'ose', -sticky => 'nsew', -background => 'blue'), $mw->Scrolled(Pane, -scrollbars => 'ose', -sticky => 'nsew', -background => 'cyan') ]; # This method is called when on Listbox is scrolled with the keyboard # It makes the Scrollbar reflect the chang, and scrolls the other lists sub scroll_listboxes { my ($sb, $scrolled, $lbs, @args) = @_; $sb->set(@args); # tell the Scrollbar what to display my ($top, $bottom) = $scrolled->yview(); foreach $list (@$lbs) { $list->yviewMoveto($top); # adjust each lb } } # Configure each Listbox to call &scroll_listboxes foreach $list (@$listboxes) { $list->configure(-yscrollcommand => [ \&scroll_listboxes, $scroll, $list, $listboxes ] ); } # Configure the Scrollbar to scroll each Listbox $scroll->configure(-command =>sub { foreach $list (@$listboxes) { $list->yview(@_); }}); # Pack the Scrollbar and Listboxes $index = 0; $scroll->pack(-side => 'left', -fill => 'y'); foreach $list (@$listboxes) { $list->pack(-side => 'left'); $list->Label(-text => "One")->pack(); $list->Label(-text => "Two")->pack(); $list->Label(-text => "Three")->pack(); $list->Label(-text => "Four")->pack(); $list->Label(-text => "Five")->pack(); $list->Label(-text => "Six")->pack(); $list->Label(-text => "Seven")->pack(); $list->Label(-text => "Eight")->pack(); $list->Button(-text => "$index", -command => sub { exit})->pack(); $index++; } MainLoop;