Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number

Beginning Perl/Tk

by perl.j (Pilgrim)
on Aug 27, 2011 at 21:35 UTC ( #922840=perltutorial: print w/replies, xml ) Need Help??

Learning Perl/Tk


Perl/Tk is probably the most widely known GUI for Perl. It is a great interface used by thousands of people. I am writing this to teach those with no previous knowledge of any programming language or GUI interface. You should know some Perl (you don't need to be an expert, but you need to know some basic/intermediate Perl) before you read this also. Perl/Tk is a module, so it should be fairly easy to get. If you don't know how to install modules, see A Guide to Installing Modules.

Beginning Perl/Tk

Ok. Let's run a simple Perl/Tk program.

##Run this program and see what happens use Tk; my $mw = new MainWindow; my $label = $mw -> Label(-text=>"Hello World") -> pack(); my $button = $mw -> Button(-text => "Quit", -command => sub { exit }) -> pack(); MainLoop;

Let's take this line by line:

  1. The first line, use Tk, tells your interpreter to use Tk. Without this line, the whole script would fail.
  2. The second line, my $mw = new MainWindow, is creating the window you are going to put your widget(s). This is also mandatory. Without this, you would have no where to put your widget(s)!
  3. The third line, my $label = $mw -> Label(-text=>"Hello World") -> pack(), creates the text on your window. If this line were my $ent = $mw -> Entry() -> pack(), it would have created an entry widget (you'll see an entry widget later in the tutorial). Here is a complete list of widgets.
  4. Lines 4-6 create a "quit" button that closes the window. There will be more in detail explanation of this later in the tutorial.
  5. The last line is mandatory in Tk programs. It loops through the code and invokes callback responses.



The button widget creates, you guessed it, a button! Some people learn by example. So if you need one, look at my first example. Lines 4-6 create a button that says "Quit". If you press the button, it ends the program. Of course, you could always change the subroutine in the code to make the button do whatever you want (you can also change the text on the button, but you knew knew that :).


The entry widget makes a one-line text box the user can type into. Here's an example:

use Tk; my $mw = new MainWindow; my $ent = $mw -> Entry() -> pack(); MainLoop;


Frames are very simple container widgets;they act as a container for widgets. Here is a great example of using frames that I found on Bin-Co:

use Tk; #Global Variables my $age = 10; my $gender = "Male"; # Main Window my $mw = new MainWindow; #GUI Building Area my $frm_name = $mw -> Frame(); my $lab = $frm_name -> Label(-text=>"Name:"); my $ent = $frm_name -> Entry(); #Age my $scl = $mw -> Scale(-label=>"Age :", -orient=>'v', -digit=>1, -from=>10, -to=>50, -variable=>\$age, -tickinterval=>10); #Gender my $frm_gender = $mw -> Frame(); my $lbl_gender = $frm_gender -> Label(-text=>"Sex "); my $rdb_m = $frm_gender -> Radiobutton(-text=>"Male", -value=>"Male", -variable=>\$gender); my $rdb_f = $frm_gender -> Radiobutton(-text=>"Female", -value=>"Female",-variable=>\$gender); my $but = $mw -> Button(-text=>"Push Me", -command =>\&push_button); #Text Area my $textarea = $mw -> Frame(); my $txt = $textarea -> Text(-width=>40, -height=>10); my $srl_y = $textarea -> Scrollbar(-orient=>'v',-command=>[yview => $t +xt]); my $srl_x = $textarea -> Scrollbar(-orient=>'h',-command=>[xview => $t +xt]); $txt -> configure(-yscrollcommand=>['set', $srl_y], -xscrollcommand=>['set',$srl_x]); #Geometry Management $lab -> grid(-row=>1,-column=>1); $ent -> grid(-row=>1,-column=>2); $scl -> grid(-row=>2,-column=>1); $frm_name -> grid(-row=>1,-column=>1,-columnspan=>2); $lbl_gender -> grid(-row=>1,-column=>1); $rdb_m -> grid(-row=>1,-column=>2); $rdb_f -> grid(-row=>1,-column=>3); $frm_gender -> grid(-row=>3,-column=>1,-columnspan=>2); $but -> grid(-row=>4,-column=>1,-columnspan=>2); $txt -> grid(-row=>1,-column=>1); $srl_y -> grid(-row=>1,-column=>2,-sticky=>"ns"); $srl_x -> grid(-row=>2,-column=>1,-sticky=>"ew"); $textarea -> grid(-row=>5,-column=>1,-columnspan=>2); MainLoop; ## Functions #This function will be executed when the button is pushed sub push_button { my $name = $ent -> get(); $txt -> insert('end',"$name\($gender\) is $age years old."); }

I haven't got to all of the widgets and features in this program yet but we'll get there : ).


Scales are, well, scales. There is not an easy way to explain them so I'll let you see one. Look at the frame on the left in the example for frames. That is a scale where you could pick your age.

Radiobutton & Checkbutton

A radiobutton is used to pick certain choice (usually). Once again I am going to refer you to the example for the frames widget. Do you see where you are supposed to pick a gender. That is a radiobutton.

A checkbutton is just like a radiobutton, it just looks different. If you look at the example for frames, you'll see both. The area where you pick your occupation is the Checkbox.


A listbox displays a list of strings, one per string. To see an example of a listbox, see the frames example. The adding jobs section is a listbox.

There are more widgets (even some that were in the example that I didn't show you) but this should give you a big kick start.

Things You May Not Have Understood

Let's go over some things you may not have understood. First off, pack(). The basic idea of pack()is that any window or widget should be subject to a Tk geometry manager; the packer is one of the placement managers. For more on Geometry Managers,see this webpage.

MainLoop. It is not an easy thing to explain in a small area so I wil refer you to this webpage.

What is a GUI? GUI stands for Graphical User Interface. That is, a user interface based on graphics (icons and pictures and menus) instead of text.

Other Things You Should (and Should Not) Do

1) Never kill the MainLoop! Kill the loop, kill the window.

2) Never use sleep in a GUI program because it will put the loop to sleep and become unresponsive.


Perl/Tk is a powerful, useful, easy-to-use tool. I hope this gave you a good start on how it is used.

See Also...

Tk on CPAN, Tk Tutorial, Featuring Your Very Own "Perl Sig/OBFU Decoder Ring"

Replies are listed 'Best First'.
Re: Beginning Perl/Tk
by Anonymous Monk on Mar 25, 2014 at 03:19 UTC

    Hi! The above is mostly excellent. One thing I was here is that there is mention of a checkbox and a listbox in the frames example, but it is not there. For whatever reason, your text is referring to an area where occupation is entered, and it's not in the code. :-(

    Hope this helps.

    Kind regards,


Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perltutorial [id://922840]
[choroba]: And they call it a "dry season"!

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (9)
As of 2017-02-20 08:00 GMT
Find Nodes?
    Voting Booth?
    Before electricity was invented, what was the Electric Eel called?

    Results (294 votes). Check out past polls.