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


Appendix A : About the Author

My name is Binny V A. I have been programming in Perl and Tcl/Tk for a while now - but only recently have I started programming with Perl/Tk. So I am not an expert in Perl/Tk. But I have already written a tutorial for Tcl/Tk. So I thought that I will translate the Tk parts to perl and give the world a new Perl/Tk program. So don't be angry if you see a lot of similarities between Tcl/Tk Tutorial and this Perl/Tk tutorial. I can plagiarise from myself, can't I? Anyway, all questions, suggestions, criticisms etc can be directed to . You can know more about me at my website. For all the programs that I made in Perl, go to the perl page in my site.

If you have liked this tutorial, I have wrote another tutorial on Tcl/Tk language. Check it out.
I also wrote tutorials on CGI-Perl and JavaScript.

Appendix B : Commonly Made mistakes in Perl/Tk

There are a few 'irregularities' at the places where Perl and Tk meet. These places are often a source of problems for new Perl/Tk programmers. Here is a list of most common mistakes made by new Perl/Tk programmers. I made quite a few of them myself.

Calling a function from a button is almost always trouble for the inexperienced. Assume that you created a function called showGreeting. $button = $mw -> Button(-text=>"Hello",-command=>showGreeting); #- Won't work.
$button = $mw -> Button(-text=>"Hello",-command=>showGreeting()); #- Again, won't work.
$button = $mw -> Button(-text=>"Hello",-command=>&showGreeting); #- Will not work.
This is the porper way of doing it.
$button = $mw -> Button(-text=>"Hello",-command=>\&showGreeting); #- Finally. Something that works.
$button = $mw -> Button(-text=>"Hello",-command=>sub { showGreeting(); } ); #- Another method of doing it.

This has a similar effect as the above problem.
$entry = $mw -> Entry(-textvariable=>$var); #- This is not the right way.
$entry = $mw -> Entry(-textvariable=>\$var); #- Correct way.

Using qw//
Don't use a white space inside a value if you are using the qw// method to configure options. See below for more details.

Appendix C : Tcl/Tk And Perl/Tk

If you are more squinted with the Tcl/Tk's way of doing it, you will be happy to know that there is a way of giving the options in the Tcl/Tk style.
$label = $mw -> Label(qw/-text Hello -font courierfont -relief raised/) -> pack();
qw function will split the given string at white space. It can be understood as being roughly equivalent to:
split(' ', q/STRING/);
So the no space is allowed inside values if you are using this. For example, the following line will create an error.
$label = $mw -> Label(qw/-text "Hello World" -font courierfont -relief raised/) -> pack();
Even using quotes(") will give unexpected results.
$label = $mw -> Label(qw/-text "Hello" -font courierfont -relief "raised"/) -> pack();

Appendix D : Codes

Almost all the programs in the tutorial are available in a zipped format for download.

Appendix E : FeedBacks

How do you like this tutorial? Pen some comments below.

If you have any questions send me an e-mail at . If you leave a question in the above feedback form, be sure to give you e-mail address if you want me to respond.

Subscribe to Feed