Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

Note: I wrote this for a friend and thought you guys should take a look at it. I tried making it as general and easy as possible. Also, please if I have any HTML problems please tell me. Thank You.

UPDATE: Thanks to jdporter, ww, and zentara's great advise I have made changes regarding wording, thoroughness, and choice of HTML tags. I would also like to thank everyone else who gave feedback.

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"


In reply to RFC: Learning Perl/Tk by perl.j

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    and all is quiet...

    How do I use this? | Other CB clients
    Other Users?
    Others studying the Monastery: (7)
    As of 2018-06-24 23:42 GMT
    Find Nodes?
      Voting Booth?
      Should cpanminus be part of the standard Perl release?

      Results (126 votes). Check out past polls.