Previous
Contents 
Next
Button, Entry, Label 
Perl/Tk Tutorial - Create GUI with Perl's Tk Module
Hello World

Hello World

Let us begin, as all other tutorials begin, with the "Hello World" program. Create a file called "Hello.pl" and enter the following into it.


#!/usr/local/bin/perl
use Tk;
# Main Window
my $mw = new MainWindow;
my $label = $mw -> Label(-text=>"Hello World") -> pack();
my $button = $mw -> Button(-text => "Quit", 
		-command => sub { exit })
	-> pack();
MainLoop;

The first line - #!/usr/local/bin/perl is not needed in windows. In Linux, it tells the name of the script language processor. In our case it is perl. Don't understand what that means? Don't worry your gray cells over it. Just put it at the top of the file.

The second line - use Tk; tells the interpreter that our program will use the Tk module. This line is an absolute must in all GUI programs you make using perl. When the interpreter encounters this line, it will load the Tk components that we will be using to create our program.

The third line - This is a comment. Any line that starts with a '#' char is a comment. Comments are not of any use in the program. It is used by programmer to talk to themselves. A programmer cannot be expected to remember every thing a script does. So he uses a comment to write it down. Next time he edits the script, he can read the comment and understand what the program is for. It is good practice to make as much comments as possible.

The fourth line, my $mw = new MainWindow;, will create a window into which the GUI elements will be placed. The variable $mw is a object of type 'MainWindow'. We will have to use this element when we want to place any widget inside it.

The fifth line - $mw -> Label(-text=>"Hello World") -> pack(); makes a label and writes "Hello world" in it. You can change the text to any thing you like. Note the structure of the command -

$label - This variable assigned to that particular widget. Ever widget must have a UNIQUE variable. This name will be used when ever that widget must be accessed.

$mw -> - $mw is the MainWindow's object. We will be placing our label widget inside this window.

Label(-text=>"Hello World") - 'Label' is the name of the widget. A widget is a user interface object in X graphical user interfaces. Confused? Lets just say that it is the name of the object that appears on screen. There are many other widgets too. If you want to display a button, you use the button widget. For text, you use the text widget. For entry, you guessed it, the entry widget. If you want, you can see more about widgets.

-text=>"Hello World" - The option for this widget. This option says that this widget must be given the text "Hello World". Options change according to the widgets - a button widget will not have all the options of the label widget and vise versa. But there will be many common ones.
Please note that operator used here is '=>' as opposed to the one used earlier '->' in $mw ->. One uses the minus(-) sign while the other uses the equals(=) sign. Do not confuse between these two.
You can keep writing other options can also be written here. For example, let us make a label for showing the text "Hello World". The other lines are same as the Hello World program.

$mw -> Label(-text=>"Hello World",-font=>"courierfont",-relief=>"raised") -> pack();

In this example, a lot more options are used. The font option is used to tell which font must be used to make the text and the relief option tells whether the text should appear raised, sunken, flat etc. To know all the options for a particular widget, read the manual that comes with Perl. It lists every widget and every option they have. If you are going to program in Perl, you will find your self peeking into the manual every few minutes. The most important and most commonly used options are listed here.

All options must separated by a comma. But as you have noted, this line is a little difficult to read. As the number of options increase, the more difficult to read it. So a more readable version is...

$mw -> Label(-text=>"Hello World",
		-font=>"courierfont",
		-relief=>"raised") 
	-> pack();

Next comes the -> pack();. This will pack the widget '$label' into the window '$mw'. 'pack' is a geometry manager. Another geometry manager is 'grid'. Personally, I like grid better. Once again, putting all this in one line is an eye sore - so you can put this part in the next line.


my $label = $mw -> Label(-text=>"Hello World")
	-> pack();

In this case, pack has no options within it. But that is not always the case.

my $label = $mw -> Label(-text=>"Hello World")
	-> pack(-side=>"left",
		-anchor=>'w');

You don't have to pack the widget in the same line of creating it - but it is convenient in small programs. You can pack the widget later using the widget's variable. For example...


my $label = $mw -> Label(-text=>"Hello World"); #We created the widget
$label -> pack(-side=>"left", -anchor=>'w'); #We pack it in another line

So we have the final syntax of how to create and display a widget...
my $WidgetVariable = $Window -> WidgetType(?Option 1=>Value 1, ?Option 2=>Value 2 ...??) -> pack();

The next three lines...

my $button = $mw -> Button(-text => "Quit", 
		-command => sub { exit })
	-> pack();

will create and display a button. Here the widget variable is '$button'. When we look at the options, we will find two options - 'text' and 'command'. The given text is Quit - so the button will have the text "Quit" on it. The command option determines what should happen when the user click on the button. You can specify a function to execute when the user clicks on the button. In this case the program will exit when this button is pressed. One can also call functions that you have created from here...


#!/usr/local/bin/perl

use Tk;
# Main Window
my $mw = new MainWindow;
my $label = $mw -> Label(-text=>"Hello World") -> pack();
my $button = $mw -> Button(-text => "Quit", 
		-command =>\&exitProgam)
	-> pack();
MainLoop;

sub exitProgam {
 	$mw->messageBox(-message=>"Goodbye");
	exit;
}

The next line - MainLoop; is the Main Loop or the Event Loop. Its job is to invoke callbacks in response to events such as button presses or timer expirations. If this line is missing, the program will run and exit with out waiting for the user to do any thing. This is another one of those 'absolute musts' of Perl/Tk programming.

Now Perl puritans will raise a great hue and cry and say that this is not the way to print "Hello World". The "pure" method is the following...

#!/usr/local/bin/perl
print "Hello World"

Putting things in perspective, I am teaching Perl/Tk - not Perl. The above is the Perl method of doing it. My method is the pTk method of doing it.

Previous
Contents 
Next
Button, Entry, Label 
Subscribe to Feed