Every programming language goes through the Hello World example, which is a complete program that prints a string (typically “Hello World”) and exits. “Hello World” may get its share of ridicule, but it’s a remarkably effective tool that shows readers how to write and execute a working program while they’re still in the first chapter of the book. In our Hello World example, we’ll have the title of our window say “Hello World” and create a Button that will dismiss the application:
#!/usr/bin/perl use Tk; my $mw = MainWindow->new; $mw->title("Hello World"); $mw->Button(-text => "Done", -command => sub { exit })->pack; MainLoop;
Despite being only six lines long, there is quite a bit going on in our little program. The first line, as any Perl programmer knows, invokes Perl.[7] The second line tells Perl to use the Tk module.
The third line:
my $mw = MainWindow->new;
is how we create a window. The window will have the same basic window manager decorations as all your other windows.
The title of
our window is changed using the title
method. If we hadn’t used this method, the
text across the top of the window would be the same as the name of
the file containing the code, excluding any extension. For instance,
if the code were stored in a file named
hello_world, the string
“Hello_world” would appear across the title bar of the
application (Tk automatically capitalizes the first character for
you). Using the title
method is not required, but
it makes the application look more polished.
Any string we put as an argument becomes the title. If we wanted the title to be “Hey! Look at my great program!,” this would be the place. This is akin to using the -title option when starting any standard X Windows application. We cover more methods for a MainWindow object later in Chapter 11.
The next line creates a Button widget, sets basic properties, and
packs the widget. (See Chapter 4 for all available
configuration options for Button
.)
The Button is set to display the text “Done” and to
perform the Perl command exit
when pushed.
Finally, the last item of concern is the MainLoop
command. This starts the event loop in motion, and from then on the
application will do only what we have told it to do: if the user
clicks on the Button, the application will exit. Anything else the
user does—minimizing, resizing, changing to other
applications—will be processed by the window manager and
ignored by our application. See Figure 1-2 for a
picture of the Hello World window.
In most of
the examples in this book, you will see sub
{ exit; }
(
or its equivalent, \&exit
) used to quit the
Perl/Tk application. This works fine as long as you have done a
use Tk;
in the same file. Perl/Tk defines its own
exit
routine, which does some cleanup and various
other things that are important to Tk. The program is then
unconditionally terminated, and control returns to the operating
system.
Another way to quit the Tk portion of the
application is to call $mw->destroy
, which
destroys the MainWindow and returns to the code listed after
MainLoop
. This allows your program to do post-GUI
processing before exiting.
[7] On Unix, that is. In Win32 you have to
type perl hello.pl
to invoke the program or
twiddle with Explorer to call up the perl
executable when .pl files are
double-clicked.
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.