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, ...

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

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.