9.7. How do I configure a Scrollbar to scroll multiple widgets?

Note that the widget type that you wish to scroll can be important as a scroll "unit" on a Text or Listbox may be a character (several pixels - depending on font) whereas it would be an X "units" on a Canvas (could be pixel - but you may also specify other units).

A concrete answer for scrolling 3 Listboxes comes courtesy of Frederick L. Wagner <derf@ti.com>:

From a working example of multi-xscrolling:
    sub multiscrollx
    {  # multiscrollx
     my ($sb,$wigs,@args) = @ARG;
     my $w;
     foreach $w (@$wigs)
     {
       $w->xview(@args);
     }
    }  # multiscrollx
 
    # %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 
      $sh->configure( -command => [ \&multiscrollx, $sh,
                         [$scratchrule,$ruleheader,$ruletable]]);
      $ruletable->configure(  -xscrollcommand => [ 'set', $sh]);
      $ruleheader->configure( -xscrollcommand => [ 'set', $sh]);
      $scratchrule->configure(-xscrollcommand => [ 'set', $sh]);
In this case,
$sh is a horizontal Scrollbar,
$ruletable and $scratchrule are Tables
$ruleheader is an Entry

However, this approach is good for any widget with X-scrolling capability, I think. So the Y counterpart should be:

    sub multiscrolly
    {  # multiscrolly
     my ($sb,$wigs,@args) = @ARG;
     my $w;
     foreach $w (@$wigs)
     {
       $w->yview(@args);
     }
    }  # multiscrolly
 
    # %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 
      $sv->configure( -command => [ \&multiscrolly, $sv,
                                    [$l1,$l2,$l3]]);
      $l1->configure( -yscrollcommand => [ 'set', $sv]);
      $l2->configure( -yscrollcommand => [ 'set', $sv]);
      $l3->configure( -yscrollcommand => [ 'set', $sv]);
Hope that helps.

Greg VanSickle <vansickl@bnr.ca> points out that this little script snippet does not provide for the binding of '<Button-2<' that he is accustomed to. He wrote a package called DSListbox to address this binding issue.

Conversely, Jong Park asked how to setup multiple Scrollbars to scroll the same widget. Nick Ing-Simmon's reply makes use of an anonymous sub and can be summed up in a little script that scrolls a Text widget (to see the scrolling in action type more than 20 lines of text into the widget):

    #!/usr/local/bin/perl -w
    
    use Tk;
    my $mw = MainWindow->new();
    
    my $s1 = $mw->Scrollbar(-orient => 'vertical');
    my $s2 = $mw->Scrollbar(-orient => 'vertical');
    
    $s1->pack(-side => 'left', -fill => 'y');
    my $t = $mw->Text(
        -yscrollcommand =>  sub{$s1->set(@_), $s2->set(@_)},
        -wrap           => 'word',
        -width          => 70,
        -height         => 20, 
        -font           => $font,
        -setgrid        => 1,
    )->pack(-side => 'left');
    $s2->pack(-side => 'right', -fill => 'y');
    $s1->configure(-command => [$t => 'yview']);
    $s2->configure(-command => [$t => 'yview']);
    
    MainLoop;
    
    __END__

Previous | Return to table of contents | Next