Introduction to Perl/Tk

Introductory perl/Tk, based on the 2 March 1999 dc perlmongers meeting.

Author: Raul Miller <moth@magenta.com>

This is an initial draft which focusses on the example scripts written for that meeting.

Note that these were written on an windows NT system, so are missing the #!/usr/bin/perl line -- add it in if you need it. [For the meeting, they were all invoked using the: perl filename syntax.]


proto.pl

use Tk;
$w= MainWindow->new;

MainLoop

  • just about the simplest Tk script
  • if this doesn't work, Tk isn't installed properly.


button

use Tk;
$w= MainWindow->new;

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

MainLoop

  • A Tk program that "actually does something"... [exits]

label

use Tk;
$w= MainWindow->new;

$w->Label(-text=>"This is some text")->pack;

MainLoop

  • Labels are handy for new users

check

use Tk;
$w= MainWindow->new;

$w->Checkbutton(-text=>"AAAA", -variable=>\$aaaa)->pack;
$w->Checkbutton(-text=>"WWWW", -variable=>\$wwww)->pack;

$w->Label(-textvariable=>\$aaaa)->pack;
$w->Label(-textvariable=>\$wwww)->pack;

MainLoop;

  • The checkbutton display shows the state of the referenced scalar, and clicking on the button toggles it's state between true and false.
  • Here, the labels are also configured to also show the state of the referenced scalars.

check1

use Tk;
$w= MainWindow->new;

$w->Checkbutton(-text=>"AAAA", -variable=>\$aaaa)->pack;
$w->Checkbutton(-text=>"WWWW", -variable=>\$wwww)->pack;
$aaaa=1;

$w->Label(-textvariable=>\$aaaa)->pack;
$w->Label(-textvariable=>\$wwww)->pack;

MainLoop;

  • Minor variation, showing that assigning a value to a significant scalar also updates the display.

radio

use Tk;
$w= MainWindow->new;

$w->Radiobutton(-text=>"AAAA", -variable=>\$aaaa, -value=>1)->pack;
$w->Radiobutton(-text=>"WWWW", -variable=>\$aaaa, -value=>2)->pack;

$w->Label(-textvariable=>\$aaaa)->pack;


MainLoop;

  • Internally, radio buttons are analogous to checkbuttons, but instead of toggling true/false they each correspond to a single value.
  • Thus: radiobuttons sharing the same scalar ref form a group.

radio1

use Tk;
$w= MainWindow->new;

$w->Radiobutton(-text=>"AAAA",
         -variable=>\$aaaa,
         -value=>1)->pack(-anchor=>'w');
$w->Radiobutton(-text=>"WWWW",
         -variable=>\$aaaa,
         -value=>2)->pack(-anchor=>'w');

$w->Label(-textvariable=>\$aaaa)->pack;


MainLoop;

  • If a group of buttons are arranged vertically, you probably want to align them on the west side of their container -- this tends to look "neater" than centering them independently.

entry

use Tk;
$w= MainWindow->new;

$w->Label(-textvariable=>\$text)->pack;

$entry=   $w->Entry->pack;
$entry->bind('', sub{$text= $entry->get});

MainLoop

  • One of the simplest widgets for entering complex data
  • Also note that we're using "bind" -- this lets you use events such as keystrokes.
  • The event names used by "bind" are defined by X-Windows, so are typically not documented as a part of Tk. On a unix system you can use "xev" on a live system to get event names.

browse

use Tk;
$w= MainWindow->new;

$w->Label(-textvariable=>\$text)->pack;

$entry=   $w->BrowseEntry(-variable=>\$text)->pack;
map {$entry->insert('end',$_)} 'this', 'is', 'a', 'test';

MainLoop

  • A variation on the entry widget which lets you supply a list of plausible answers.
  • This one is smart enough to update its scalar reference every time you enter a keystroke

checkhelp

use Tk;
$w= MainWindow->new;
$help= $w->Balloon;

$c1= $w->Checkbutton(-text=>"AAAA",
         -variable=>\$aaaa)->pack;
$help->attach($c1, -msg=> "This check button says AAAA");
$c2= $w->Checkbutton(-text=>"WWWW",
         -variable=>\$wwww)->pack;
$help->attach($c2, -msg=> "This check button says WWWW");

$label_1= $w->Label(-textvariable=>\$aaaa)->pack;
$help->attach($label_1, -msg=> "see AAAA, above");
$label_2= $w->Label(-textvariable=>\$wwww)->pack;
$help->attach($label_2, -msg=> "see WWWW, above");

MainLoop;

  • Pop-up help is nice for new users
  • Note that the same "help widget" is used for a variety of messages.

dialog

use Tk;
$w= MainWindow->new;

print $w->Dialog(-text=>'hey, there!',
           -title=>'hey',
           -buttons=>['a','b','c']
)->Show;

print "\n";

  • Modal dialog
  • Note that there's no MainLoop here
  • Note that the return value from Dialog's Show method tells you which button was pushed.
  • While it wasn't used here, you can use the withdraw method of the main window to hide the window (and the deiconify method of that main window to make it reappear).

fileselect

use Tk;
$w= MainWindow->new;

print $w->FileSelect->Show;

print "\n";

  • Another modal dialog -- this one lets you pick a file name from the file system.
  • You'd probably want to have tell this one which directory to start out in, but there's no portable way that I know of to get the current working directory. [Under unix, you can use: substr `pwd`, 0, -1]

mess

use Tk;
$w= MainWindow->new;

$w->Message(-text=>"This is some text")->pack;

MainLoop

  • Another way of displaying information -- this one tries to achieve a nice aspect ration (aspect ratio is the ratio between width and height).

t

use Tk;
$w= MainWindow->new;

$t= $w->Text->pack;
$t->insert('end', q{
This is some text








it goes on for a while




...





...
....












the end});

MainLoop

  • Your basic "editor" widget.
  • See also "TextUndo" which adds an undo mechanism and methods for reading and writing a file.
  • No scrollbars here, but you can typically "scroll" the text around by dragging with the middle mouse button [or by holding down both buttons simultaneously on a two button mouse].

t1

use Tk;
$w= MainWindow->new;

$t= $w->Scrolled('Text')->pack;
$t->insert('end', q{
This is some text








it goes on for a while




...





...
....












the end});

MainLoop

  • You can put scroll bars on any widget by having the Scrolled widget create it for you -- the first argument to Scrolled is the class of the widget you wish to create.