📄 ch17.htm
字号:
If you use the most current versions of the C++ compiler and make
files, circa 1994 or later, and X11R6, you should have no problems.
In case of problems, please consult the <TT><FONT FACE="Courier">FAQ</FONT></TT>,
<TT><FONT FACE="Courier">INSTALL</FONT></TT>, or <TT><FONT FACE="Courier">README</FONT></TT>
files for your specific system.<P>
<CENTER>
<TABLE BORDERCOLOR=#000000 BORDER=1 WIDTH=80%>
<TR VALIGN=TOP><TD ><B>Tip</B></TD></TR>
<TR VALIGN=TOP><TD >
<BLOCKQUOTE>
Keep in mind that your installation may have Perl in a different place than shown here. Instead of <TT><FONT FACE="Courier">/usr/bin/perl</FONT></TT>, you may have to use <TT><FONT FACE="Courier">/usr/local/bin/perl</FONT></TT>.
</BLOCKQUOTE>
</TD></TR>
</TABLE></CENTER>
<P>
<H2><A NAME="DocumentationforPerlTk"><FONT SIZE=5 COLOR=#FF0000>Documentation
for </FONT><TT><FONT SIZE=5 COLOR=#FF0000 FACE="Courier">Perl/Tk</FONT></TT></A>
</H2>
<P>
The <TT><FONT FACE="Courier">make install</FONT></TT> execution
installs the <TT><FONT FACE="Courier">pod</FONT></TT> files for
you in <TT><FONT FACE="Courier">nroff</FONT></TT> format. Check
the <TT><FONT FACE="Courier">perl5/Tk/</FONT></TT> directory for
the file <TT><FONT FACE="Courier">UserGuide.pod</FONT></TT>. This
file is editable and readable by humans. <TT><FONT FACE="Courier">pod</FONT></TT>
files have to be interpreted through a reformatting program such
as <TT><FONT FACE="Courier">pod2man</FONT></TT>, <TT><FONT FACE="Courier">pod2html</FONT></TT>,
or <TT><FONT FACE="Courier">pod2latex</FONT></TT>, all of which
should be in your Perl distribution.
<P>
The following commands provide you with information in HTML files
or in <TT><FONT FACE="Courier">man</FONT></TT> pages:
<BLOCKQUOTE>
<TT><FONT FACE="Courier">pod2man ~khusain/p5/Tk/UserGuide.pod
| nroff -man | more<BR>
pod2html ~khusain/p5/Tk/UserGuide<BR>
perldoc Toplevel.pm</FONT></TT>
</BLOCKQUOTE>
<P>
The most visually pleasing of the three methods is the <TT><FONT FACE="Courier">pod2html</FONT></TT>
method:
<BLOCKQUOTE>
<TT><FONT FACE="Courier">$ <B>pod2html perl5/Tk/Dial.pm<BR>
</B>Creating Dial.pm.html from Dial.pm</FONT></TT>
</BLOCKQUOTE>
<P>
The output of this command is as follows (you can use Mosaic or
Netscape to view the contents of this file):
<BLOCKQUOTE>
<TT><FONT FACE="Courier"><!-- $Id$ --><BR>
<HTML><HEAD><BR>
<CENTER><TITLE>Dial.pm</TITLE><BR>
</HEAD><BR>
<BODY></CENTER><p><hr><BR>
<BR>
<H1><BR>
<A NAME="Dial.pm_name_0"><BR>
NAME</A><BR>
</H1><BR>
Dial - an alternative to the scale widget<BR>
<p><p><hr><BR>
<BR>
<H1><BR>
<A NAME="Dial.pm_synopsis_0"><BR>
SYNOPSIS</A><BR>
</H1><BR>
<BR>
<XMP><BR>
use Tk::Dial;<BR>
$dial = $top->Dial(-margin => 20,<BR>
-radius
=> 48,<BR>
-min => 0,
<BR>
-max =>
100,<BR>
-value => 0,
<BR>
-format
=> '%d');<BR>
margin - blank space to leave around dial<BR>
radius - radius of dial<BR>
min, max - range of possible values<BR>
value - current value<BR>
format - printf-style format for displaying format<BR>
<BR>
</XMP><BR>
<p>Values shown above are defaults.<BR>
<p><p><hr><BR>
<BR>
<H1><BR>
<A NAME="Dial.pm_description_0"><BR>
DESCRIPTION</A><BR>
</H1><BR>
A dial looks like a speedometer: a 3/4 circle with a needle indicating
the current value. Below the graphical dial is an entry
that displays the current value, and which can be used to enter
a value by hand. <p>The needle is moved by pressing button
1 in the canvas and dragging. The needle will follow the mouse,
even if the mouse leaves the canvas, which allows for high precision.
Alternatively, the user can enter a value in the entry space and
press Return to set the value; the needle will be set accordingly.
<BR>
<p><p><hr><BR>
<BR>
<H1><BR>
<A NAME="Dial.pm_to_0"><BR>
TO DO</A><BR>
</H1><BR>
<BR>
<XMP><BR>
Configure<BR>
Tick marks<BR>
Step size<BR>
<BR>
</XMP><BR>
<p><p><hr><BR>
<BR>
<H1><BR>
<A NAME="Dial.pm_authors_0"><BR>
AUTHORS</A><BR>
</H1><BR>
Roy Johnson, <A HREF="MAILTO:rjohnson@shell.com">rjohnson@shell.com</A>
<BR>
<p>Based on a similar widget in XV, a program by John Bradley,
<BR>
<A HREF="MAILTO:bradley@cis.upenn.edu">bradley@cis.upenn.edu</A>
<BR>
<p><p><hr><BR>
<BR>
<H1><BR>
<A NAME="Dial.pm_history_0"><BR>
HISTORY </A><BR>
</H1><BR>
<BR>
<XMP><BR>
<BR>
August 1995: Released for critique by pTk mailing list<BR>
<BR>
</XMP><BR>
<p><BR>
</BODY><BR>
</HTML></FONT></TT>
</BLOCKQUOTE>
<P>
Also in the <TT><FONT FACE="Courier">Tk-b9.01/doc</FONT></TT>
directory are some <TT><FONT FACE="Courier">.ht</FONT></TT> files
which are copyrighted HTML pages of the documentation for the
package.
<H2><A NAME="WritingScriptsinPerlTk"><FONT SIZE=5 COLOR=#FF0000>Writing
Scripts in </FONT><TT><FONT SIZE=5 COLOR=#FF0000 FACE="Courier">Perl/Tk</FONT></TT></A>
</H2>
<P>
The idea behind writing the <TT><FONT FACE="Courier">Perl/Tk</FONT></TT>
script using the modules for the package is to make life easier.
The scripts have to include the <TT><FONT FACE="Courier">Tk</FONT></TT>
package with the <TT><FONT FACE="Courier">use Tk;</FONT></TT>
statement. You might want to consider using the warning switch
(<TT><FONT FACE="Courier">-w</FONT></TT>) and, if you are paranoid,
use the <TT><FONT FACE="Courier">use strict;</FONT></TT> statement
to do extra syntax checking.
<P>
Look at the simple "Hello, world" script shown in Listing
17.1.
<HR>
<BLOCKQUOTE>
<B>Listing 17.1. A simple "Hello, world" script.<BR>
</B>
</BLOCKQUOTE>
<BLOCKQUOTE>
<TT><FONT FACE="Courier"> 1 #!/usr/bin/perl -w
<BR>
2 #<BR>
3 # Simple Tk script to create a button that
prints "Hello, world".<BR>
4 #<BR>
5 use Tk;<BR>
6 $mw = MainWindow->new;<BR>
7 $hello = $mw->Button(-text => 'Hello,
world',<BR>
<FONT FACE="ZAPFDINGBATS">Â</FONT>
-command => sub {print STDOUT "Hello, world\n"; });
<BR>
8 $greet = $mw->Button(-text => 'Greetings',
<BR>
<FONT FACE="ZAPFDINGBATS">Â</FONT>
-command => sub {print STDOUT "Greetings\n"; exit;});
<BR>
9 $hello->pack;<BR>
10 $greet->pack(-side => "left");<BR>
11 MainLoop;</FONT></TT>
</BLOCKQUOTE>
<HR>
<P>
The first line in the file starts Perl with the <TT><FONT FACE="Courier">-w</FONT></TT>
switch for warnings to be issued, if necessary. Line 5 imports
the <TT><FONT FACE="Courier">Tk</FONT></TT> objects into the application.
The statement in line 6 creates the main window. The "Hello,
world" and Greetings buttons are created in lines 7 and 8.
In line 9, the Hello button is added to the main window. In line
10, the Greetings button is <TT><FONT FACE="Courier">pack</FONT></TT>-ed
into the main window on the left side of the Hello button. (I
cover <TT><FONT FACE="Courier">pack</FONT></TT>-ing later throughout
this chapter.) Line 11 starts the main user interface loop for
this application. The output is shown in Figure 17.1.
<P>
<A HREF="f17-1.gif" tppabs="http://www.mcp.com/815097600/0-672/0-672-30891-6/f17-1.gif"><B>Figure 17.1 :</B><I>Sample "Hello, world" application.</I></A>
<P>
The <TT><FONT FACE="Courier">MainLoop;</FONT></TT> statement is
the main widget event handler loop and is required in all <TT><FONT FACE="Courier">Perl/Tk</FONT></TT>
scripts. This event handler statement is usually near the end
of the <TT><FONT FACE="Courier">main</FONT></TT> procedure after
the widgets have been created and are ready to be displayed.
<P>
In Listing 17.1, note that the Greet button defines an anonymous
function (at line 8) with a call to the <TT><FONT FACE="Courier">exit</FONT></TT>
function. In line 7, the <TT><FONT FACE="Courier">hello world</FONT></TT>
function does not call the <TT><FONT FACE="Courier">exit</FONT></TT>
function.
<P>
The objects in this application are added one object at a time
in a hierarchical order. This <BR>
involves the use of the <TT><FONT FACE="Courier">-> </FONT></TT>infix
dereference operator; for example, the <TT><FONT FACE="Courier">$mw->Button(...)</FONT></TT>
call forces the button to be created with the <TT><FONT FACE="Courier">$mw</FONT></TT>
object as the parent. Almost all objects and parameters in calls
to <TT><FONT FACE="Courier">Perl/Tk</FONT></TT> routines are passed
by reference.
<P>
Please note the use of the <TT><FONT FACE="Courier">=></FONT></TT>
operator, which is simply a synonym for the comma operator. The
program in Listing 17.1 could be rewritten as shown in Listings
17.2 and 17.3.
<HR>
<BLOCKQUOTE>
<B>Listing 17.2. The "Hello, world" application using
commas.<BR>
</B>
</BLOCKQUOTE>
<BLOCKQUOTE>
<TT><FONT FACE="Courier"> 1 #!/usr/bin/perl -w<BR>
2 #<BR>
3 # Using commas instead of =>.<BR>
4 #<BR>
5 <BR>
6 use Tk;<BR>
7 $mw = MainWindow->new;<BR>
8 $hello = $mw->Button(-text , 'Hello, world',<BR>
<FONT FACE="ZAPFDINGBATS">Â</FONT>-command , sub {print
STDOUT "Hello, world\n"; });<BR>
9 $greet = $mw->Button(-text , 'Greetings', <BR>
<FONT FACE="ZAPFDINGBATS">Â</FONT>-command , sub {print
STDOUT "Greetings\n"; exit;});<BR>
10 $hello->pack;<BR>
11 $greet->pack(-side , "left");<BR>
12 MainLoop;</FONT></TT>
</BLOCKQUOTE>
<HR>
<HR>
<BLOCKQUOTE>
<B>Listing 17.3. The "Hello, world" application using
hashes.<BR>
</B>
</BLOCKQUOTE>
<BLOCKQUOTE>
<TT><FONT FACE="Courier"> 1 #!/usr/bin/perl -w<BR>
2 #<BR>
3 # Using Hashes instead of dereferencing using -><BR>
4 #<BR>
5 <BR>
6 use Tk;<BR>
7 $mw = MainWindow->new;<BR>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -