6.3. The Scrollbar Widget

Instead of automatically creating one or more scrollbars with the Scrolled method, you can use the Scrollbar widget method and perform the configuration yourself. It is better to create and configure your own scrollbars when you need to do something nonstandard, such as having one scrollbar scroll two listboxes.

6.3.1. Creating a Scrollbar Widget

To create the scrollbar, invoke the Scrollbar method from the parent widget. It returns a reference to the newly created scrollbar that you can use for configuration:

$scrollbar = $mw->Scrollbar([ options ...])

There are at least two other things you need to do to get a scrollbar working with another widget. First, create the to-be-scrolled widget and use the scrollbar with its -xscrollcommand or -yscrollcommand option. Then configure the scrollbar so that it knows to talk to that widget. Here's an example that creates a listbox widget (don't worry if you don't quite follow all of this now; I just want to show a complete example before we go on to talk about all the options):

# Create the vertical scrollbar $scrollbar = $mw->Scrollbar(); $lb = $mw->Listbox(-yscrollcommand => ['set' => $scrollbar]); #Configure the scrollbar to talk to the listbox widget $scrollbar->configure(-command => ['yview' => $lb]); #Pack the scrollbar first so that it doesn't disappear when we resize $scrollbar->pack(-side => 'right', -fill ...

Get Learning Perl/Tk now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.