Next
Contents 
Perl/Tk Tutorial - Create GUI with Perl's Tk Module
Perl/Tk Tutorial - Create GUI with Perl's Tk Module

Perl/Tk Tutorial - Create GUI with Perl's Tk Module

Perl Programming Republic. Perl/Tk Tutorial - Create GUI with Perl's Tk Module

Perl/Tk (also known as pTk) is a collection of modules and code that attempts to wed the easily configured Tk 8 widget toolkit to the powerful lexigraphic, dynamic memory, I/O, and object-oriented capabilities of Perl 5. In other words, it is an interpreted scripting language for making widgets and programs with Graphical User Interfaces (GUI).

Perl or Practical Extraction and Report Language is described by Larry Wall, Perl's author, as follows: "Perl is an interpreted language optimized for scanning arbitrary text files, extracting information from those text files, and printing reports based on that information. It's also a good language for any system management tasks. The language is intended to be practical (easy to use, efficient, complete) rather than beautiful (tiny, elegant, minimal)."
The perlintro man page has this to say.

Perl is a general-purpose programming language originally developed for text manipulation and now used for a wide range of tasks including system administration, web development, network programming, GUI development, and more.

Tk, the extension(or module) that makes GUI programming in perl possible, is taken from Tcl/Tk. Tcl(Tool Command Language) and Tk(ToolKit) was created by Professor John Ousterhout of the University of California, Berkeley. Tcl is a scripting language that runs on Windows, UNIX and Macintosh platforms. Tk is a standard add-on to Tcl that provides commands to quickly and easily create user interfaces. Later on Tk was used by a lot of other scripting languages like Perl, Python, Ruby etc.

Applications

Perl has been used since the early days of the web to write CGI scripts, and is now a component of the popular LAMP (Linux/Apache/MySQL/Perl) platform for web development. Perl has been called "the glue that holds the web together". Large systems written in Perl include Slashdot, and early implementations of Wikipedia and PHP.

Perl finds many applications as a glue language, tying together systems and interfaces that were not specifically designed to interoperate. Systems administrators use Perl as an all-purpose tool; short Perl programs can be entered and run on a single command line.

Philosophy

Perl has several mottos that convey aspects of its design and use. One is There's more than one way to do it (TMTOWTDI - usually pronounced 'Tim Toady'). Another is Perl: the Swiss Army Chainsaw of Programming Languages. A stated design goal of Perl is to "make easy tasks easy and difficult tasks possible".

Perl is free software, and may be distributed under either the Artistic or the GPL License. It is available for most operating systems but is particularly prevalent on Unix and Unix-like systems (such as Linux, FreeBSD, and Mac OS X), and is growing in popularity on Microsoft Windows systems.

Perl/Tk Requirements

