http://www.perlmonks.org?node_id=919261

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

Intro

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.

Widgets

Button

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 :).

Entry

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;

Frame

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

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.

Listbox

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.

Conclusion

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"

--perl.j

Replies are listed 'Best First'.
Re: RFC: Learning Perl/Tk
by zentara (Archbishop) on Aug 08, 2011 at 18:22 UTC
    5. The last line is mandatory Tk programs. It loops through the code and invokes callback responses.

    You left out the "in" before Tk. Also since it is so important to a new GUI programmer to understand that they are dealing with a hidden eventloop, not just some linear code, I think you should emphasize the importance of it more. Some concepts to add:

    The MainLoop is tied to the Mainwindow. Kill the loop, kill the window.

    Never use sleep in a gui program because it will put the loop to sleep and make the whole window unresponsive.

    Other typos: but you knew knew that , Frames are very simple widgets. They are used as a type of border should be "Frames are very simple container widgets; they act as a container for other widgets";

    Besides that, you switched to the grid packing manager, from pack, and don't explain it.

    Uh.. my eyes are strained already. :-) The last thing I will mention, is you make no mention of the Tk "widget" program, which shows all the widgets in action.


    I'm not really a human, but I play one on earth.
    Old Perl Programmer Haiku ................... flash japh
      Amazing feedback. Thank You so much. I will make the changes.
      --perl.j
Re: RFC: Learning Perl/Tk
by toolic (Bishop) on Aug 08, 2011 at 19:48 UTC
Re: RFC: Learning Perl/Tk
by thundergnat (Deacon) on Aug 10, 2011 at 14:39 UTC
    my $button = $mw -> Button(-text => "Quit", -command => sub { exit }) -> pack();

    Arrgh. Don't use a bare exit; in a Tk program, use Tk::exit; It won't matter in this little snippet, but for a larger more complex GUI with multiple windows/widgets Tk::exit; will do a controlled cleanup and shutdown and avoid all kinds of warnings about unreleased resources.

    It might be worth mentioning the Widgets demo program included with Tk to help explain some of the widgets.
Re: RFC: Learning Perl/Tk
by Gulliver (Monk) on Aug 16, 2011 at 20:20 UTC
    Lines 4-7 create a "quit" button that closes the window

    Shouldn't that be lines 4-6?