O'Reilly logo

Advanced Perl Programming by Sriram Srinivasan

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Starting with Perl/Tk

All user interfaces developed with Perl/Tk follow this general sequence:

  1. Create a main window (also known as the top-level window).

  2. Instantiate one or more widgets, configure them, and arrange them inside the main window. A widget is simply a collection of data and methods that produce some visible element of the interface, like a button or listbox, and make it behave properly when clicked or otherwise manipulated.

  3. Start the event loop. After this, the user’s actions (events) determine what the program does.

Example 14.1 shows these steps executed in serial order; they result in the simple GUI[54] shown in Figure 14.1.

Example 14-1. Simple UI Code

use Tk;                                          # Slurp the module in.
# -------------------------------------------------------
$top-->title ("Simple");
# -------------------------------------------------------
# -------------------------------------------------------
                 anchor => 'n',                # anchor text to "north"
                 relief => 'groove',           # border style
                 width  =>  10, height => 3);# 10 chars wide, 3 high.

$l-->pack();      # Give it a default place within the main window
# -------------------------------------------------------
# Sit in an infinite loop dispatching incoming events.
MainLoop();
Our first Perl/TK screen

Figure 14-1. Our first Perl/TK screen

This example captures a number of significant Tk concepts (and those of most GUI toolkits in general).

The main window is the outermost shell and ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required