Copyright (c) 1995-1996 Nick Ing-Simmons. All rights reserved. This package is free software; you can redistribute it and/or modify it under the same terms as Perl itself, with the exception of the files in the pTk sub-directory which have separate terms derived from those of the orignal Tk4.0 sources and/or Tix. IN NO EVENT SHALL THE AUTHORS OR DISTRIBUTORS BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OF THIS SOFTWARE, ITS DOCUMENTATION, OR ANY DERIVATIVES THEREOF, EVEN IF THE AUTHORS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. THE AUTHORS AND DISTRIBUTORS SPECIFICALLY DISCLAIM ANY WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND NON-INFRINGEMENT. THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, AND THE AUTHORS AND DISTRIBUTORS HAVE NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. See pTk/license.terms for details of this Tk license, and pTk/Tix.license for the Tix license.
This compilation of Frequently Asked Questions & answers (FAQ) is intended to answer several of the first (and mostly more basic) questions posted to the comp.lang.perl.tk newsgroup and the ptk mailing list.
This document concerns itself with the Perl/Tk programming language (or, if you prefer, the Tk extension to Perl). Please beware that this is not the Perl FAQ, this is not the Tcl FAQ, nor is this the Tcl/Tk FAQ. Each of those other documents are useful sources of information for Perl/Tk programmers but are completely different from this one.
This is a dynamic document and contributions, clarifications, and corrections are most welcome! Please send e-mail to <pvhp@lns62.lns.cornell.edu>. With your help this document will get better (-:
Return to table of contents
Perl/Tk (also known as pTk or ptk) is a collection of modules and code
that attempts to wed the easily configured Tk 4 widget toolkit to the
powerful lexigraphic, dynamic memory, I/O, and object-oriented capabilities
of Perl 5. In other words, it is an interpreted scripting language for
making widgets and programs with Graphical User Interfaces (GUI).
(Examples of widget programs [not necessarily written in perl/Tk]
include xterm, xclock, most web-browsers,
etc.. They are programs with "GUI" interfaces of one
sort or another and are subject to the effects of your window manager.)
The current release of Perl/Tk is based on "Tk 4.0p3" the widget Toolkit originally associated with the Tcl (Tool command language) scripting language. However, Perl/Tk does not require any of the lexical features/idiosynchrocies of Tcl. Perl/Tk uses perl 5 syntax, grammar, and data structures.
Like perl, wish,
and even tclsh; perl/Tk is most easily used on a Unix computer
platform running X. Having said that, it is worth noting that things like
Linux, OS/2, and FreeBSD mean that the micro-computer hardware crowd too can
have access to the wonders of perl/Tk. Perl and Tcl/Tk have also been ported to
many non Unix operating systems, and perl/Tk has been ported to OS/2, hence
there are plenty of reasons to believe that the perl/Tk code will be more
widely ported in the near future (such work has already started).
The ``Tk400.200'' package is the production release of perl/Tk (corresponding to Tcl/Tk-4.0p3) and was written primarily by Nick Ing-Simmons <Nick.Ing-Simmons@tiuk.ti.com> at Texas Instruments in Northampton, England, to work with the latest version of Larry Wall's ``perl''. Nick Ing-Simmons is currently busy converting the Tcl/Tk-4.1 code to perl callable code as well.
The pTk code proper is an externally callable Tk toolkit (i.e. a re-write of the Tk 4.0 code that allows easier external linking & calling, especially by perl). Ptk can then be called from Perl 5 via the Tk.pm et al perl glue modules. Hence "ptk" does not necessarily refer to Perl Tk but could be taken to mean portable Tk - given a glue package to another language. The stated goal of the pTk code is to have its library usable from perl, Tcl, LISP, C++, python, etc.. It just so happens that present work is concentrating on perl.
Historical note: "ptk" was known as "ntk" before about 11:30 EST 4 May 1995.
The perl/Tk language is itself further extensible via the standard perl 5 module mechanism. A number of composite widget and special character extensions to the language have been written using perl modules.
Return to table of contents
2. What is the difference between perl/Tk and Tkperl?
TkPerl was originally the name of a (now unsupported) perl 4 package that Malcolm Beattie <mbeattie@sable.ox.ac.uk> at Oxford University gave to his code to wed the Tk X toolkit with Perl. (He has since referred to that package as a different "kettle of fish" from perl/Tk.)
Since that beginning Malcolm has also come up with a Tcl module for perl 5 that has a Tcl::Tk module extension. That module allows the use of Tcl within a Perl script (i.e. you must know both languages to get your widgets to work.) If you are interested in that package instead, see the necessary kits for Malcolm Beattie's Tcl/Tk extensions to Perl, which have been distrubuted as Tcl-b#.tar.gz and TclTk-b#.tar.gz files in the authors/id/MICB/ directory at CPAN sites (locations given in a separate question in this FAQ).
The name "tkperl" is sometimes applied to the "perl/Tk" or "ptk" package that is the subject of this FAQ. Nick Ing-Simmons prefers "perl/Tk" as the name of the package, with "pTk" or "ptk" as contractions of that name as well as referring to something technically distinct: given the extensibility of the pTk code the "p" could also be taken to mean 'portable' or 'pure' (not to be confused with either the Helsinki University of Technology portTk, nor with Brian Warkentine's Rivet). In this document the code of interest is either referred to as "perl/Tk", "pTk", or "ptk" though the primary focus is on perl/Tk.
Warning: do not attempt to install both perl/Tk and Tcl/Tkperl in the same perl installation. The names in the respective modules overlap. In particular the Tcl::Tk module is declared in a Tk.pm file - so a statement like:
use Tk;
will probably confuse your perl. If you cannot live without either module
then install make & maintain separate perls for each and arrange your
script writing accordingly (this will not be easy).
A more extensive comparison of the differences between the Tkperl and the perl/Tk code is given in the Tcl-perl.pod file that is distributed with perl/Tk (see below for locations).
Lastly, it should be mentioned that if you build your perl/Tk statically rather than dynamically it will make a new perl interpreter called tkperl (confusing isn't it? :-).
Return to table of contents
(This question remains for historical reasons as well as to discuss things that have not quite made it to CPAN.)
The Tk kits used to be available from
ftp://ftp.wpi.edu/perl5/ 130.215.24.209
[ However, visiting the CPAN sites (discussed in the
next question) should provide
you with all that you need (including other perl modules).]
Please note that if you will be attempting to build with the old Tk-b9.01 then you will probably also want Nick's document patch to go with that older kit, it is available from:
http://w4.lns.cornell.edu/~pvhp/ptk/etc/ptkpod-b9.01.patch
To construct Perl 5.003++ go to a CPAN site and
pick up the full kit (it will have some mysterious name like perl5.003.tar.gz
or something).
As of November 1995 Tk kits (various versions) are being distributed at CPAN ftp sites (see later question in this FAQ) in the authors/id/NI-S/ directory.
Tk-b8: For this older version you must have Perl (5.001m) (not n) and the ptk kit. The Tk-b8 kit is at CPAN sites, as well as from:
USA
ftp://ftp.wpi.edu/perl5/Tk-b8.tar.gz 130.215.24.209
ftp://ftp.perl.com/pub/perl/ext/tk/ 199.45.129.30
http://www.freebsd.org/ports/programming.html (pick up "pTk-b8")
http://www.metronet.com/perlinfo/perl5/extensions/Tk-b8.tar.gz
UK
ftp://ftp.ox.ac.uk/pub/perl/Tk-b8.tar.gz 163.1.2.4
Australia (please be patient and only try during off hours)
ftp://ftp.syd.dit.csiro.au/pub/perl5/ftp.wpi.edu 130.155.20.5
Perl 5.001m is available from one of the CPAN ftp sites
in the src/5.0/ directory as file perl5.001m.tar.gz.
Australia (please be patient and only try during off hours)
ftp://syd.dit.csiro.au/pub/perl5/local/perl5.001m+Tk-b8-Linux-ELF.tar.gz
It unpacks into /usr/local. You need to have ELF running and to
have the ELF X11 libraries (please be patient and only try during
off hours).
Binaries for the old Perl 5 & Tk-b6 are available for a number of UNIX platforms courtesy of Thomas Schlagel and Alan Stange of Brookhaven Lab at:
USA
http://pubweb.bnl.gov/~ptk/
Thomas and Alan have recently (winter 1995-1996) announced that they will
update the Tk module version number of the many binaries they distribute.
Return to table of contents
"CPAN" = Comprehensive Perl Archive Network a worldwide collection of anonymous ftp sites for Perl et al (not to be confused with CTAN which is for TeX). The files of particular interest to a perl/Tk programmer would include:
Updated: Sun Dec 8 17:12:55 EST 1996
Africa
South Africa
ftp://ftp.is.co.za/programming/perl/CPAN/ 196.4.160.12
Asia
Hong Kong
ftp://ftp.hkstar.com/pub/CPAN/ 202.82.7.4
Japan
ftp://ftp.jaist.ac.jp/pub/lang/perl/CPAN/ 150.65.7.5
ftp://ftp.lab.kdd.co.jp/lang/perl/CPAN/ 192.26.91.6
South Korea
ftp://ftp.nuri.net/pub/CPAN/ 203.255.112.6
Taiwan
ftp://dongpo.math.ncu.edu.tw/perl/CPAN/ 140.115.25.3
Australasia
Australia
ftp://coombs.anu.edu.au/pub/perl/CPAN/ 150.203.76.2
ftp://ftp.mame.mu.oz.au/pub/perl/CPAN/ 128.250.209.2
New Zealand
ftp://ftp.tekotago.ac.nz/pub/perl/CPAN/ 202.49.6.24
Europe
Austria
ftp://ftp.tuwien.ac.at/pub/languages/perl/CPAN/ 128.130.34.160
Belgium
ftp://ftp.kulnet.kuleuven.ac.be/pub/mirror/CPAN/ 134.58.127.2
Czech Republic
ftp://sunsite.mff.cuni.cz/Languages/Perl/CPAN/ 194.50.23.220
Denmark
ftp://sunsite.auc.dk/pub/languages/perl/CPAN/ 130.225.51.30
Finland
ftp://ftp.funet.fi/pub/languages/perl/CPAN/ 128.214.248.6
France
ftp://ftp.pasteur.fr/pub/computing/unix/perl/CPAN/ 157.99.64.12
Germany
ftp://ftp.leo.org/pub/comp/programming/languages/perl/CPAN/ 131.159.0.252
ftp://ftp.rz.ruhr-uni-bochum.de/pub/CPAN/ 134.147.32.42
ftp://ftp.uni-hamburg.de/pub/soft/lang/perl/CPAN/ 134.100.32.54
Greece
ftp://ftp.ntua.gr/pub/lang/perl/
Hungary
ftp://ftp.kfki.hu/pub/packages/perl/CPAN/ 148.6.0.5
Italy
ftp://cis.utovrm.it/CPAN/ 160.80.22.17
the Netherlands
ftp://ftp.cs.ruu.nl/pub/PERL/CPAN/ 131.211.80.17
ftp://ftp.EU.net/packages/cpan/ 134.222.91.7
Norway
ftp://ftp.uit.no/pub/languages/perl/cpan/ 129.242.4.34
Poland
ftp://ftp.pk.edu.pl/pub/lang/perl/CPAN/ 149.156.132.152
ftp://sunsite.icm.edu.pl/pub/CPAN/ 148.81.209.3
Portugal
ftp://ftp.ci.uminho.pt/pub/lang/perl/ 193.136.16.247
ftp://ftp.telepac.pt/pub/CPAN/ 194.65.5.98
Russia
ftp://ftp.sai.msu.su/pub/lang/perl/CPAN/ 158.250.29.1
Slovenia
ftp://ftp.arnes.si/software/perl/CPAN/ 193.2.1.72
Spain
ftp://ftp.etse.urv.es/pub/mirror/perl/ 193.144.20.6
ftp://ftp.rediris.es/mirror/CPAN/ 130.206.1.2
Sweden
ftp://ftp.sunet.se/pub/lang/perl/CPAN/ 130.238.253.4
Switzerland
ftp://sunsite.cnlab-switch.ch/mirror/CPAN/ 193.5.24.1
UK
ftp://ftp.demon.co.uk/pub/mirrors/perl/CPAN/ 158.152.1.44
ftp://sunsite.doc.ic.ac.uk/packages/CPAN/ 193.63.255.1
ftp://unix.hensa.ac.uk/mirrors/perl-CPAN/ 129.12.200.129
North America
Ontario
ftp://ftp.utilis.com/public/CPAN/ 207.34.209.49
ftp://enterprise.ic.gc.ca/pub/perl/CPAN/ 192.197.182.100
California
ftp://ftp.digital.com/pub/plan/perl/CPAN/ 204.123.2.4
ftp://ftp.cdrom.com/pub/perl/CPAN/ 165.113.58.253
Colorado
ftp://ftp.cs.colorado.edu/pub/perl/CPAN/ 128.138.243.20
Florida
ftp://ftp.cis.ufl.edu/pub/perl/CPAN/ 128.227.205.206
Illinois
ftp://uiarchive.cso.uiuc.edu/pub/lang/perl/CPAN/ 128.174.5.14
Massachusetts
ftp://ftp.iguide.com/pub/mirrors/packages/perl/CPAN/ 206.15.105.99
New York
ftp://ftp.rge.com/pub/languages/perl/ 157.225.178.12
North Carolina
ftp://ftp.duke.edu/pub/perl/ 152.3.233.7
Oklahoma
ftp://ftp.ou.edu/mirrors/CPAN/ 129.15.2.40
Oregon
ftp://ftp.orst.edu/pub/packages/CPAN/ 128.193.4.12
Texas
ftp://ftp.sedl.org/pub/mirrors/CPAN/ 198.213.9.194
ftp://ftp.metronet.com/pub/perl/ 192.245.137.6
South America
Chile
ftp://sunsite.dcc.uchile.cl/pub/Lang/perl/CPAN/ 146.83.5.204
For those equipped with multi-protocol browsers you might pay a visit to
Tom Christiansen's CPAN
multiplexer
whose relevant Tk URLs are (the second one is not active since it violates the
HTML-2.0 spec according to nsgmls):
http://perl.com/cgi-bin/cpan_mod?module=Tk
http://perl.com/cgi-bin/cpan_mod?module=Tk&readme=1
According to Stephen P. Potter some of the CPAN sites have decompression on the
fly for people who do not have programs like gunzip. For example,
at the ufl site (Florida USA) type this into your ftp session to download a
gunzipped version of Tk:
ftp> get Tk400.200.tar.gz Tk400.200.tar
If you have the appropriate CPAN and FTP modules already installed you can
retrieve a module from CPAN and carry out a complete
installation with a perl one-liner like this:
perl -MCPAN -e 'install "Tk"'
For more information on CPAN you can send e-mail to the CPAN administrators,
<cpan-adm@ftp.funet.fi>.
If you know of some Perl resources that seem not to be in the CPAN
(you did check the contents listings in indices/, didn't you?) please tell
the CPAN administrators.
If you have some modules/scripts/documentation yourself that you
would like to contribute to CPAN, please read the file authors/00upload.howto
and let the CPAN administrators know about it.
Return to table of contents
In general, building perl/Tk requires:
cc
compilers, as well as with the free GNU gcc compiler. A
make utility of some sort (make/gmake)
will be extremely helpful.
The versions of the various Perl utilities that you need on hand are roughly as follows:
Utility Version Tk version/comments perl 5.001m Tk-b8 (not 5.001n) perl 5.002 (or 002_01) Tk-b11.02 perl 5.002 (up to 003_04) Tk400.200 (not 003_06++) MakeMaker 4.18 (or higher) Tk-b8 MakeMaker 5.21 (or higher) Tk-b11+ (a patch is required to get the xsubpp with perlStep - by - step the commands to build the Tk extension to Perl are (for the dynamically linked version) roughly as follows:
Note carefully that this process leaves behind a large amount of documentation and examples in various sub-directories. You are strongly encouraged to look carefully through your build tree for docs, examples, etc. and keep those valuable files in a safe place.
A relatively easy way to determine if the perl on your system allows for dynamic linking was mentioned by Kenneth Albanowski <kjahds@kjahds.com>. If the following does not say "dl_none.xs" then you probably do have dynamically linked perl (or perhaps a very non-Unixy perl):
perl -e 'use Config; print $Config{dlsrc},"\n"'
(thanks to Paul Lussier <plussier@isd.3com.com> for the correction!).
Here is a little more detailed discussion of each the steps just given:
uninstall perl script in the Tk build directory.
Run that script before the perl Makefile.PL step if
you had a previously installed Tk extension (especially if the
version number was prior to b10, e.g. Tk-b9.01 or Tk-b8).
perl Makefile.PL X11=/usr/local/X11R5
or perhaps different directory tree specification is necessary with your X
installation:
perl Makefile.PL X11INC=/usr/local/share/X11R5/include \
X11LIB=/usr/local/arch/X11R5/lib
There are system and site dependencies in all of the above steps.
However, the largest single source of build trouble comes from not using the
latest versions of the various utilities (C compiler,
make, etc.). In particular ensure that when you say
perl Makefile.PL that the perl that gets invoked is up to
date - use
which perl (or whence perl)
and perl -v to determine this. If
necessary specify the full path name to your perl5 interpreter/compiler.
(Some people do not rm their older perl interpreters
when upgrading to a more recent version - beware.)
If you still run into trouble take a look at the INSTALL, the README and the README file for your specific system (e.g. README.AIX, README.OSF, etc.). You might also find your system mentioned in the ptk hyper-mail archive at:
http://pubweb.bnl.gov/~ptk/
or
http://www.rosat.mpe-garching.mpg.de/mailing-lists/ptk/
or
ftp://ftp.ccd.bnl.gov/pub/ptk/archives/
or the Perl 5 Porters page at one of the following URLs:
http://www.rosat.mpe-garching.mpg.de/mailing-lists/Perl5-Porters/
http://www.nicoh.com/cgi-bin/lwgate/PERL5-PORTERS/
http://www.hut.fi/~jhi/perl5-porters.html
If you wish to discuss your Tk build problems with others run and save the
output from the myConfig script in the Tk build directory
(the output may already be in the myConfig.out file from your
perl/Tk build directory), as well as the myconfig script in your
perl build directory. It is often helpful to include the output of either
(or both) of these scripts in your discussion.
Presented here are the beginnings of a list of problems associated with building perl/Tk on various platforms (for help building perl itself please refer to the Perl FAQ). This list is in no way complete nor authoritative (nor is it necessarily even up-to-date!) but simply lists problems people have reported. Keep in mind that your installation may differ (e.g. location differences such as /usr/bin/perl vs. /usr/local/bin/perl) even if its the same platform listed here:
For Tk-b8: modifying the perl.exp file may be necessary. There is a patch in Tk-b8/README.AIX. It may be necessary to make regen_headers after the patch.
Previous versions: Most people seem to prefer the dynamic linking afforded by a recent version of the gcc compiler on this system.
MachTen's pwd.h can't be included more that once or you get an error.
It looked to me like tclUnix.h was only used in tclUnixUtil.c, so I commented out the #include <pwd.h> in tclUnix.h.
For Tk-b8: make is reputedly not up to the task on this system.
Tk-b8/README.OSF recommends gmake instead.
Stephane Bortzmeyer
<bortzmeyer@pasteur.fr>
reports a successful build with Perl 5.001m, xsubpp 1.922,
MakeMaker 4.23. He points out that it was necessary for him to
upgrade the xsubpp and MakeMaker that he received with his copy of
Perl5.001m.
Matthew Black <black@csulb.edu> recently mentioned a need to apply "patchSG0000596" to get perl sockets to work. His message was copyrighted and is not included here. Send e-mail to him to find out where the get "patchSG0000596".
John Stoffel reports a successful build of static Tk-b10 on Ultrix 4.5.
http://www.rosat.mpe-garching.mpg.de/mailing-lists/Perl5-Porters/
http://www.nicoh.com/cgi-bin/lwgate/PERL5-PORTERS/
http://www.hut.fi/~jhi/perl5-porters.html
In general your non-Unix platform must be able to support perl 5 and
Xlib
(a C compiler and a make utility are tremendously useful too).
If you want to run perl/Tk on another computer and simply have the display show
up on yours then all you need on your computer is an "X server"
The long list of UNIX and non-unix
perl 5 ports, Tcl/Tk ports, and Perl/Tk ports that used to
appear in this FAQ has now moved to a separate web page at:
http://w4.lns.cornell.edu/~pvhp/ptk/ptkPORT.html
Return to table of contents
6. Where is the Documentation?
A great deal of Perl/Tk documentation gets installed as part of the Tk extension building process. Hence, a great deal of documentation is probably residing on your computer already.
More documentation is "in the works": there are several books dealing with perl/Tk in progress, an ongoing magazine column and a growing FAQ (the document you are presently reading).
The additional material you may want to look at can be split into Perl/Tk, Perl, Tcl/Tk, and X documentation categories:
In your Tk build directory there should be a doc/ sub-directory in which there are a number of .htm files (after you make install). These files were originally Tcl/Tk man pages (from the man* sub-directories), but the *.htm files have been converted to Perl syntax and rendered in HTML format. You can use the Perl/Tk web browser to view them locally with a command like:
tkweb index.html
or you may view them on the web itself by installing them in a web-server
directory tree, or by pointing a browser at:
http://w4.lns.cornell.edu/~pvhp/ptk/doc/
The nTk/pTk Mailing List Archive is a very useful source of information too, and is accesible at either
http://pubweb.bnl.gov/~ptk/
or via ftp at
ftp://ftp.ccd.bnl.gov/pub/ptk/archives/
(both in the USA). You may search the contents of another ptk mailing
list hypertext archive thanks to a cgi-bin script written by Achim Bohnet in
Germany at:
http://www.rosat.mpe-garching.mpg.de/mailing-lists/ptk/
You must subscribe to the mailing list to receive e-mail from the list.
To subscribe to the mailing list you can send mail to
majordomo@lists.stanford.edu
(i.e.
<majordomo@lists.stanford.edu>)
with the following command in the body of your e-mail message:
subscribe ptk joe.user@somewhere (Joe D. User)
To send a message to all recipients of the mailing list send e-mail to <ptk@lists.stanford.edu>.
To remove yourself from the mailing list send e-mail to majordomo@lists.stanford.edu (i.e. <majordomo@lists.stanford.edu>) with the following command in the body of your e-mail message:
unsubscribe ptk joe.user@somewhere (Joe D. User)
Where instead of "joe.user@somewhere" you might very well like to substitute
another string of characters.
(Please note: one does not send unsubscribe messages to the ptk list. One does send "unsubscribe ptk" messages to a special e-mail list administration program. In the case of the ptk list you send to majordomo@lists.stanford.edu. You must of course do this from the account and computer from which you initially subscribed. In particular if you are viewing the hypertext version of this FAQ you may not be able to unsubscribe from majordomo@lists.stanford.edu by following the mailto: hyperlinks - if your web-browser account/computer is different from your subscribe to e-mail-lists account/computer (the details of this might depend on which browser you use though). Thank you for your cooperation on this.)
In order to determine where on your system the perl5/ directory is located type the following one-line perl command (at your shell prompt - this is not a line from a perl script):
perl -e 'print join("\n",@INC,"");'
If that command does not turn up a perl5/ directory then
make sure that you are running perl 5 with the following: perl -v
(this too can simply be entered at the shell prompt).
The raw pod files that come with the Tk kit are examples of the perl
"plain old documentation" format and are just about human
readable as they are (e.g. you may more,
cat, or less them; or send them to a printer).
Many (not all) of the perl/Tk pod documents get are
converted to *roff format and are installed in you perl man page area as part
of the perl/Tk build process.
If you have a recent version of perl/Tk try something like man 3 Tk::Tk. If this does not work check your manual page path with
perl -MConfig -e 'print $Config{man1dir},"\n",$Config{man3dir},"\n"'
or
perl -e 'use Config; print $Config{man1dir},"\n",$Config{man3dir},"\n"'
And if you still cannot find the manual pages check with your system
administrator for the proper MANPATH and/or Tk installation version.
"Raw" .pod (such as UserGuide.pod) can be viewed with the tkpod hypertext pod viewer. Raw .pod may also be run through any one or more of a large numbers of re-formatting perl filters. Such programs include pod2man, pod2text, pod2html, pod2latex, etc. (these get installed when you install perl). Other translators pod2texinfo, pod2fm, pod2pdf, etc., also exist. Check a CPAN site for these scripts if you do not already have them.
A command line like the following (but subject to local variations) should work for you:
tkpod site_perl/Tk/UserGuide.pod
or if you like Unix manual page style:
pod2man perl5/Tk/UserGuide.pod | nroff -man | more
(note that I am showing examples with almost full file path names - the
alternative would be to cd into the appropriate directory then type:
pod2man UserGuide.pod | nroff -man | more
There should even be a perl script to run that above command for you. It is
executed as:
perldoc UserGuide
Note that if there is pod like documentation in a perl module you
may also execute tkpod (or perldoc) on it as in:
tkpod ColorEditor.pm
(please note that unfortunately, not all .pm mod files
have pod
embedded.)
If you have misplaced your tkpod program but still want that GUI look and feel (like xman) make the appropriate changes to the following script:
#!/usr/bin/perl
use Tk;
use Tk::Pod;
my $m = new MainWindow;
$m -> Pod(-file => 'ColorEditor.pm');
# or use command line path/filename:
# $m -> Pod(-file => $ARGV[0]);
MainLoop;
World Wide Web - perl/Tk man pages
http://pubweb.bnl.gov/~ptk/doc/index.html
http://w4.lns.cornell.edu/~pvhp/ptk/doc/
http://w4.lns.cornell.edu/~pvhp/ptk/pod/
Newsgroups
comp.lang.perl.tk
comp.lang.perl.misc
comp.lang.perl.anounce
comp.lang.tcl
comp.lang.tcl.announce
comp.answers
news.answers
Perl/Tk FAQ-Archives (ftp sites) [Note: FAQ may be many separate files]
(see also CPAN sites)
ftp://rtfm.mit.edu/pub/usenet/comp.lang.perl.tk
ftp://rtfm.mit.edu/pub/usenet-by-hierarchy/comp/lang/perl/tk
ftp://ftp.uu.net/usenet/news.answers/perl-faq/ptk-faq
ftp://ftp.ccd.bnl.gov/pub/ptk/ 130.199.54.188
ftp://ftp.ccd.bnl.gov/pub/ptk/ptkFAQ.txt 130.199.54.188
ftp://ftp.wpi.edu/perl5/pTk-FAQ 130.215.24.209
ftp://perl.com/pub/perl/doc/ptkFAQ.gz 199.45.129.30
ftp://perl.com/pub/perl/doc/ptkFAQ.ps.gz 199.45.129.30
WWW-FAQ for perl/Tk
http://w4.lns.cornell.edu/~pvhp/ptk/ptkTOC.html
http://w4.lns.cornell.edu/~pvhp/ptk/ptkFAQ.html
World Wide Web - perl/Tk info sites
http://pubweb.bnl.gov/~ptk/
http://fxfx.com/kgr/compound/ (Perl Tk Compound Widget Page)
http://w4.lns.cornell.edu/~pvhp/ptk/ptkIMG.html (FAQ image supplement)
http://w4.lns.cornell.edu/~pvhp/ptk/etc/
http://w4.lns.cornell.edu/~pvhp/ptk/misc/
http://w4.lns.cornell.edu/~pvhp/ptk/PNMTYAK/
http://www.mirai.com/wks/
The Mailing list
majordomo@lists.stanford.edu
ptk@lists.stanford.edu
http://www.perl.com/perl/faq/Q2.2.html
or at:
http://www.perl.com/perl/info/books.html
For Perl 5 there is (as of September 1996) a
"New Camel"
by Larry Wall,
Tom Christiansen,
and Randal L. Schwartz,
with Stephen Potter.
Programming Perl 2nd EditionThe two early Perl books by Schwartz and Wall are very helpful (even if they do pertain to perl 4 and not 5. Beware that perl/Tk makes extensive use of perl 5 object-oriented features.):
Larry Wall, Tom Christiansen, & Randal L. Schwartz with Stephen Potter
(c) 1996 O'Reilly & Associates Inc.
ISBN 1-56592-149-6; Order number: 1496 (English)
Learning Perl (The Llama)
Randal L. Schwartz
Copyright (c) 1993 O'Reilly & Associates Inc.
ISBN 1-56592-042-2 (English)
ISBN 2-84177-005-2 (French)
ISBN 3-930673-08-8 (German)
ISBN 4-89502-678-1 (Japanese)
Programming Perl (The Camel)There is also some Perl5 (book material) information at:
Larry Wall and Randal L. Schwartz
Copyright (c) 1991 O'Reilly & Associates Inc.
ISBN 0-937175-64-1 (English)
ISBN 3-446-17257-2 (German) (Programmieren in Perl, translator: Hanser Verlag)
ISBN 4-89052-384-7 (Japanese)
http://www.metronet.com/1h/perlinfo/perl5/
Jon Orwant (the organizer of the
comp.lang.perl.tk newgroup)
has a book on Perl 5 that has a chapter that discusses the Tk extension.
(Please note that it is mostly
about Perl 5, there is a some discussion of four simple Perl/Tk programs, but
it is not a book wholly devoted to Perl/Tk.) It is nevertheless a good
introduction to object-oriented Perl 5 programming. The relevant info:
Perl 5 Interactive CourseThe Perl 5 Quick Reference Guide (may require LaTeX for installation) can be obtained from any CPAN ftp site. Detailed location information is also available at the author's website:
Jon Orwant
(c) 1996 The Waite Group Press
A Division of SAMS Publishing, Corte Madera, CA USA
ISBN: 1-57169-064-6
http://www.xs4all.nl/~jvromans/perlref.html
The quick reference guide has also been turned into a small Nutshell handbook:
Perl 5 Desktop Reference
Johan Vromans
Copyright (c) February 1996 O'Reilly & Associates Inc.
ISBN: 1-56592-187-9; Order number: 1879
Eric F. Johnson has a book that discusses many perl5 for Unix vs. perl5 for Windows NT issues. He includes a final chapter with extensive discussion of the Tk extension and the ->Text() widget in particular. The information on that book is:
Cross-Platform Perl
(c) 1996 Eric F. Johnson
MIS:Press/M&T Books
ISBN: 1-55851-483-X
Kamran Husain and Robert F. Breedlove have written a perl 5 book that includes a chapter on Tk with some discussion of Menu()s. That book is:
Perl 5 Unleashed
Kamran Husain and Robert F. Breedlove
(c) 1996 Sams Publishing, Indianapolis, IN
ISBN: 0-672-30891-6
There is also a "Perl 5 How-To" book available that contains a great deal of erroneous information about perl/Tk. Among other things that book wrongly mentions that it is necessary to have a complete Tcl/Tk library installed on one's system to compile the Tk extension to perl. (They are incorrect - it is only necessary to have the appropriate perl version, libc and Xlib, the Tk extension is otherwise "self-contained").
There will be a new book on web clients out soon. It is slated to have at least a chapter on perl/Tk:
Web Client Programming with Perl
Clinton Wong
1st Edition March 1997 (est.)
O'Reilly & Associates Inc.
ISBN: 1-56592-214-X; Order number: 214X
Additional book information may be found at Tom Christiansen's perl & cgi books page, or at his Perl-related Book Reviews page.
The multi-part perl 5 manual pages are available (assuming they have been installed in your MANPATH, type man perl, man perlmod etc.).
The perl man pages are also available on the web at a number of locations including:
World Wide Web - perl 5.001m man pages
(the 5.002 pages [listed below] are more useful though)
Australia
http://www.cs.mu.oz.au/~slf/perl5/perl.html
http://bwyan.anu.edu.au/perl.html
Austria
http://www.cosy.sbg.ac.at/comp/lang/perl/perl5man/perl.html
Brazil
http://www.lsi.usp.br/perl5/
Canada
http://csclub.uwaterloo.ca/perldoc/perl.html
http://stoner.eps.mcgill.ca/perl/perl.html
Germany
http://www.t-informatik.ba-stuttgart.de/Perl5/perl.html
http://www.dfv.rwth-aachen.de/doc/perl/perl.html
Norway
http://www.pvv.unit.no/sw/perl5/index.html
Slovak Republic
http://www.savba.sk/autori/perl/perl.html
Slovenia
http://www.ijs.si/perl/
Taiwan
http://www.ccu.edu.tw/perl5/index.html
UK
http://www.cs.man.ac.uk/~richardd/perl5/perl.html
http://ppewww.ph.gla.ac.uk/perl/perl.html
USA
http://rhine.ece.utexas.edu/~kschu/perlman.html
http://duggy.extern.ucsd.edu/perl/perl.html
http://tbone.biol.scarolina.edu/~dean/perl/perl.html
http://www.mit.edu:8001/perl/perl.html
http://icg.stwing.upenn.edu/perl5/perl.html
http://w4.lns.cornell.edu/public/compdoc/info/perl/perl.html
World Wide Web - perl 5.002 man pages
(also very useful for previous versions of perl 5)
Canada
http://dymaxion.ns.ca/www/dv/perl_manual/index.html
http://wepil.uwaterloo.ca/~mathers/perl/perl.html
Czech Republic
http://infog.eunet.cz/~muaddib/perl5/index.html
Finland
http://www.hut.fi/~jhi/perl5/index.html
Germany
http://nat-www.uia.ac.be/perl/perl.html
Netherlands
http://www.cs.ruu.nl/pub/mirrors/CPAN/doc/manual/html/frame_index_long.html
USA
http://pubweb.bnl.gov/~ptk/pod/perl.html
http://www.metronet.com/0/perlinfo/perl5/manual/perl.html
http://www.lafayette.edu/doughera/doughera/perl/manual/perl.html
http://www.va.pubnix.com/staff/stripes/perlinfo/
http://www.perl.com/perl/manual/
http://cesr39.lns.cornell.edu/public/perl/manual/
http://www.ilap.com/perl/
http://saturn.lbcc.cc.or.us/www_root/docs/perl5/PERL.HTML
In addition to the CPAN ftp source sites, a miscellany of internet perl resources includes:
Newsgroups
comp.lang.perl.misc
comp.lang.perl.announce
comp.lang.perl.modules
comp.infosystems.www.authoring.cgi
comp.answers
news.answers
Perl FAQ-Archives (ftp sites) [Note: FAQ may be many separate files]
(see also the CPAN sites)
North America
ftp://rtfm.mit.edu/pub/usenet/news.answers/perl-faq/
ftp://ftp.uu.net/usenet/news.answers/perl-faq 192.48.96.9
ftp://ftp.khoros.unm.edu/pub/perl/faq.gz 198.59.155.28
Europe
ftp://ftp.cs.ruu.nl/pub/NEWS.ANSWERS/perl-faq/ 131.211.80.17
ftp://src.doc.ic.ac.uk/packages/perl/FAQ 146.169.2.10
Gopher Perl FAQ
gopher://gopher.metronet.com/11/perlinfo/faq
WWW-FAQ for Perl
http://www.perl.com/perl/faq/
http://www.smartpages.com/bngfaqs/comp/lang/perl/top.html
http://www.smartpages.com/bngfaqs/comp/lang/perl/misc/top.html
http://www.smartpages.com/bngfaqs/comp/lang/perl/announce/top.html
http://www.cis.ohio-state.edu/hypertext/faq/usenet/perl-faq/top.html
Perl for Win32 FAQ (discusses Win95)
http://www.perl.hip.com/PerlFaq.htm
Perl info sites
Gopher (gopher:70) perl info sites
USA
gopher://gopher.metronet.com/11h/perlinfo
World Wide Web (http:80) perl info sites
USA
http://www.yahoo.com/Computers_and_Internet/Languages/Perl/index.html
http://www.perl.com/
http://www.khoros.unm.edu/staff/neilb/perl/home.html
http://www.khoros.unm.edu:80/staff/neilb/perl/metaFAQ/
http://www.metronet.com/perlinfo/
http://www.metronet.com/perlinfo/perl5.html (Perl 5)
http://www.eecs.nwu.edu/perl/perl.html
http://cesr39.lns.cornell.edu/public/perl/
http://www.virtualschool.edu/mon/Perl.html
http://www.hermetica.com/technologia/unexec/
http://www.oac.uci.edu/indiv/ehood/perlWWW/
http://web.sau.edu/~mkruse/www/scripts/
http://orwant.www.media.mit.edu/the_perl_journal/
http://www.perl.com/Architext/AT-allperl.html
http://www.mispress.com/introcgi/
http://www.walrus.com/~smithj/webcan/
http://web.syr.edu/~chsiao05/cps600_project.html
http://www.iftech.com/classes/webdev/webdev_perl.htm
http://www.cc.iastate.edu/perlmenu/
http://www.ora.com/www/item/cgi_prog.html
UK
http://pubweb.nexor.co.uk/public/perl/perl.html
http://www.bio.cam.ac.uk/web/form.html
Web references to Perl mailing lists
http://www.perl.com/perl/info/mailing-lists.html
http://www.nicoh.com/cgi-bin/lwgate/PERL5-PORTERS/
http://www.hut.fi/~jhi/perl5-porters.html
http://www.rosat.mpe-garching.mpg.de/mailing-lists/
Tcl and the Tk Toolkit
John K. Ousterhout
Copyright (c) 1994 Addison-Wesley Publishing Company
ISBN 0-201-63337-X (alk. paper)
LOC QA76.73.T44097 1994; 005.13'3--dc20
Practical Programming in Tcl and Tk
Brent Welch
Copyright (c) 1995 Prentice Hall
ISBN 0-13-182007-9
Within the tclsh or wish shells your manpath
includes the tcl/tk man pages (which may not be in your login
MANPATH). Thus from the % prompt within either shell type commands
like:
% man -k Tk
The Tcl/Tk Reference Guide is also a source of useful information. Although it's Tcl specific most perl/Tk commands can be, more or less, easily derived from it. [As of Tk-b9.01 the names of some functions and some configuration options have changed slightly from their Tcl/Tk counterparts. With Tk-b9.01 (and higher) a great many functions start with an upper case letter and continue with all lower case letters (e.g. there is a perl/Tk Entry widget but no entry widget), and many configuration options are all lower case (e.g. there is a perl/Tk highlightthickness option but no highlightThickness option).] You may fetch the Tcl/Tk Reference Guide (may require LaTeX for installation) from:
ftp://ftp.slac.stanford.edu/software/TkMail/tkref-4.0.1.tar.gz 134.79.18.30 ftp://ftp.aud.alcatel.com/tcl/docs/tkref-4.0.1.tar.gz 198.64.191.10There are a number of other Tcl/Tk resources on the internet including:
Newsgroups
comp.lang.tcl
comp.lang.tcl.announce
comp.infosystems.www.authoring.cgi
comp.answers
news.answers
FAQ-Archive (ftp) [Note: Tcl FAQ may be many files, Tk FAQ is one file]
ftp://ftp.aud.alcatel.com/tcl/docs/ 198.64.191.10
ftp://rtfm.mit.edu/pub/usenet/news.answers/tcl-faq
ftp://rtfm.mit.edu/pub/usenet/news.answers/tcl-faq/tk
WWW-FAQ for Tcl/Tk
http://www.teraform.com/%7Elvirden/tcl-faq/
http://www.smartpages.com/faqs/tcl-faq/top.html
http://www.smartpages.com/bngfaqs/comp/lang/tcl/top.html
http://www.cis.ohio-state.edu/hypertext/faq/usenet/tcl-faq/top.html
http://www.cis.ohio-state.edu/hypertext/faq/bngusenet/comp/lang/tcl/top.html
http://www.sco.com/Technology/tcl/Tcl.html
World Wide Web - Tcl/Tk info sites
Canada
http://web.cs.ualberta.ca/~wade/Auto/Tcl.html
UK
http://http2.brunel.ac.uk:8080/~csstddm/TCL2/TCL2.html
http://www.cis.rl.ac.uk/proj/TclTk/
USA
http://www.yahoo.com/Computers_and_Internet/Languages/Tcl_Tk/index.html
http://www.sunlabs.com/research/tcl/docs.html
http://www.sunlabs.com/research/tcl/4.0.html
http://www.sco.com/Technology/tcl/Tcl.html
http://www.neosoft.com/tcl/
http://www.elf.org/tcltk-man-html/contents.html
Tcl/Tk - miscellaneous extensions
ftp://ftp.cme.nist.gov/pub/expect/
http://www.cs.hut.fi/~kjk/porttk.html
http://www.cis.upenn.edu/~ioi/tix/tix.html
http://www.ece.cmu.edu/afs/ece/usr/svoboda/www/th/homepage.html
http://www.tcltk.com/ [incr Tcl]
http://www.neosoft.com/tcl/TclX.html
http://www.eolas.com/eolas/webrouse/tcl.htm [WebWish]
http://www.se.cuhk.hk/~hkng2/big5tk/big5tk.html
http://www.cs.uoregon.edu/~jhobbs/work/ [BLT etc.]
There are a number of X resources on the internet including:
Newsgroups
comp.windows.x
comp.windows.x.announce
comp.windows.x.apps
X FAQs:
ftp://ftp.x.org/contrib/faqs/
X FAQ on the World Wide Web:
http://www.x.org/ftp/contrib/faqs/x-faq-multipart/x-faq-1
http://www.x.org/ftp/contrib/faqs/x-faq-multipart/x-faq-2
http://www.x.org/ftp/contrib/faqs/x-faq-multipart/x-faq-3
http://www.x.org/ftp/contrib/faqs/x-faq-multipart/x-faq-4
http://www.x.org/ftp/contrib/faqs/x-faq-multipart/x-faq-5
http://www.x.org/ftp/contrib/faqs/x-faq-multipart/x-faq-6
http://www.x.org/ftp/contrib/faqs/x-faq-multipart/x-faq-7
X Window System book info on the Web:
http://www.x.org/ftp/contrib/docs/Xbibliography.OReilly
http://www.ora.com/catalog/v1/
http://www.ora.com/catalog/v2/
http://www.ora.com/catalog/v3/
http://www.ora.com/catalog/v4/
http://www.ora.com/catalog/v5/
http://www.ora.com/catalog/v6a/
http://www.ora.com/catalog/v6b/
http://www.ora.com/catalog/v6c/
http://www.ora.com/catalog/r6/noframes.html
http://www.ora.com/oracom/prog/flanart.html
World Wide Web - X Window System info sites
http://www.x.org/
http://www.x.org/consortium/GettingX.html
http://www.x.org/consortium/x_info.html
http://www.x.org/consortium/R6.1doc/man/X11/
http://www.wolfram.com/~cwikla/widget/
http://www.zeta.org.au/~rosko/pigui.htm
http://www.rahul.net/kenton/xsites.html
http://www.unx.com/DD/txaCurrent.shtml
Return to table of contents
7. How do I write scripts in perl/Tk?
Start your script as you would any perl script (e.g.
#!/usr/bin/perl, #!/usr/local/bin/perl,
#!/opt/bin/perl, [built static? then
#!/usr/bin/tkperl], whatever, see the
perlrun(1) man page for more information).
Throwing the -w warning switch is recommended.
The use of the statement use strict; is recommended.
Use of the statement use Tk; is required.
A simple "Hello World!" widget script could be written as follows:
#!/usr/local/bin/perl -w
use strict;
use Tk;
my $main = new MainWindow;
$main->Label(-text => 'Hello World!'
)->pack;
$main->Button(-text => 'Quit',
-command => sub{exit}
)->pack;
MainLoop;
The MainLoop; statement is the main widget event handler
loop and is usually found in perl/Tk scripts (usually near the end of
the main procedure after the widgets have been declared and
packed). MainLoop; is actually a function call and you
may see it written as MainLoop();, &Tk::MainLoop;,
&Tk::MainLoop();, etc.
Note the use of the -> infix dereference operator. Most things in calls to perl/Tk routines are passed by reference.
Note also the use of the => operator which is simply a synonym for the comma operator (well it is a bit more than that :-). In other words, the arguments that get passed to Label and Button in the above example are good old perl associative arrays (perl 5 people prefer to call them "hashes" however). Indeed, we might have written the above as:
#!/usr/local/bin/perl -w
use strict;
use Tk;
my $main = new MainWindow;
$main->Label(-text , 'Hello World!'
)->pack;
$main->Button(-text , 'Quit',
-command , sub{exit}
)->pack;
MainLoop;
Or even as:
#!/usr/local/bin/perl -w
use strict;
use Tk;
my $main = new MainWindow;
my %hello = ('-text','Hello World!');
my %quit_com = ('-text' => 'Quit', '-command' => sub{exit});
$main->Label(%hello)->pack;
$main->Button(%quit_com)->pack;
MainLoop;
Note however, that the use of the => in the first
method of writing this script makes it look more
"Tcl-ish" :-).
Lastly, we note the extensive use of the my function in most perl/Tk programs. my is roughly equivalent to local in Perl 4 - but is purported to be "faster and safer" as well as much more strictly local in scope. See perlfunc(1) manpage for more information on my.
Other examples of code may be found in the perl5/Tk/demos/ directory and in perl5/Tk/demos/widget_lib/.
(A variant on this scipt called hello is available in the
file perl5/Tk/demos/hello in your own pTk distribution.
Also, Source code for this and other examples
from UserGuide.pod may be
found at http://w4.lns.cornell.edu/~pvhp/ptk/pod/.
To load code from the web save as a local filename,
edit the first line to point to your perl interpreter, then:
chmod u+x filename, then execute:
filename.)
Return to table of contents
8. What widget types are available under perl/Tk?
The following Tk widget primitives are available under perl/Tk:
A good introduction to the primitives and how they may be used in conjunction with each other may be found in the widget demo script. Note that all the widget demos have a "Show Code" button. To help figure out what is happening in the script you may, when the window appears, edit the text and instrument the code with print statements and then simply press "Rerun Demo". Another place to see examples of the primitives (on the web) is at the image supplement to this FAQ at the following URL:
http://w4.lns.cornell.edu/~pvhp/ptk/ptkIMG.html
Return to table of contents
9. How do I get widget X to do Y ?
There are a number of tasks that can be accomplished with perl/Tk widgets, configurations, and bindings (a few that can't and a few that require specific tricks). Beginners are encouraged to work through the examples in UserGuide.pod. Some examples from UserGuide.pod are addressed in this document among those that follow.
Basically a widget can be "created" by simply calling the sub of the same name:
my $main = new MainWindow;
will set aside the necessary system memory etc. for a new
MainWindow widget (it does not appear until after the
MainLoop; call). The object "created" is then callable
via the variable $main. So, for example, if you wanted a
Button in your MainWindow, then this:
$main->Button();
would be a very basic example of a widget command. If you wanted to later call
this button widget you would need a "widget tag or ID" to "get a
handle on it". Instead of the above call try something like:
my $button = $main->Button();
The variable $button is how you refer to the Button widget in
subsequent calls, such as when we call the pack routine:
$button -> pack;
A complete script that incorporates these ideas to make a very plain button
would look like:
#!/usr/bin/perl -w
use Tk;
use strict;
my $main = new MainWindow;
my $button = $main -> Button();
$button -> pack;
MainLoop;
But who wants such a plain looking button? You can provide a number of
different widget configurations via calls to the configure routine
as in:
#!/usr/bin/perl -w
use Tk;
use strict;
my $main = new MainWindow;
my $button = $main->Button();
$button -> configure(-text => 'Press me!');
$button -> pack;
MainLoop;
The Perl motto is "there is more than one way to do it." - perl/Tk
remains quite true to this motto as well. Note that the above script could
have been written quite succinctly without the use of either the
$main or $button variables as:
#!/usr/bin/perl -w
use Tk;
use strict;
new MainWindow -> Button(-text => 'Press me!') -> pack;
MainLoop;
But if you want your widgets to actually do things then you must set
up callback procedures as discussed later...
As of Tk-b9(.01) the - sign in front of some options (like -text in the above example) will be optional (hence ->Button(text => 'Press me!')) would work. As of the version of Perl/Tk that supercedes Tk-b11.02 the - sign may again be required. In any case it is best to code with it especially if it is necessary. Another commonly overlooked problem is that elements in a hash are supposed to be strings hence a configuration option like -length +> 5, really ought to be specified as either '-length' +> 5, or "-length" +> 5, etc.
Return to table of contents
9.1. How do I get a Button to call a Perl subroutine?
You may specify the -command option in the call to create & pack the button as in:
$main->Button(-text => 'Print',
-command => sub{do_print($filename, $font)}
)->pack;
Where sub do_print { } is a subroutine that handles two arguments
and is declared elsewhere in the script. A full script example of the use of
the above code is presented in the second example(s)
in UserGuide.pod
(Full source code for this and other examples from UserGuide.pod may be found at http://w4.lns.cornell.edu/~pvhp/ptk/pod/. To load code from the web save as a local file say ex1.pl, edit the first line to point to your perl interpreter, then change permission: %chmod u+x ex1.pl, then execute the script: %ex1.pl.)
The above method is called the "anonymous subroutine (closure)" method. As discussed in Callback.pod one might have re-written that statement to use the "reference to a sub" method thusly:
$main->Button(-text => 'Print',
-command => [ \&do_print , $filename, $font ]
)->pack;
Note the backslash in front of \&do_print. This causes perl to
generate a reference to sub do_print rather than call it.
(thanks Jim Stern :-)
Return to table of contents
9.2. How do I get a Button to actively change under my mouse pointer?
You should specify both an '-image' and an '-activeimage' configuration option either when calling the ->Button() method or in a later separate call to the ->configure() method.
Here is an example excerpted from the basic_demo script that comes with the Tk kit:
#!/usr/local/bin/perl -w
use Tk;
$main = MainWindow->new;
$QPBFile = "demos/images/QuitPB.xpm";
$QPBaFile = "demos/images/QuitPBa.xpm";
$QuitPB = $main->Pixmap('-file' => Tk->findINC("$QPBFile"));
$QuitPBa = $main->Pixmap('-file' => Tk->findINC("$QPBaFile"));
my $but = $main->Button('-image' => $QuitPB,
'-activeimage' => $QuitPBa,
'-command' => sub { $main->destroy }
) -> pack;
MainLoop;
__END__
Return to table of contents
9.3. How do I arrange the layout of my widgets?
To control the layout and appearance of widgets in a window one makes use of a geometry manager, as well as -padding, -fill, -expand, and -anchor options of individual widgets.
A geometry manager is any Tk procedure for controlling the arrangement of widgets in your application window. The predominant geometry manager used in both Tcl/Tk and perl/Tk is pack also known informally as the "packer" (other geometry managers are the "placer" and the canvas widget itself but are much less popular. There is also Nick Ing-Simmon's Table widget [discussed in a later question] and BLT_Table [which made it's way into perl/Tk thanks to Guy Decoux - but is also discussed in a later question]. So far tixForm is for Tcl/Tk only, but a perl/Tk version of Tix is in the works. You can invoke pack at the time of widget creation via calls like:
$widget->pack;
where widget can be any of the perl/Tk widget primitives. Widget option
lists are usually passed as an associative array (hash) in parentheses thusly:
$widget(-option0 => value0,-option1 => value1)->pack;
pack is often used in conjunction with the frame
container widget to arrange your widgets much like a hiearchically arranged
set of window panes (ultimately in a rectangular "tiling" fashion of
sorts). An example of this would be:
my $top2 = $main->Toplevel;
my $frame = $top2->Frame;
$frame->pack;
$frame->Label(-text => 'Left2')->pack(-side => 'left');
$frame->Label(-text => 'Right2')->pack(-side => 'right');
$top2->Label(-text => 'Bottom2')->pack(-side => 'bottom');
MainLoop;
Note that pack itself is given parameters in this example. The
default behavior for pack is equivalent to specifying
-side => 'top' which can be overridden as in the above example.
(Full source code for this and other examples from UserGuide.pod may be found at http://w4.lns.cornell.edu/~pvhp/ptk/pod/. To load code from the web save as a local file say ex2.pl, edit the first line to point to your perl interpreter, change permission using: chmod u+x ex2.pl, then type the name of your script: ex2.pl.)
One of the more helpful options to pass to pack when trying to get a given widget layout "just right" is through padding: either -padx or -pady. The details of the use of pad depend on which specific widget you are trying to pack. In fact you can often add the -pad in the call to create the widget rather than in the call to pack.
There is also the -anchor configuration option for widgets. A good introduction to the 9 possible -anchor (and -overanchor) values is given by the popup demo in your perl/Tk build directory.
When setting a widget within a frame next to another widget one may wish to make use of the -fill => 'style' (where style = none | x | y | both) options of either pack or the widget itself. A typical situation where this is used is in setting up the Scrollbar next to a Canvas or Text widget.
Another aspect to consider when laying out your widgets is their behavior under resize operations (grabbing a part of the window frame and making it bigger or smaller - details depend on your window manager). This may be controlled by the -expand option of either pack or the widget itself.
Return to table of contents
9.4. How do I get a Popup to popup?
For things like a simple "are you sure?" dialog box you might want to take a look at Dialog.pm which is discussed in a later question within this FAQ [15.1].
If you don't wish to require Tk::Dialog, you need something more complicated, or you simply want to create your own independent window with widgets; you must first setup a Toplevel in perl/Tk. The fourth example in UserGuide.pod gives a simple example of how to call Toplevel. Quoting from that script:
my $main = new MainWindow;
fill_window($main, 'Main');
my $top1 = $main->Toplevel;
Where sub fill_window is declared after the call to
MainLoop;. When running that script take
careful note of which window pops up first, which window has grabbed
the active attention of your input device(s), and which widget within the
active window has the keyboard/mouse focus when all three windows are
open.
The use of Toplevels brings up the issue of grab - or which independent window is presently "active" and which are activatable. To make a Toplevel window active call grab thusly:
$Top_widget->grab(grab_option);
where $Top_widget identifies the desired Toplevel
(it would be either $top1 or $top2 in the
sample script referred to above).
grab_option could be -global - but this is
discouraged as a sign of "desparate programming style". To give a
Toplevel "local grab" you may simply say:
$Top_widget->grab;
That is, without an argument.
The use of Toplevels may also bring up the issue of focus - or which window - even which widget within a window - is presently "hot". You may call focus on an entire Toplevel:
$Top_widget->focus;
However, focus is most often used with individual widgets rather
than a whole Toplevel.
To de-iconify a widget there is in fact a Popup function that may be called thusly:
$Top_widget->Popup();
Return to table of contents
9.5. How do I bind keyboard keys?
There are many default key bindings built in to the widgets of perl/Tk. Making proper use of them often involves setting up the right callback. (You may wish to consult the examples in BindTable.pod for help with this subject.)
The basic idea is:
$widget -> bind('<keyname>' => action);
Where $widget is the tag or ID of the widget for which the
bindings are to hold (note for global bindings you have to bind to
<All>, for semi-global bindings you need to bind
to all the relevant widgets in your application), '<keyname>'
can be things like:
<Key> or <KeyPress> or <Any-KeyPress>
<KeyRelease>
<Button> or <ButtonPress>
<ButtonRelease>
<Button-1> or <B1>
<Double-1>
<Enter>
<Leave>
<Motion>
To figure out what names perl/Tk uses for such <bindings> use the
"binder-finder" on a widget's .pm file. For example, you could find
bindings hidden inside of Button.pm by typing this at your
shell prompt:
perl -ne 'print if s/.*(<[^>]*>).*/$1/g;' Button.pm
while in the directory where Button.pm is located (and if you are
not there then simply specify the /path/to/Button.pm).
Note that due to inheritance (e.g.the type of script bindings that are
being discussed here) what the binder-finder turns up may not be the last word
on a given widget's behaviour. This may be especially true for a widget inside
of a compound/composite widget.
Note also that the binder-finder will turn up things like <FILEHANDLES>
as well as honest <Bindings>. Discrimination in its use is called for
(and while your at it you could have just as easily used an editor and actually
examined the code directly now couldn't you?).
To get an idea of what the code is for a key that you are interested in try running the xlib_demo that comes in your perl/Tk build directory. Hold your mouse pointer over the window that appears and simply type the key that you are interested in. The code should appear in the window. If you do not have perl/Tk up and running yet try "xmodmap -pk" or look directly at the /usr/include/X11/keysymdef.h file where keysym names are given with an XK_ pre-pended. Do not try things like the Tcl/Tk %k symbols in perl scripts. %Ks will be mis-interpreted as non-existant perl hashes. Instead look at the Xevent function.
Ali Corbin <corbin@adsw.fteil.ca.boeing.com> recently posted a great little script for determining keyboard key bindings on a MainWindow:
#!/usr/local/bin/perl -w
use Tk;
$top = MainWindow->new();
$frame = $top->Frame( -height => '6c', -width => '6c',
-background => 'black', -cursor => 'gobbler' );
$frame->pack;
$top->bind( '<Any-KeyPress>' => sub
{
my($c) = @_;
my $e = $c->XEvent;
my( $x, $y, $W, $K, $A ) = ( $e->x, $e->y, $e->K, $e->W, $e->A );
print "A key was pressed:\n";
print " x = $x\n";
print " y = $y\n";
print " W = $K\n";
print " K = $W\n";
print " A = $A\n";
} );
MainLoop();
To bind the action of one widget to that of another try taking a
look at the .pm file for the widget of interest - is there a binding
function already defined? If so you may use it. An example would be the use of
"Up" & "Down" Buttons for a
Listbox: one could bind the Buttons to call
Tk::Listbox::UpDown, however, Guy Decoux describes a much more
clever way to use the <Up> and <Down> already
defined in Listbox.pm (this does not work with Tk-b9.01):
#!/usr/local/bin/perl
use Tk;
$top = MainWindow->new;
$lb = $top->Listbox(-height => 10);
for($i=0; $i < 120; $i++) {
$lb->insert('end', $i);
}
$f = $top->Frame;
$up = $f->Button(
-text => "Up",
-command => [ $lb->bind(ref $lb, '<Up>'), $lb]
);
$down = $f->Button(
-text => "Down",
-command =>sub {&{$lb->bind(ref $lb, '<Down>')}($lb)}
);
$up->pack(-side => 'left');
$down->pack;
$f->pack;
$lb->pack;
MainLoop;
Return to table of contents
On Fri, 15 Sep 95 10:30:56 BST Nick Ing-Simmons <Nick.Ing-Simmons@tiuk.ti.com> writes:
Re: Multiple binds to a single widget? ************************************** On Thu, 14 Sep 1995 14:57:54 -0400 Alain St <astdenis@cmc.doe.CA> writes: !In the tcl/tk doc I have, they say that prepending the script !with '+' appends the new binding to the current one. ! !How do I do that in perlTk? !
You cannot do that that way (yet?) - one issue is what it would mean to prepend '+' to a perl/Tk callback :
$widget->bind('<A>','+',[\&subname,$arg]);
# did not look right to me
Other issue is that I would need to manage a list-of-callbacks in glue code.Bind your new command to a new tag:
$widget->bind('Extra',....);
And add Extra to the widgets bindtags:
$widget->bindtags([ref($widget),$widget,'Extra',
$widget->toplevel,'all']);
Return to table of contents
9.7. How do I bind the action of a slider (sic) to ... ?
Technically speaking they are called Scrollbars (not sliders) and one
must configure the action of the desired widget to call the
Scrollbars (i.e. bind is not involved here)
A common task using Scrollbars is to configure things like Canvas, Listbox, or a Text widgets to be updated (change appearance) when the slider of the acompanying Scrollbar is moved by the user.
As an example consider the code that sets up a twelve element Listbox and an accompanying vertical Scrollbar:
my $main = new MainWindow;
my $box = $main->Listbox(-relief => 'sunken',
-width => -1, # Shrink to fit
-height => 5,
-setgrid => 'yes');
my @items = qw(One Two Three Four Five Six Seven
Eight Nine Ten Eleven Twelve);
foreach (@items) {
$box->insert('end', $_);
}
my $scroll = $main->Scrollbar(-command => ['yview', $box]);
So far so good. But merely setting them up does not mean that the
Listbox even knows that the Scrollbar is lying next to
it. Note that the scalar variable $scroll is how we
refer to the Scrollbar, thus, hooking the $box up to
handle $scroll events is a matter of configuration:
$box->configure(-yscrollcommand => ['set', $scroll]);
A complete script that makes use of this code (and adds the necessary
calls to pack and MainLoop;) is given as
the fifth example in
UserGuide.pod (and may be
found at http://w4.lns.cornell.edu/~pvhp/ptk/pod/.)
There was an old Perl/Tk tendency to have a bunch of ScrlFoo widgets (such as ScrlListbox). The use of such widgets is now deprecated in favor of a new Scrolled class, as in:
$w = $patent->Scrolled('Text',...);
The widgets that can be ->Scrolled() include:
Return to table of contents
9.8. How do I configure a Scrollbar to scroll multiple widgets?
Note that the widget type that you wish to scroll can be important as a scroll "unit" on a Text or Listbox may be a character (several pixels - depending on font) whereas it would be an X "units" on a Canvas (could be pixel - but you may also specify other units).
A concrete answer for scrolling 3 Listboxes comes courtesy of Frederick L. Wagner <derf@ti.com>:
From a working example of multi-xscrolling:
sub multiscrollx
{ # multiscrollx
my ($sb,$wigs,@args) = @ARG;
my $w;
foreach $w (@$wigs)
{
$w->xview(@args);
}
} # multiscrollx
# %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
$sh->configure( -command => [ \&multiscrollx, $sh,
[$scratchrule,$ruleheader,$ruletable]]);
$ruletable->configure( -xscrollcommand => [ 'set', $sh]);
$ruleheader->configure( -xscrollcommand => [ 'set', $sh]);
$scratchrule->configure(-xscrollcommand => [ 'set', $sh]);
In this case,
$sh is a horizontal Scrollbar,
$ruletable and $scratchrule are Tables
$ruleheader is an Entry
However, this approach is good for any widget with X-scrolling capability, I think. So the Y counterpart should be:
sub multiscrolly
{ # multiscrolly
my ($sb,$wigs,@args) = @ARG;
my $w;
foreach $w (@$wigs)
{
$w->yview(@args);
}
} # multiscrolly
# %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
$sv->configure( -command => [ \&multiscrolly, $sv,
[$l1,$l2,$l3]]);
$l1->configure( -yscrollcommand => [ 'set', $sv]);
$l2->configure( -yscrollcommand => [ 'set', $sv]);
$l3->configure( -yscrollcommand => [ 'set', $sv]);
Hope that helps.
Greg VanSickle <vansickl@bnr.ca> points out that this little script snippet does not provide for the binding of '<Button-2<' that he is accustomed to. He wrote a package called DSListbox to address this binding issue.
Conversely, Jong Park asked how to setup multiple Scrollbars to scroll the same widget. Nick Ing-Simmon's reply makes use of an anonymous sub and can be summed up in a little script that scrolls a Text widget (to see the scrolling in action type more than 20 lines of text into the widget):
#!/usr/local/bin/perl -w
use Tk;
my $mw = MainWindow->new();
my $s1 = $mw->Scrollbar(-orient => 'vertical');
my $s2 = $mw->Scrollbar(-orient => 'vertical');
$s1->pack(-side => 'left', -fill => 'y');
my $t = $mw->Text(
-yscrollcommand => sub{$s1->set(@_), $s2->set(@_)},
-wrap => 'word',
-width => 70,
-height => 20,
-font => $font,
-setgrid => 1,
)->pack(-side => 'left');
$s2->pack(-side => 'right', -fill => 'y');
$s1->configure(-command => [$t => 'yview']);
$s2->configure(-command => [$t => 'yview']);
MainLoop;
__END__
Return to table of contents
9.9. How do I display a bitmap?
You can display X bitmaps on your widgets with the -bitmap configuration option. Typically -bitmaps are configured into Label, Frame, Button, etc. widgets (Canvas widgets are another story however see question [10.1] below). In order to emphasize the bitmap option itself let us assume we were specifying a bitmap for a Label with a call like:
$main->Label(-bitmap => 'bitmap-name')->pack;
Where bitmap-name could be any of the built in Tk bitmaps:
error, gray25, gray50, hourglass,
info, question, questhead,
warning (see the widget demo for a full list).
In order to use some of the bitmaps in the perl5/Tk/demos/images/ directory you would specify a fuller path name like:
$main->Label(-bitmap => "\@$tk_library/demos/images/face")->pack;
Note the escaped "\@" on the directory specification (as
well as the use of the $tk_library variable imported by use
Tk;). If you wanted to specify a file called foobar.xbm in
the directory where you were running the script then either:
$main->Label(-bitmap => '@foobar.xbm')->pack;
#or
$main->Label(-bitmap => "\@foobar.xbm")->pack;
should work just fine. In another directory however that would be a problem.
So something like:
$main->Label(-bitmap => "\@$ENV{'HOME'}/img/foobar.xbm")->pack;
will help someone who has an img/foobar.xbm file in their $HOME
directory. If you don't mind the non-portability then hard-wiring in the full
path name will help as well. (Or if you have write access then put your files
in Tk/demos/images/ e.g.)
Return to table of contents
9.10. How do I display an image?
You will want to get a "Photo" handle on the file as in the following example where 'imggif' is the Photo handle for a gif file that is distributed with perl/Tk:
#!/usr/bin/perl -w
use strict;
use Tk;
my $main = new MainWindow;
$main ->Label(-text => 'Main')->pack;
$main -> Photo('imggif',
-file => "$Tk::tk_library/demos/images/earth.gif");
my $l = $main->Label('-image' => 'imggif')->pack;
$main->Button(-text => 'close',
-command => sub{destroy $main}
)->pack(-side => 'left');
$main->Button(-text => 'exit',
-command => [sub{exit}]
)->pack(-side => 'right');
MainLoop;
(Canvas widgets are another story however
see question a later question within this FAQ).
Return to table of contents
9.11. What Image types are available?
In addition to the Tk builtin bitmaps there is support for reading images from files in formats such as: X11 Bitmaps (.xbm), X Pixmaps (.xpm), and Graphics Inline Format (.