O'Reilly logo

Learning Perl/Tk by Nancy Walsh

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

6.4. Examples

These examples are included to hopefully clear up any confusion about how to use scrollbars in the real world. Each example uses the Scrolled method if possible; then we do the same thing manually. We haven't covered all the widget types we are using here, but we aren't doing anything fancy with them either. If you see an option or method you don't recognize, just see the appropriate chapter for that widget to learn more.

6.4.1. Entry Widget

The entry widget can only be scrolled horizontally. The entry can only contain one line of text at most, so a vertical scrollbar would do nothing. Using Scrolled to create a scrolled entry widget is easy:

$mw->Scrolled("Entry", -scrollbars => "s", -width => 30)->pack();

If you want to make the scrollbar only show when the data in the entry widget requires it, use -scrollbars => "os". Using the Scrollbar method is a bit more work:

$scrollbar = $mw->Scrollbar(-orient => 'horizontal');
$entry = $mw->Entry(-width => 30, 
                    -xscrollcommand => ['set' , $scrollbar]);
$scrollbar->configure(-command => ['xview', $entry]);
$scrollbar->pack(-side => 'bottom', -fill => 'x');
$entry->pack(-side => 'bottom', -fill => 'x');

Both will create an entry that looks similar to the one in Figure 6.13.

Figure 6.13. Entry widget with a scrollbar

6.4.2. Listbox, Text, and Canvas Widgets

A listbox widget can be scrolled both horizontally and vertically, ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required