18. Perl/Tk
Contents:
Perl/Tk is an extension for writing Perl programs with a Graphical User Interface (GUI) on both Unix and Windows 95/NT. Tk was originally developed as an extension to the Tcl language, for use with the X Window System on Unix. With its port to Perl, Tk gives Perl programmers the same control over the graphical desktop that Tcl programmers have taken for granted. The Tk extension makes it easy to draw a window, put widgets into it (such as buttons, checkboxes, entry fields, menus, etc.) and have them perform certain actions based on user input. A simple "Hello World" program would look like this: When you run it, it would look like Figure 18.1 .#!/usr/bin/perl -w use Tk; my $mw = MainWindow->new; $mw->Button(-text => "Hello World!", -command =>sub{exit})->pack; MainLoop; Figure 18.1: A simple Perl/Tk programPushing the "Hello World!" button exits the program, and your window disappears.
Let's walk through these few lines of code.
After calling the Perl interpreter, the program calls the Tk module.
Then it proceeds to build a generic, standard window (
The very last line tells the program to "go do it." 18.1 Widgets
Widgets in Perl/Tk are created with
widget creation commands
,
which include
Positioning widgets is done with
geometry managers
.
In the "Hello World" example shown earlier, the 18.1.1 Widget Methods
Widgets can be configured, queried, or manipulated via various
widget
methods
. For example, all widgets support the
Widget methods are listed in the discussion of each widget
later in this chapter. However, since all widgets support
the 18.1.1.1 The configure method
The To get the value for a current widget, just supply it without a value:$button->configure(-width => 100); The result is an array of scalars; the values you care about are the last two, which represent the default value and its current value, respectively.$button->configure(-width);
You can also call 18.1.1.2 The cget method
For simply retrieving the value of an option, 18.1.2 Scrollbars
Many widgets have scrollbars associated with them. Scrollbars
can be added to a widget in two ways: either using an independent Scrollbar
widget or using the 18.1.2.1 Using the Scrolled method
You use the This creates an Entry widget with an "optional" scrollbar on the bottom. The first argument to$mainwindow->Scrolled('Entry', -scrollbars => 'os' -textvariable => \$address)->pack;
Scrolled
is the type
of widget (in this case, an Entry widget). Then use the
-scrollbars
option to list the location of the scrollbar
("s" for the south, or bottom, edge of the widget). Here, we
specify an "optional" scrollbar with "o", meaning that the
scrollbar will only appear if needed.
Any additional options to the 18.1.2.2 The Scrollbar widget
For more flexibility with a scrollbar, you can use the Scrollbar
widget. To do so, you need to create the target widget to scroll, set
the First, we create the scrollbar with vertical orientation (which is actually the default). Next, we create the Listbox widget with the$scrollbar = $mainwindow->Scrollbar(-orient => 'vertical'); $listbox = $mainwindow->Entry(-yscrollcommand => ['set' => $scrollbar]); $scrollbar->configure(-command => ['yview' => $listbox]); $scrollbar->pack(-side => 'right', -fill => 'y'); $listbox->pack(-side => 'left', -fill => 'both');
-yscrollcommand
option to define a callback when the widget is scrolled vertically.
The scrollbar is then configured with a callback that says to inform
the Listbox widget when it is clicked vertically.
Finally, the Scrollbar and Listbox widgets are packed side-by-side.
See further discussion of the Scrollbar widget later in this chapter
for more information.
18.1.3 CallbacksMany widgets allow you to define a callback , which is a command to execute when the widget is selected. For example, when you press an exit button, the callback might be to a routine that cleans up and quits the program. When you click on a radio button, you might want to change the window to reflect the new preferences.
Widgets that support callbacks have a 18.1.4 Colors and FontsTk was originally created for the X Window System and is still primarily used in that environment. For that reason, it has inherited the font and color scheme used for the X Window System. Colors that can be used with Tk widgets are identified either by an RGB value or by a name that has been associated with an RGB value. In general it is easiest to use a color name rather than an explicit RGB value; for a listing of the color names that are supported, see the rgb.txt file in your X distribution or use the showrgb command. (Most common color names are supported, so you can say things like "red," "pink," "green," and even "chartreuse" with confidence.)
Fonts are another matter. Under the X Window System, fonts
are named things like
-adobe-helvetica-medium-o-normal--12-120-75-75-p-67-iso8859-1
.
Wildcards can make the fonts easier to use, but they're still a
mouthful. For a listing of fonts available for a particular
X server, use the
xlsfonts
command. There are a few font
"aliases" that have been defined for your convenience (such as
|
|