Before starting with the tutorial, make sure you have the following things. If some are missing you still can learn perl - but you will not be able to use it to its full power.

  1. ActivePerl from http://www.activestate.com/ActivePerl/ for windows - for programming in Windows. Linux don't need any special outside interpreter because it already has it in most of the distributions.
  2. A good text editor. I would recommend Crimson Editor(http://www.crimsoneditor.com/) for Windows and XEmacs for Linux.

Installing/Using Perl

In Unix/Linux you can execute your perl scripts by typing "perl <filename>" at command prompt. But before you do that make sure you have both Perl and its Tk module. Most linux distributions have perl - but quite a few don't have the Tk module. Make sure that the system you are using have the Tk module. If you don't have it, go to http://www.cpan.org and download the perl module. Or you can use the perl's CPAN module to install the Tk module. To do this, open a terminal and enter the following command
perl -MCPAN -e shell
cpan> install Bundle::CPAN
cpan> reload cpan
cpan> install Tk

Another(and a much easier) way to do this is to get a rpm of Perl/Tk and installing it with the command
rpm -ivh FILENAME

If you are using Ubuntu, a easy way of installing Perl/Tk is using this command...

sudo apt-get install perl-tk

If you are using Windows, download ActivePerl and install it. Then you can execute any perl file by double clicking it.

Two more things before we begin the tutorial. I will be teaching perl/tk and I expect you to know how to program in perl. I may ignore some of the perl coding conventions like including use strict;, -w or use warnings; in my examples. The examples have only one purpose - to demonstrate the feature that will be taught in that part of the tutorial. Sorry about that - but I have to keep my tutorial's example scripts short and to the point.

Finally, this is a tutorial for Perl/Tk only - I will not be teaching perl here. So if you know perl, continue. But if you are a beginner to perl, I would recommend that you read my perl tutorial.

Perl/Tk Tutorial - Create GUI with Perl's Tk Module
Author - Binny V A()
Bin-Co
Personal Site - BinnyVA.com

Next
Contents 

Comments

Anshuman Atre at 26 Feb, 2007 08:41
Hi,

I have just finished a great tool with many command line options. It also has a basic CLI based user interface to make it interactive.

I happen to a own the O'Rielly "Perl/Tk" that I bought for a steal a few years ago, and am considering adding a GUI to my tool. So I downloaded the Perl/Tk source code from CPAN and failed to compile it using gcc on Solaris. However, it compiled fine on my Linux box.

I tried out a few code snippets and these are my first impressions about Perl/Tk:

1. Yes, it's powerful enough to provide a cool front end to my tool.
2. The coding seems pretty straight forward, though repeatative and monotonous (with lots of trial and error iterations).
3. The end user, however would need Perl/Tk installed on his/her box to able to run my Perl/Tk, which is NOT shipped by default. How do I convince him/her to spend a good part of an hour to get Perl/Tk installed, so that he can use my tool!? It'd be easier for him/her to use the (ample) command line options I give, or use the rudimentary text based interface.

So my question: If I spend a good part of a month writing a Perl/Tk interface, what are the chances that an average (not a programmer/admin) user/operator gets to use it?

Thanks!
Reply to this.
Binny V A at 27 Feb, 2007 05:04
Tk interface, unfortunately, looks really outdated in linux systems. However, in windows systems they look great. If to want cool looking interfaces in Linux, try something like Gtk for perl.

My advice is to provide the GUI along with the application - but after separating the application from the GUI. The app should work without the GUI if that is how the user wants it. The GUI will be a value add.
Reply to this.
Mariano at 27 Mar, 2007 05:22
If the user is currently using a Windows terminal you could generate an exe with your app in wich you can include all the used libraries (i.e. Tk).

If not, it means it is very easy to install the module.
Reply to this.
Anonymous at 14 Mar, 2008 08:37
Use Perl2Exe to compile it into a exe, it automatically includes the Perl/Tk binaries. (If your on windows,otherwise, lul linux fails). Use serials.ws to find a serialfor perl2exe.
Reply to this.
purnachander at 05 Jul, 2007 08:08
Hi,

I'm trying to add GUI to my tool. So i started learning Perl/Tk module. Can you tell me where I can get documentation for different values of an option.

For e.g, The option "-background" is used to set the background color. I know how to set my background to common colors like, green, black,etc.... However, if I'd like to choose my own color from RGB. How can I do it?

Similarly, if i need to know various values available for an option, where should i search for documentation.
Reply to this.
Binny V A at 05 Jul, 2007 11:07
Just search the net - you will get Perl/Tk Docs without much difficulty. For instance...
Perl/Tk Documentation
Reply to this.
Anonymous at 30 Aug, 2007 11:58
I do not see the answer in that documentation. I"m looking for the proper syntax to specify the color in RGB instead of a color name. purnachander, did you ever find it?
Reply to this.
balaji at 30 Jul, 2007 09:46
hi ,
i scripting a tool for vlsi design using perl. But i dont know how to make the gui for it .Can u elaborately tell me wat ar all steps to be carried out .
Reply to this.
Anonymous at 09 Aug, 2007 03:21
if i use this method for installing Tk

perl -MCPAN -e shell
cpan> install Bundle::CPAN
cpan> reload cpan
cpan> install Tk

i am finding this error
Fetching with LWP:
www.perl.org/CPAN/authors/id/N/NI/NI-S/Tk-804.027.tar.gz
LWP failed with code[500] message[read timeout]

can somebody help me pls!!!!!
Reply to this.
Binny V A at 09 Aug, 2007 07:04
Your best bet is just to try again. I think it is a temporary server issue.
Reply to this.
senthil at 04 Sep, 2007 06:10
hi,
i am senthil. i need a code of perl tk. i need to get a input from user using the one input like visual basic. so get value to assign the one variable.
like "$input= ccnb"

ccnb is get from user.
senthil_v@newgenimaging.com
Reply to this.
Binny V A at 04 Sep, 2007 09:11
The details are available in the Entry page
Reply to this.
aflexo at 11 Dec, 2007 04:07
Great samples in tutorial! These just helped me to understand the whole thing about Perl/TK programming))
Reply to this.
dsw at 18 Dec, 2007 08:50
This interface does not look outdated. If you are looking for something more polished, then I suggest you write a tutorial about using QT. Oldschool is the only school :-)
Reply to this.
Binny V A at 18 Dec, 2007 11:08
Unfortunately, you are right - in the case of Linux. Tk interface in Linux is just horrible. But it looks rather good in Windows. Plus - I have not looked at QT yet - I have learned a bit of Gtk - but nowhere enough to write a tutorial.
Reply to this.
PhillC at 10 Jan, 2008 07:21
First, thanks for a great tutorial using PerlTK. Your style and examples work for me very well.

