⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ch17.htm

📁 《Perl 5 Unreleased》
💻 HTM
📖 第 1 页 / 共 5 页
字号:
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">&lt;!-- $Id$ --&gt;<BR>

&lt;HTML&gt;&lt;HEAD&gt;<BR>

&lt;CENTER&gt;&lt;TITLE&gt;Dial.pm&lt;/TITLE&gt;<BR>

&lt;/HEAD&gt;<BR>

&lt;BODY&gt;&lt;/CENTER&gt;&lt;p&gt;&lt;hr&gt;<BR>

<BR>

&lt;H1&gt;<BR>

&lt;A NAME=&quot;Dial.pm_name_0&quot;&gt;<BR>

NAME&lt;/A&gt;<BR>

&lt;/H1&gt;<BR>

Dial - an alternative to the scale widget<BR>

&lt;p&gt;&lt;p&gt;&lt;hr&gt;<BR>

<BR>

&lt;H1&gt;<BR>

&lt;A NAME=&quot;Dial.pm_synopsis_0&quot;&gt;<BR>

SYNOPSIS&lt;/A&gt;<BR>

&lt;/H1&gt;<BR>

<BR>

&lt;XMP&gt;<BR>

&nbsp;use Tk::Dial;<BR>

&nbsp;$dial = $top-&gt;Dial(-margin =&gt;&nbsp;&nbsp;20,<BR>

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-radius

=&gt;&nbsp;&nbsp;48,<BR>

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-min&nbsp;&nbsp;&nbsp;&nbsp;=&gt;&nbsp;&nbsp;&nbsp;0,

<BR>

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-max&nbsp;&nbsp;&nbsp;&nbsp;=&gt;

100,<BR>

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-value&nbsp;&nbsp;=&gt;&nbsp;&nbsp;&nbsp;0,

<BR>

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-format

=&gt; '%d');<BR>

&nbsp;margin - blank space to leave around dial<BR>

&nbsp;radius - radius of dial<BR>

&nbsp;min, max - range of possible values<BR>

&nbsp;value&nbsp;&nbsp;- current value<BR>

&nbsp;format - printf-style format for displaying format<BR>

<BR>

&lt;/XMP&gt;<BR>

&lt;p&gt;Values shown above are defaults.<BR>

&lt;p&gt;&lt;p&gt;&lt;hr&gt;<BR>

<BR>

&lt;H1&gt;<BR>

&lt;A NAME=&quot;Dial.pm_description_0&quot;&gt;<BR>

DESCRIPTION&lt;/A&gt;<BR>

&lt;/H1&gt;<BR>

A dial looks like a speedometer: a 3/4 circle with a needle indicating

the current value.&nbsp;&nbsp;Below the graphical dial is an entry

that displays the current value, and which can be used to enter

a value by hand. &lt;p&gt;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>

&lt;p&gt;&lt;p&gt;&lt;hr&gt;<BR>

<BR>

&lt;H1&gt;<BR>

&lt;A NAME=&quot;Dial.pm_to_0&quot;&gt;<BR>

TO DO&lt;/A&gt;<BR>

&lt;/H1&gt;<BR>

<BR>

&lt;XMP&gt;<BR>

&nbsp;Configure<BR>

&nbsp;Tick marks<BR>

&nbsp;Step size<BR>

<BR>

&lt;/XMP&gt;<BR>

&lt;p&gt;&lt;p&gt;&lt;hr&gt;<BR>

<BR>

&lt;H1&gt;<BR>

&lt;A NAME=&quot;Dial.pm_authors_0&quot;&gt;<BR>

AUTHORS&lt;/A&gt;<BR>

&lt;/H1&gt;<BR>

Roy Johnson, &lt;A HREF=&quot;MAILTO:rjohnson@shell.com&quot;&gt;rjohnson@shell.com&lt;/A&gt;

<BR>

&lt;p&gt;Based on a similar widget in XV, a program by John Bradley,

<BR>

&lt;A HREF=&quot;MAILTO:bradley@cis.upenn.edu&quot;&gt;bradley@cis.upenn.edu&lt;/A&gt;

<BR>

&lt;p&gt;&lt;p&gt;&lt;hr&gt;<BR>

<BR>

&lt;H1&gt;<BR>

&lt;A NAME=&quot;Dial.pm_history_0&quot;&gt;<BR>

