Top level windows

The main window of a Perl/Tk program is created by use of MainWindow, as below.
use Tk;
use strict;
my $mw = MainWindow->new;
# insert widgets into $mw
MainLoop;
# place any subroutines here

Widgets are the various components of the Tk program that interact with the user; the basic types of widgets - buttons, labels, checkbuttons, radiobuttons, scales, entries, text entries, scrollable widgets, menubuttons, menus, and dialogue boxes - we will describe in the next chapter. To give a flavour for the syntax, though, we give a simple example of the use of a ``Button'' widget:

#!perl
# file mw.pl
use Tk;
use strict;
use warnings;
my $mw = MainWindow->new;
$mw->title('Button');
my $exit = $mw->Button(-text => 'Exit',
                       -command => [$mw => 'destroy']);
$exit->pack;
MainLoop;
A screenshot of this window appears below.

Figure 2.2: A simple Perl/Tk program
Image mw

In this example, we begin by creating the main window, with a title via the title method. The button widget, represented by the variable $exit, is created from a call to the Button method. Two options are passed to this call - one, the text option, passes along the string to be used as a label for the button, and the other, the command option, specifies the action to be taken when the button is pressed (in this case, simply exit the program). The pack method, which will be discussed later in this chapter, places the widget in the window.

MainWindow is in fact a special kind of Toplevel widget, of which a given program may have several (for example, a help window may be invoked when a particular button is pressed within the main window). A Toplevel window is created via the following syntax:

my $toplevel = $mw->Toplevel( [ option => value ]);
The various options available, as described in the Tk documentation, are listed below. There is also a title method, used as
$mw->title('Window title');
which may be used to set the title of the window.

As an example of creating a new top level window, consider the following program:

#!perl
# file tl.pl
use Tk;
use strict;
use warnings;
my $tl;
my $mw = MainWindow->new;
$mw->title('File preview');
my $button = $mw->Button(-text => 'Help',
                         -command => \&do_top);
my $exit = $mw->Button(-text => 'Exit',
                       -command => [$mw => 'destroy']);
$button->pack;
$exit->pack;
MainLoop;

sub do_top {
  if (! Exists($tl)) {
    $tl = $mw->Toplevel;
    $tl->title('Help');
    $tl->Button(-text => 'Close',
                -command => sub {$tl->withdraw })->pack;
  }
  else {
    $tl->deiconify;
    $tl->raise;
  }
}
For now don't worry about the use of Button widget - this will be discussed in the next Chapter. What happens in this example is that when the user presses the Help button a new top level window will be created, if it doesn't already exist. This window will be withdrawn when the Close button is pressed within that window. The main window is closed (and the program terminates) when the Exit button is pressed. A screen shot of this program with the new top level window created appears below.

Figure 2.3: Example of creating a top level window
Image tl

Randy Kobes 2003-11-17