I'm embarking on my first Perl GUI programming experience. After investigating a number of options such at GTK2 and wxWindows, I think Tk appeals to me most. While the PerlTk GUI look and feel may be a little outdated, I like the syntax used to create it. I especially like the grid packaging system.

I've been reading about the Tcl::Tk module currently available in CPAN. It seems that it has some advantages over PerlTK, specifically speed and the ability to use the latest Tcl/Tk 8.5 widgets such as Tile. It also appears to support PerlTK syntax, but of course there are some differences.

I was wondering if you'd consider a tutorial highlighting the Tcl::Tk module vs PerlTk differences, and especially which widgets are included in Tcl:Tk by default and how to call external Tcl widgets. In fact, maybe just a single page explaining these differences might be really useful.

Regards,
Phill


Reply to this.
Binny V A at 13 Jan, 2008 07:21
I'll look into it - but chances are that I will not be able to do this study. Currently I am in Web Development - I have not been active at the Tk front. So, my knowledge about those systems are a bit outdated. Anyway, thanks for the suggestion - I will look into it.
Reply to this.
Anonymous at 27 Feb, 2008 04:58
assigning a variable to backgrounds and things like that
textvariable is for text, but I cannot find a similar entry for frames or anything else outside of text in a label
I get a message that says unknown color name "SCALAR(0x18a3f6c)" if I use code similar to $frame=$mw->Frame(-background=>\$variable)->pack();
but if I use $frame=$mw->Frame(-background=>$variable)->pack(); without the slash the widget is assigned to the one color permanently
this is a significant problem to me and I would appreciate someone else trying to solve this, because I have been unable to find Docs. myself to assist me.
Reply to this.
Chanio at 03 Mar, 2008 05:20
I appreciate very much your great and honest work!
This complete tutorial is a very helpful tool for both: the users and the perl/Tk community.

Wouldn't it suit better if your site had a wiki style?
I imagine perldocs published on-line in a wiki fashion, all full of hyperlinks...

THXS! Have a nice year!
Alberto (Argentina)
Reply to this.
Anonymous at 14 Mar, 2008 06:23
Finally figured out how to specify rgb colors for widgets. Posting it here in case anybody else is looking.

Put a # in front of the 6 digit rgb code and enclose it with single quotes.

Ex.

-background=>'#ff0000'

Reply to this.
Kumar P at 03 Apr, 2008 06:45
I have an assignment to create a GUI, which should be able to parse the MPEG Video Bit Stream and show us various properties/semantics of the underlying video. Is perl/TK the right choice for developing such application?
Reply to this.
Jefffrey at 04 Apr, 2008 10:35
I've started writing a database and point of sale front end for my dive shop. I'm using perl because it's the only language I'm somewhat fluent in. I have used Tk before but not Gtk2. Can anyone tell me if there is enough info out there to get the hang of Gtk2 fairly quickly or is there a learning curve
Reply to this.
Chanio at 10 Apr, 2008 08:04
If someone wants to get a more VB-like result with Tk you might want to use the free: Zooz GUI builder .

It is not better than doing it all by coding. It is just another way of complementing your knowledge of Perl-Tk... And, perhaps, learning something new!

Reply to this.
Rathnakar at 21 Apr, 2008 09:41
To get Gui you can use perl/Tk modules which would give more flexibility ..
I found it very useful . Refer to the examples in the site to get a better picture
Reply to this.
Comment


Comment




Comment Formating : HTML tags a, strong, em, b, i, code, pre, p and br allowed. Other tags will be shown as code(< will become &lt;). Urls, Line breaks will be auto-formated.
Subscribe to Feed