Home > Perl > GUI Applications in Perl > Writing GUI Applications using Perl/Tk - Part 2
Writing GUI Applications using Perl/Tk - Part 2
Written by Philip L Yuson   
Who is this for
This article is for those who want to write Perl programs with graphical user interfaces (GUI).
What you should know
You should be familiar with the Perl programming language. Knowledge of Tk concepts will definitely help.

Tk Concepts
Tk is a toolkit that comes with widgets that you use to build graphical user interfaces. A widget can be a Button, Entry, Listbox or Text. Each of these widgets has options that the programmer can define to make it function in a certain way.

To put a text on a pushbutton, you will have to define the -text option with the string you want. If you want to right justify the text, you define this in the -justify option.

Tk has a Geometry Manager that manages where these widgets are to be displayed on the window. To define where you want the widgets to be placed, you need to use the grid or pack functions of the geometry manager.

Another thing you have to know about Tk is binding. Binding allows you to perform a set of instructions when an event occurs on a specific widget. You define this through the bind function.

Your First Perl/Tk Program
My motto is always "learn by doing." So to give you an idea of how a Perl/Tk program looks like, let us write a simple program.

Make sure that you have the Tk module installed. If you don't know how to do this, please read part 1 of this article. Let us now assume that everything is ok. You have your Perl system and you have your Tk module installed.

Start a text editor (Notepad in Windows and vi in Linux or Unix). Below is the entire Perl/Tk code. The code creates a label and Button. On clicking the button, the program displays "Congratulations! it works!"


# 1 Standard line in Linux/Unix

use Tk;

# 2 Include the Tk Module for this program

my $top = new MainWindow;

# 3 Define the main window


# 4 Set the Title of the window to "Test"

my $lab = $top->Label(-textvariable=>\$labelText);

# 5 Create the Label. Display the contents of $labelText

my $b = $top->Button(-text=>'Click Me',
-command=>sub {$labelText="Congratulations! it works!" });

# 6 Create the Button. When clicked, set $labelText to "Congratulations, it works!"

$lab->grid(-row=>0, -col=>0);

# 7 Display the label on the first row and first column of the window

$b->grid(-row=>1, -col=>0);

# 8 Display the button on the second row and first column of the window


# 9 You need this to allow the GUI program to wait for the next event

Line 1 is standard when the program is run in Unix/Linux. It tells the shell to execute Perl first before processing the next lines.

Line 2 specifies that you will need the Tk module.

Line 3 creates the Main Window. All GUI programs have a main window where the widgets are to be placed. The Main Window is identified in the $top variable.

Line 4 sets the title of the Main Window to "Test". You can change this to whatever title you want for your Main Window. Note that the $top variable is used to identify the main window. One thing I find confusing is the use of -> and =>. One trick I discovered is that setting a value to an option can be associated to setting a value to a variable. To set a variable, you use the = sign right? So to set a value for an option, use the =>. If you are not setting a value to an option then use ->.

Line 5 creates the Label widget. It tells Tk to get the text to be displayed from the $labelText variable. Note that the reference of the variable is passed \$labelText instead of the variable itself. (This is one of the confusing part I found in Perk/Tk.) Since $labelText is not set, the Label will not have any text on it.

Line 6 creates the Button widget. The text displayed on the button is Click Me. You can change this to whatever you want. On clicking the button, the statements in the -command option are executed. Note that the value of the -command option is an anonymous subroutine. It sets the $labelText to Congratulations! It Works!.

Setting this variable to another value will cause that value to be displayed on the label.

Also note that the variable this time is used and not the reference of the variable.

Lines 7 and 8 defines where you want to place the two widgets. By using the grid method, the Geometry Manager will divide the Main Window into a grid. The first row will be -row=>0. The first column will be -col=>0.

Thus line 7 puts the Label on the first row (-row=>0) and first column (-col=>0) of the grid.

Line 8 puts the Button on the second row (-row=>1) and first column (-col=>0) of the grid.

Line 9 is needed to allow the GUI program to function properly. Removing this will cause the script to end.

Check out

  • my Widget Page for a Sample Calendar Program or the same program converted into a Calendar object.

  • To learn more on how to create composite Widgets, read the Next article

For more information on writing Perl/Tk programs, you can check out:

  • My Perl/Tk Tutorial. This includes a Perl/Tk Concept Tutor script that allows you to create some basic widgets manipulate their options.

Copyright: © 2018 Philip Yuson