Examples
These examples are included to hopefully clear up any confusion about
using Scrollbars in the real world. Each example uses the
Scrolled
method if possible, then does 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.
Entry Widget
The Entry widget can only be scrolled
horizontally. The Entry can contain only 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
appear only 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-14.
Figure 6-14. Entry widget with a Scrollbar
Listbox, Text, and Canvas Widgets
A Listbox widget can be scrolled both horizontally and vertically, although you might not always ...
Get Mastering 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.