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 ...

Get Advanced Perl Programming 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.