Perl Tk has two methods of making scrollbars. One is through the use of the Scrollbar widget, created, as with other widgets, as
my $scrollbar = $mw->Scrollbar( [ option => value ] );The other method, which is easier but not as configurable, is through the use of Scrolled method, as in
my $widget = $mw->Scrolled('WidgetName', -scrollbars => 'value', [ option => value ] );Here, ``WidgetName'' is then name of a widget, such as ``Text'', ``Listbox'', or ``Entry''. The ``value'' of the -scrollbars option is some combination of ``n'', ``s'', ``e'', ``w'', ``on'', ``os'', ``oe'', or ``ow''. The ``o'' preceding the direction means to make the scrollbar optional, so that it will be displayed only if needed. The other options in the above are those for the particular widget itself. Note that, to avaoid some complaints from Tk, if you use a combination of ``n'' or ``s'' with ``e'' or ``w'', use the ``n'' or ``s'' indicator first.
In this Chapter we confine ourselves to the Scrolled method of creating scrollable widgets. As an example of a scrolled widget, we will create a listbox of the names of files in the current directory. The code is as follows.
#!perl #! file scroll.pl use Tk; use strict; use warnings; opendir(DIR, '.') or die "Cannot open '.' for reading: $!\n"; my @files = grep {! -d } readdir DIR; closedir DIR or die "Cannot close '.': $!\n"; my $mw = MainWindow->new; $mw->title('Listbox'); my $label = $mw->Label(-text => 'File listing'); my $lb = $mw->Scrolled('Listbox', -scrollbars => 'osow', -selectmode => 'single'); $lb->insert('end', sort @files); my $exit = $mw->Button(-text => 'Exit', -command => [$mw => 'destroy']); $label->pack; $lb->pack; $exit->pack; MainLoop;Here the scrollable list box is created with the Scrolled method, with the -scrollbars option ``osow'' meaning to create scollbars at the bottom and to the right of the listbox, as needed. A screenshot of this window appears below.
Randy Kobes 2003-11-17