HISTORY &lt;/A&gt;<BR>

&lt;/H1&gt;<BR>

<BR>

&lt;XMP&gt;<BR>

<BR>

August 1995: Released for critique by pTk mailing list<BR>

<BR>

&lt;/XMP&gt;<BR>

&lt;p&gt;<BR>

&lt;/BODY&gt;<BR>

&lt;/HTML&gt;</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 &quot;Hello, world&quot; script shown in Listing

17.1.

<HR>

<BLOCKQUOTE>

<B>Listing 17.1. A simple &quot;Hello, world&quot; script.<BR>

</B>

</BLOCKQUOTE>

<BLOCKQUOTE>

<TT><FONT FACE="Courier">&nbsp;1&nbsp;&nbsp;#!/usr/bin/perl -w

<BR>

&nbsp;2&nbsp;&nbsp;#<BR>

&nbsp;3&nbsp;&nbsp;# Simple Tk script to create a button that

prints &quot;Hello, world&quot;.<BR>

&nbsp;4&nbsp;&nbsp;#<BR>

&nbsp;5&nbsp;&nbsp;use Tk;<BR>

&nbsp;6&nbsp;&nbsp;$mw = MainWindow-&gt;new;<BR>

&nbsp;7&nbsp;&nbsp;$hello = $mw-&gt;Button(-text =&gt; 'Hello,

world',<BR>

&nbsp;&nbsp;&nbsp;&nbsp;<FONT FACE="ZAPFDINGBATS">&Acirc;</FONT>

-command =&gt; sub {print STDOUT &quot;Hello, world\n&quot;; });

<BR>

&nbsp;8&nbsp;&nbsp;$greet = $mw-&gt;Button(-text =&gt; 'Greetings',

<BR>

&nbsp;&nbsp;&nbsp;&nbsp;<FONT FACE="ZAPFDINGBATS">&Acirc;</FONT>

-command =&gt; sub {print STDOUT &quot;Greetings\n&quot;; exit;});

<BR>

&nbsp;9&nbsp;&nbsp;$hello-&gt;pack;<BR>

10 $greet-&gt;pack(-side =&gt; &quot;left&quot;);<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 &quot;Hello,

world&quot; 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 &quot;Hello, world&quot; 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">-&gt; </FONT></TT>infix

dereference operator; for example, the <TT><FONT FACE="Courier">$mw-&gt;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">=&gt;</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 &quot;Hello, world&quot; application using

commas.<BR>

</B>

</BLOCKQUOTE>

<BLOCKQUOTE>

<TT><FONT FACE="Courier">&nbsp;1 #!/usr/bin/perl -w<BR>

&nbsp;2 #<BR>

&nbsp;3 # Using commas instead of =&gt;.<BR>

&nbsp;4 #<BR>

&nbsp;5 <BR>

&nbsp;6 use Tk;<BR>

&nbsp;7 $mw = MainWindow-&gt;new;<BR>

&nbsp;8 $hello = $mw-&gt;Button(-text , 'Hello, world',<BR>

&nbsp;&nbsp;&nbsp;<FONT FACE="ZAPFDINGBATS">&Acirc;</FONT>-command , sub {print

STDOUT &quot;Hello, world\n&quot;; });<BR>

&nbsp;9 $greet = $mw-&gt;Button(-text , 'Greetings', <BR>

&nbsp;&nbsp;&nbsp;<FONT FACE="ZAPFDINGBATS">&Acirc;</FONT>-command , sub {print

STDOUT &quot;Greetings\n&quot;; exit;});<BR>

10 $hello-&gt;pack;<BR>

11 $greet-&gt;pack(-side , &quot;left&quot;);<BR>

12 MainLoop;</FONT></TT>

</BLOCKQUOTE>

<HR>

<HR>

<BLOCKQUOTE>

<B>Listing 17.3. The &quot;Hello, world&quot; application using

hashes.<BR>

</B>

</BLOCKQUOTE>

<BLOCKQUOTE>

<TT><FONT FACE="Courier">&nbsp;1 #!/usr/bin/perl -w<BR>

&nbsp;2 #<BR>

&nbsp;3 # Using Hashes instead of dereferencing using -&gt;<BR>

&nbsp;4 #<BR>

&nbsp;5 <BR>

&nbsp;6 use Tk;<BR>

&nbsp;7 $mw = MainWindow-&gt;new;<BR>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -