9.6. How do I bind the action of a slider (sic) to ... ?

Technically speaking they are called Scrollbars (not sliders) and one must configure the action of the desired widget to call the Scrollbars (i.e. bind is not involved here)

A common task using Scrollbars is to configure things like Canvas, Listbox, or a Text widgets to be updated (change appearance) when the slider of the acompanying Scrollbar is moved by the user.

As an example consider the code that sets up a twelve element Listbox and an accompanying vertical Scrollbar:

    my $main = new MainWindow;
    my $box = $main->Listbox(-relief => 'sunken', 
                             -width => -1, # Shrink to fit
                             -height => 5,
                             -setgrid => 'yes');
    my @items = qw(One Two Three Four Five Six Seven
                   Eight Nine Ten Eleven Twelve);
    foreach (@items) {
       $box->insert('end', $_);
    }
    my $scroll = $main->Scrollbar(-command => ['yview', $box]);
So far so good. But merely setting them up does not mean that the Listbox even knows that the Scrollbar is lying next to it. Note that the scalar variable $scroll is how we refer to the Scrollbar, thus, hooking the $box up to handle $scroll events is a matter of configuration:
 
    $box->configure(-yscrollcommand => ['set', $scroll]);
A complete script that makes use of this code (and adds the necessary calls to pack and MainLoop;) is given as the fifth example in UserGuide.pod (and may be found at http://w4.lns.cornell.edu/~pvhp/ptk/pod/.)

There was an old Perl/Tk tendency to have a bunch of ScrlFoo widgets (such as ScrlListbox). The use of such widgets is now deprecated in favor of a new Scrolled class, as in:

    $w = $patent->Scrolled('Text',...);
The widgets that can be ->Scrolled() include:

Previous | Return to table of contents | Next