Contents
Perl/Tk Tutorial - Create GUI with Perl's Tk Module
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.
- 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.
- 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

Comments
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!
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.
If not, it means it is very easy to install the module.
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.
Perl/Tk Documentation
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 .
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!!!!!
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
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
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.
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)
Put a # in front of the 6 digit rgb code and enclose it with single quotes.
Ex.
-background=>'#ff0000'
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!
I found it very useful . Refer to the examples in the site to get a better picture
My Thanks
a, strong, em, b, i, code, pre, pandbrallowed. Other tags will be shown as code(< will become <). Urls, Line breaks will be auto-formated.