Home > Perl > GUI Applications in Perl > Writing GUI Applications Using Perl/Tk - Part 1
Writing GUI Applications Using Perl/Tk - Part 1
Written by Philip L Yuson   
Who is this for
This article is for those who want to use Perl to write a graphical user interface.
What you should know
You should have an idea of Perl programming and modules. If you want to know how to write programs in Perl/Tk, please check the next article.

What is TCL/Tk
Tk was developed by John Ousterhout as part of the TCL scripting language. Tk is a toolkit that allows a programmer to create graphical user interfaces for TCL.

Being trained in the traditional programming languages (COBOL, Visual Basic, Assembler and C), I find TCL quite confusing at first. However, once you get the hang of it, TCL is almost as easy to use as any programming language. The syntax is simpler and the functions are as powerful as any scripting language. Since this article is about Perl/Tk, we will have to cut short our discussion on TCL. If you want to know more about TCL, go to the ActiveState website.

Tk is one of the nicest thing that ever happened to scripting. Tk comes with a lot of built in objects that you can use to create graphical user interfaces. Because of this, a lot of people have developed ways to incorporate Tk into other scripting languages. Since Perl is one of the more popular scripting languages, you would of course expect that Perl would have a Tk module - and it does.

To use Tk in Perl, you will have to check if your Perl system recognizes the Tk module.

Checking for Tk in Perl
If you are using Linux or Unix, you need to start X window and open a console window. If you are using Windows, start a DOS window.

perl -e "use Tk;"

on the command line. In Windows, you might have to go to the directory where you installed Perl (usually perlbin). The Perl compiler should start and process the use Tk; statement. If the Tk module is not installed, the Perl compiler will display an error message.

Installing Tk Module in Windows
If the Tk module is not installed, you will have to install it. In ActivePerl (Windows), you can use the Perl Program Manager (PPM). You have to be connected to the internet to be able to install packages using PPM.

Start a DOS Window and go to the directory where you installed Perl. Then type


on the command line. This runs the ppm program and will display the following prompt: PPM>. Type

install Tk

on the prompt. PPM will go to the internet and fetch the Tk module and install it on your Perl system. This might take several minutes so be patient.

Installing Tk Module in Unix/Linux
You can install Perl/Tk using the CPAN shell. To do this, start the CPAN shell:

perl -MCPAN -e shell

if this is the first time you use CPAN, it will ask you for some questions. Generally speaking, you can use the defaults. But you need to specify your location though. Once CPAN is configured, you can install Perl/Tk by typing:

install Tk

It will go and get the Tk source and do all the steps you have to manually do.

If you want to compile the source, then you will need the following:

  • Perl must be installed (of course)

  • C language compiler

  • X-Windows include libraries

  • linkable libraries for X-Windows

If the Tk module is not in your Linux/Unix system, you will have to install it. Read the License first then download the source from the website and compile it. Make a directory (ptk) in the /usr/local subdirectory. Download the source(Tk800.024.tar.gz) to that (/usr/local/ptk) directory. Do the following:



gunzip -c Tk*.tar.gz | tar xvf -

this unzips the source and restores the files onto their respective directory.

cd Tk800.022

change the directory to the Perl/Tk directory.


The restore file will contain an INSTALL file. Read it for additional information on the installation procedure. Note also the that the file name is all in uppercase

perl Makefile.PL

Let Perl create the Make file.


compiles and links the Tk files.

make test

runs perl/Tk scripts to test the installation process

make install

install the Tk modules in the perl libraries

Once you have done all these, you can check if your installation is correct by trying the steps described in Checking for Tk in Perl section.

For more Information:

Copyright: © 2018 Philip Yuson