Perl/Tk

Getting Perl/Tk

You can get the source distribution of Perl/Tk from CPAN, at http://www.perl.com/CPAN/modules/by-module/Tk/.

You should also be able to get the distribution (and compile, test, and install it) using the CPAN module. If you are running a recent version of the CPAN module, you should just be able to run cpan, or you can always do perl -MCPAN -e 'shell;'.

Compiling Perl/Tk

You compile and install Tk just like you would any other Perl module. (It just takes longer than most. ;)

Start by unpacking the distribution. On Linux (or using GNU tar), just do tar zxvf Tk*.tar.gz. With other tars, do gzip -cd Tk*.tar.gz | tar xvf -. Go into the directory that is created and run perl Makefile.PL, then make, make test, and make install.

Examples

Hello, World!

#!/usr/bin/perl

use Tk;

$mw=MainWindow->new;

$mw->Label(-text => "Hello, World!")->pack;

MainLoop;


Simple Entry example

#!/usr/bin/perl

use Tk;

$mw=MainWindow->new;

$mw->Entry(-textvariable => \$foo)->pack;
$mw->Label(-textvariable => \$bar)->pack;
$mw->Button(-text => "test", -command => sub { $bar=$foo; })->pack;

MainLoop;

Using pack

pack is a geometry manager. Other geometry managers include grid and place. pack is by far the easiest to use. It has many options to control placement of widgets, including -anchor, -side, -expand, and -fill.

#!/usr/bin/perl

use Tk;

$mw=MainWindow->new;

$mw->Button(-text => "Top")->pack(-side => "top");
$mw->Button(-text => "Left")->pack(-side => "left");
$mw->Button(-text => "Right")->pack(-side => "right");
$mw->Button(-text => "Bottom")->pack(-side => "bottom");

MainLoop;


There are more examples at http://www.silug.org/~steve/software/.

steve@silug.org