|
|
|
|
|
Figure 6-13.
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 want to use both options. If you know how wide your data is going to be and the window can accommodate it, a horizontal scrollbar is unnecessary. Our first example uses the Scrolled method and creates two scrollbars: |
|
|
|
 |
|
|
|
|
$mw->Scrolled("Listbox", -scrollbars => "se",
-width => 50, -height => 12)->pack(); |
|
|
|
|
|
|
|
|
To do the same thing manually, we need to use Scrollbar to create two scrollbars and configure them to work with the widget: |
|
|
|
 |
|
|
|
|
$f = $mw->Frame()->pack(-side => 'top', expand => 1, -fill => 'both');
$xscroll = $f->Scrollbar(-orient => 'horizontal');
$yscroll = $f->Scrollbar();
$lb = $f->Listbox(-width => 50, -height => 12,
-yscrollcommand => ['set', $yscroll],
-xscrollcommand => ['set', $xscroll]);
$xscroll->configure(-command => ['xview', $lb]);
$yscroll->configure(-command => ['yview', $lb]);
$xscroll->pack(-side => 'bottom', -fill => 'x');
$yscroll->pack(-side => 'right', -fill => 'y');
$lb->pack(-side => 'bottom', -fill => 'both', -expand => 1); |
|
|
|
|
|
|
|
|
As you can see, using Scrolled saves a lot of extra work. In Figure 6-14, we see a listbox with two scrollbars, one on the south and one on the east. This window was created using Scrolled. There is a subtle difference: the small square of open space where the two scrollbars meet in the southeast corner. When we create the scrollbars ourselves, we don't get that small space (whichever scrollbar gets packed first takes it). |
|
|
|
|
|
|
|
|
Scrolled text and canvas widgets are created the same exact way a scrolled listbox widget is created, so we won't bother repeating the same code again. |
|
|
|
|
|
|
|
|
One Scrollbar, Multiple Widgets |
|
|
|
|
|
|
|
|
There are times when you want to use one scrollbar with more than one widget. When the user clicks on the scrollbar, it should scroll all the widgets in the same direction at the same time. For this example, we will create three listboxes, each |
|
|
|
|