📄 ch17.htm
字号:
<HTML>
<HEAD>
<TITLE>Chapter 17 -- GUI Interfaces with Perl/Tk</TITLE>
<META NAME="GENERATOR" CONTENT="Mozilla/3.0b5aGold (WinNT; I) [Netscape]">
</HEAD>
<BODY TEXT="#000000" BGCOLOR="#FFFFFF" LINK="#0000EE" VLINK="#551A8B" ALINK="#CE2910">
<H1><FONT COLOR=#FF0000>Chapter 17</FONT></H1>
<H1><B><FONT SIZE=5 COLOR=#FF0000>GUI Interfaces with </FONT><TT><FONT FACE="Courier" COLOR=#FF0000>Perl/Tk</FONT></TT></FONT></B>
</H1>
<P>
<HR WIDTH="100%"></P>
<P>
<H3 ALIGN=CENTER><FONT COLOR="#000000"><FONT SIZE=+2>CONTENTS<A NAME="CONTENTS"></A>
</FONT></FONT></H3>
<UL>
<LI><A HREF="#IntroductiontothePerlTkPackage" >Introduction to the Perl/Tk Package</A>
<UL>
<LI><A HREF="#AbouttheAuthorsofthePerlTkPackage" >About the Authors of the Perl/Tk Package</A>
</UL>
<LI><A HREF="#WhereDoIGetandBuildPerlTk" >Where Do I Get and Build Perl/Tk?</A>
<LI><A HREF="#DocumentationforPerlTk" >Documentation for Perl/Tk</A>
<LI><A HREF="#WritingScriptsinPerlTk" >Writing Scripts in Perl/Tk</A>
<LI><A HREF="#WidgetsasBuildingBlocks" >Widgets as Building Blocks</A>
<LI><A HREF="#ConfiguringWidgets" >Configuring Widgets</A>
<UL>
<LI><A HREF="#UsingCheckButtonWidgets" >Using CheckButton Widgets</A>
<LI><A HREF="#UsingRadioButtonWidgets" >Using RadioButton Widgets</A>
<LI><A HREF="#CallingSubroutinesWhenaButtonIsPre" >Calling Subroutines When a Button Is Pressed</A>
<LI><A HREF="#ArrangingtheLayoutofWidgets" >Arranging the Layout of Widgets</A>
<LI><A HREF="#UsingtheListboxandScrollbarWidgets" >Using the Listbox and Scrollbar Widgets</A>
<LI><A HREF="#UsingTextWidgets" >Using Text Widgets</A>
<LI><A HREF="#SpecifyingFontsforTextandOtherWidg" >Specifying Fonts for Text and Other Widgets</A>
<LI><A HREF="#TextEntryWidgets" >Text Entry Widgets</A>
</UL>
<LI><A HREF="#HandlingtheKeyboardwithKeyBindings" >Handling the Keyboard with Key Bindings</A>
<LI><A HREF="#DisplayingaBitmap" >Displaying a Bitmap</A>
<LI><A HREF="#DisplayingImageswithPhoto" >Displaying Images with Photo</A>
<LI><A HREF="#Menus" >Menus</A>
<LI><A HREF="#TheCanvasWidget" >The Canvas Widget</A>
<LI><A HREF="#MiscellaneousTopics" >Miscellaneous Topics</A>
<UL>
<LI><A HREF="#ReferenceswithSubroutines" >References with Subroutines</A>
<LI><A HREF="#ChangingtheCursor" >Changing the Cursor</A>
<LI><A HREF="#DebuggingYourPerlTkScripts" >Debugging Your Perl/Tk Scripts</A>
<LI><A HREF="#MoreThanOneInputFileHandle" >More Than One Input File Handle</A>
<LI><A HREF="#EliminatingandHidingWindows" >Eliminating and Hiding Windows</A>
<LI><A HREF="#CreatingMoreThanOneIndependentWindo" >Creating More Than One Independent Window</A>
</UL>
<LI><A HREF="#Summary" >Summary</A>
</UL>
<HR>
<P>
This chapter introduces you to using the <TT><FONT FACE="Courier">Perl/Tk</FONT></TT>
package to create graphical user interfaces (GUI) with Perl. After
reading and working through the examples in this chapter, you
should be able to create interactive user interfaces for your
Perl scripts with very short design times.
<H2><A NAME="IntroductiontothePerlTkPackage"><FONT SIZE=5 COLOR=#FF0000>Introduction
to the </FONT><TT><FONT SIZE=5 COLOR=#FF0000 FACE="Courier">Perl/Tk</FONT></TT><FONT SIZE=5 COLOR=#FF0000>
Package</FONT></A></H2>
<P>
The set of <TT><FONT FACE="Courier">Tk</FONT></TT> modules attempts
to tie the <TT><FONT FACE="Courier">Tk</FONT></TT> widget toolkit
with Perl 5. The set is referred to as <TT><FONT FACE="Courier">Perl/Tk</FONT></TT>,
<TT><FONT FACE="Courier">pTk</FONT></TT>, or even <TT><FONT FACE="Courier">ptk</FONT></TT>.
The idea behind the merging of this set is to get the best features
of both languages: Perl has very good text handling, regular expression
support, dynamic memory, file and pipe I/O, and object-oriented
capabilities for processing data; <TT><FONT FACE="Courier">Tk</FONT></TT>
gives you a quick and easy way to create GUI programs. <TT><FONT FACE="Courier">Perl/Tk</FONT></TT>
is most easily used on a UNIX computer platform running the X
Window system. <TT><FONT FACE="Courier">Perl/Tk</FONT></TT> does
not require any of the lexical features of the <TT><FONT FACE="Courier">Tcl</FONT></TT>
scripting language. In other words, you do not have to be an expert
at <TT><FONT FACE="Courier">Tcl/Tk</FONT></TT>, but it will be
in your best interest to have worked with the <TT><FONT FACE="Courier">Tcl/Tk</FONT></TT>
toolkit on its own to take the most advantage of the <TT><FONT FACE="Courier">Perl/Tk</FONT></TT>
package.This chapter will not make you a <TT><FONT FACE="Courier">Tk</FONT></TT>
expert. I assume you have a rudimentary working knowledge of <TT><FONT FACE="Courier">Tcl/Tk</FONT></TT>
programming. However, if you have never used <TT><FONT FACE="Courier">Tcl/Tk</FONT></TT>
before, now would be a good time to read the sample code for your
<TT><FONT FACE="Courier">Tcl/Tk</FONT></TT> package on your system.
Here is a good starting reference book by the author of <TT><FONT FACE="Courier">Tcl</FONT></TT>
himself, Dr. John K. Ousterhout, professor at the University of
California at Berkeley:
<BLOCKQUOTE>
<I>Tcl and the Tk Toolkit</I>, John K. Ousterhout, ISBN 0-201-63337-X,
<BR>
Addison-Wesley Publishing Company, 1994
</BLOCKQUOTE>
<P>
There is one thing to remember when reading his book: You still
have to translate the <TT><FONT FACE="Courier">Tcl/Tk</FONT></TT>
scripts to Perl.
<P>
The <TT><FONT FACE="Courier">pTk</FONT></TT> library is a version
of the <TT><FONT FACE="Courier">Tk</FONT></TT> 4.0 toolkit. The
library allows easier external linking and calling by Perl scripts
and can be modularized. A number of composite widget extensions
to the language have been written using Perl modules. Note that
<TT><FONT FACE="Courier">ptk</FONT></TT> does not necessarily
refer to <TT><FONT FACE="Courier">Perl/Tk</FONT></TT>, but could
be taken to mean <I>portable Tk</I>; that is, portable to another
language such as Perl, C, LISP, C++, and so on. It just so happens
that our present work concentrates on Perl.
<H3><A NAME="AbouttheAuthorsofthePerlTkPackage">About the Authors
of the <TT><FONT SIZE=4 FACE="Courier">Perl/Tk</FONT></TT><FONT SIZE=4>
Package</FONT></A></H3>
<P>
The <TT><FONT FACE="Courier">Perl/Tk</FONT></TT> package is still
in beta phase and is being written primarily by Nick Ing-Simmons
<TT><FONT FACE="Courier"><Nick.Ing-Simmons@tiuk.ti.com></FONT></TT>
at Texas Instruments in Northampton, England. Mr. Nick was gracious
enough to let us use his examples as the basis for the code samples
in this chapter. He asked me to remind you that he is no way liable
for how you use his package. He also requests that you give him
credit for his authorship when you do use it. With regard to liability,
he provides the following comments: <P>
<CENTER>
<TABLE BORDERCOLOR=#000000 BORDER=1 WIDTH=80%>
<TR VALIGN=TOP><TD WIDTH=586><B>License Agreement</B></TD></TR>
<TR VALIGN=TOP><TD WIDTH=586>
<BLOCKQUOTE>
<FONT SIZE=2>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.</FONT>
</BLOCKQUOTE>
<BLOCKQUOTE>
<FONT SIZE=2>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.</FONT>
</BLOCKQUOTE>
</TD></TR>
</TABLE></CENTER>
<P>
<P>
<TT><FONT FACE="Courier">TkPerl</FONT></TT> was originally the
name of a Perl 4 package that Mr. Malcolm Beattie, <TT><FONT FACE="Courier"><mbeattie@sable.ox.ac.uk></FONT></TT>,
at Oxford University used to mesh the <TT><FONT FACE="Courier">Tk</FONT></TT>
toolkit with Perl. He also authored a <TT><FONT FACE="Courier">Tcl</FONT></TT>
module for Perl 5 with a <TT><FONT FACE="Courier">Tcl::Tk</FONT></TT>
module extension. That module allows the use of <TT><FONT FACE="Courier">Tcl</FONT></TT>
within a Perl script. You still must know both languages to get
your widgets to work. I do not cover these libraries here.<P>
<CENTER>
<TABLE BORDERCOLOR=#000000 BORDER=1 WIDTH=80%>
<TR VALIGN=TOP><TD ><B>Tip</B></TD></TR>
<TR VALIGN=TOP><TD >
<BLOCKQUOTE>
The name <TT><FONT FACE="Courier">Tkperl</FONT></TT> is sometimes applied to the <TT><FONT FACE="Courier">Perl/Tk</FONT></TT>, <TT><FONT FACE="Courier">pTk</FONT></TT>, or <TT><FONT FACE="Courier">ptk</FONT></TT> package.
</BLOCKQUOTE>
</TD></TR>
</TABLE></CENTER>
<P>
<CENTER>
<TABLE BORDERCOLOR=#000000 BORDER=1 WIDTH=80%>
<TR VALIGN=TOP><TD ><B>Caution</B></TD></TR>
<TR VALIGN=TOP><TD >
<BLOCKQUOTE>
Do <I>not</I> attempt to install both <TT><FONT FACE="Courier">perl/Tk</FONT></TT> and <TT><FONT FACE="Courier">Tcl/Tkperl</FONT></TT> in the same Perl installation. The names in the respective modules must overlap or something. I had the darnedest time
getting some of scripts to work right. Turns out that there is a warning note in the FAQ about this behavior. I could not get both of them to work together in the same Perl installation tree. Bowing to editorial schedules, I blew away all the <TT><FONT
FACE="Courier">Tk*</FONT></TT> directories and installed <TT><FONT FACE="Courier">Tk-b9.01</FONT></TT> only. Things improved a bit, but some of the samples do not work at all.
</BLOCKQUOTE>
</TD></TR>
</TABLE></CENTER>
<P>
<P>
A more extensive comparison of the differences between the <TT><FONT FACE="Courier">Tkperl</FONT></TT>
and the <TT><FONT FACE="Courier">Perl/Tk</FONT></TT> code is given
in the <TT><FONT FACE="Courier">Tcl-perl.pod</FONT></TT> file
that is distributed with the latest <TT><FONT FACE="Courier">Perl/Tk</FONT></TT>
package. The FAQs for <TT><FONT FACE="Courier">Perl/Tk</FONT></TT>
did provide some hints about getting the two packages to work
together along with some friendly warnings about it not being
very easy to do.
<H2><A NAME="WhereDoIGetandBuildPerlTk"><FONT SIZE=5 COLOR=#FF0000>Where
Do I Get and Build </FONT><TT><FONT SIZE=5 COLOR=#FF0000 FACE="Courier">Perl/Tk</FONT></TT><FONT SIZE=5 COLOR=#FF0000>?</FONT></A>
</H2>
<P>
At the time this book was written, the latest version was <TT><FONT FACE="Courier">Tk-b9.01</FONT></TT>.
You'll need <TT><FONT FACE="Courier">Perl 5.002b1</FONT></TT>
or later to be able to work with the <TT><FONT FACE="Courier">Tk-b9.01</FONT></TT>
kit. If you are like me and are skipping chapters while reading
a book, don't skip the installation of Perl 5.002. There are some
patches to 5.001m, but the 5.002 code will already have these
patches in there. You need Perl 5.002, so install it first.
<P>
You can get the toolkit from any CPAN site. Here are the sites
where I found the latest versions:
<P>
<BLOCKQUOTE>
<TT><FONT FACE="Courier"><A HREF="ftp://ftp.cis.ufl.edu/pub/perl/CPAN/" tppabs="ftp://ftp.cis.ufl.edu/pub/perl/CPAN/">ftp://ftp.cis.ufl.edu/pub/perl/CPAN/</A>
<BR>
<A HREF="ftp://ftp.uoknor.edu/mirrors/CPAN/" tppabs="ftp://ftp.uoknor.edu/mirrors/CPAN/">ftp://ftp.uoknor.edu/mirrors/CPAN/</A><BR>
<A HREF="ftp://ftp.metronet.com/pub/perl" tppabs="ftp://ftp.metronet.com/pub/perl">ftp://ftp.metronet.com/pub/perl</A><BR>
<A HREF="ftp://ftp.perl.com/CPAN" tppabs="ftp://ftp.perl.com/CPAN">ftp://ftp.perl.com/CPAN</A></FONT></TT>
</BLOCKQUOTE>
<P>
Several more sites are listed in the FAQ and via the search results
in Netscape Navigator. Try to get the package from a site that's
geographically nearest you.
<P>
Building the <TT><FONT FACE="Courier">Perl/Tk</FONT></TT> package
after you get it is going to take some time and effort on your
part. If you get any errors, most of them will be due to the use
of out-of-date libraries or the wrong version of Perl, or both.
Read the following directions carefully before you begin the installation.
<P>
First, unpack <TT><FONT FACE="Courier">perl/Tk</FONT></TT> in
your home directory. This will be your <TT><FONT FACE="Courier">Tk</FONT></TT>
build directory and will not corrupt any existing Perl 5 tree
should something go wrong. Unzip and untar the files in this test
directory. Use these commands:
<BLOCKQUOTE>
<TT><FONT FACE="Courier">gunzip Tk-b9.01.tar.gz<BR>
tar -xvf Tk-b9.01.tar</FONT></TT>
</BLOCKQUOTE>
<P>
Next, create the make files. First, change the directory to the
<TT><FONT FACE="Courier">Tk/build</FONT></TT> directory, and then
run the following commands to get to the directory, create the
Makefiles, and create the package, respectively:
<BLOCKQUOTE>
<TT><FONT FACE="Courier">cd Tk-b9.01
<BR>
perl Makefile.PL<BR>
make<BR>
make install
</FONT></TT>
</BLOCKQUOTE>
<P>
After you have built the files, check to see if you had any errors.
<P>
It's <I>not</I> a good idea to take a coffee break while this
builds because some error messages might not be fatal and you
may have an incomplete build and not see the error messages fly
by. Take note of any missing references to libraries or header
files.
<P>
If there were any errors due to missing make files or references
to some header files, check the <TT><FONT FACE="Courier">cc</FONT></TT>
command in the make files for the directory with the offending
error. Chances are that you do not have any inclusions of the
<TT><FONT FACE="Courier">pTk</FONT></TT> subdirectory header files.
To help this out, try including the <TT><FONT FACE="Courier">$(Inc)</FONT></TT>
variable in the <TT><FONT FACE="Courier">$ccCMD</FONT></TT> statement
in the <TT><FONT FACE="Courier">GNUmakefile</FONT></TT> file.
You may have to do this for all the subdirectories under the <TT><FONT FACE="Courier">Tk</FONT></TT>
directory. Most of the errors I got during the build process were
correctable with this inclusion.
<P>
Now, test the demo programs in the <TT><FONT FACE="Courier">./Tk/demos</FONT></TT>
directory. Not all of the demos worked on my Linux system. The
major complaint concerns <TT><FONT FACE="Courier">CreateArgs()</FONT></TT>
in the <TT><FONT FACE="Courier">Toplevel</FONT></TT> module causing
too many recursive calls. You may fare better. Minor tweaking
did not remove this error. Major surgery seemed too daunting a
task at this stage. Basically, any demo involving dialog boxes
did not work. Do not waste your time trying to get these demos
to work. Here is the error message that showed this problem:
<BLOCKQUOTE>
<TT><FONT FACE="Courier">Deep recursion on subroutine "CreateArgs"
<BR>
at /usr/lib/perl5/Tk/Toplevel.pm line 16.</FONT></TT>
</BLOCKQUOTE>
<P>
Even with this annoying bug, you should be able to install and
work with the rest of the <BR>
package.
<P>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -