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

📄 ch17.htm

📁 《Perl 5 Unreleased》
💻 HTM
📖 第 1 页 / 共 5 页
字号:
17.5, this was done with lines 15, 19, and 23):

<BLOCKQUOTE>

<TT><FONT FACE="Courier">-command =&gt; sub {print &quot;$animal

\n&quot;; } ,</FONT></TT>

</BLOCKQUOTE>

<P>

You can refer to a subroutine by using the escaped ampersand with

the name of the subroutine. Any parameters that have to be passed

to the subroutine have to be specified with the <TT><FONT FACE="Courier">-command</FONT></TT>

parameters as one list. For example, consider the following function

which creates a button with a caption of <TT><FONT FACE="Courier">Stats</FONT></TT>,

and calls the subroutine <TT><FONT FACE="Courier">do_print when

the button is pressed</FONT></TT>:

<BLOCKQUOTE>

<TT><FONT FACE="Courier">$main-&gt;Button(-text =&gt; 'Stats',

<BR>

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-command

=&gt; [ \&amp;do_print , $inputfile, $outputfile ]<BR>

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

)-&gt;pack;</FONT></TT>

</BLOCKQUOTE>

<P>

Note the backslash in front of <TT><FONT FACE="Courier">\&amp;do_print</FONT></TT>.

This causes Perl to generate a reference to <TT><FONT FACE="Courier">sub

do_print</FONT></TT> rather than call it. The input variables,

<TT><FONT FACE="Courier">$inputfile</FONT></TT> and <TT><FONT FACE="Courier">$outputfile</FONT></TT>,

are passed by reference not by value, into <TT><FONT FACE="Courier">the

do_print</FONT></TT> subroutine.

<H3><A NAME="ArrangingtheLayoutofWidgets">Arranging the Layout

of Widgets</A></H3>

<P>

Widgets are laid out on a window using the <TT><FONT FACE="Courier">-pack</FONT></TT>,

<TT><FONT FACE="Courier">-padding</FONT></TT>, <TT><FONT FACE="Courier">-fill</FONT></TT>,

<TT><FONT FACE="Courier">-expand</FONT></TT>, and <TT><FONT FACE="Courier">-anchor</FONT></TT>

options of a widget. Windows also use a program called a geometry

manager in <TT><FONT FACE="Courier">Tk</FONT></TT>.

<P>

A geometry manager controls the arrangement of widgets in a window.

The most common geometry manager used in <TT><FONT FACE="Courier">pTk</FONT></TT>

is <TT><FONT FACE="Courier">pack</FONT></TT>. You have seen the

use of the <TT><FONT FACE="Courier">-pack</FONT></TT> function

to place buttons on a window in earlier sections of this chapter.

The <TT><FONT FACE="Courier">pack</FONT></TT> function is also

known informally as the &quot;packer.&quot; You can invoke <TT><FONT FACE="Courier">pack</FONT></TT>

at the time of widget creation via calls like

<BLOCKQUOTE>

<TT><FONT FACE="Courier">$widget-&gt;pack;</FONT></TT>

</BLOCKQUOTE>

<P>

where <TT><FONT FACE="Courier">widget</FONT></TT> can be any of

the <TT><FONT FACE="Courier">Perl/Tk</FONT></TT> widget primitives.

The <TT><FONT FACE="Courier">pack</FONT></TT> function is often

used in conjunction with the <TT><FONT FACE="Courier">Frame</FONT></TT>

container widget to arrange your widgets much like a hierarchically

arranged set of window panes. See Listing 17.6 and Figure 17.5.<P>

<A HREF="f17-5.gif" tppabs="http://www.mcp.com/815097600/0-672/0-672-30891-6/f17-5.gif"><B>Figure 17.5 :</B><I>The output from Listing 17.6.</I></A>

<HR>

<BLOCKQUOTE>

<B>Listing 17.6. Packing using frames.<BR>

</B>

</BLOCKQUOTE>

<BLOCKQUOTE>

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

&nbsp;2 use Tk;<BR>

&nbsp;3<BR>

&nbsp;4 $main = MainWindow-&gt;new;<BR>

&nbsp;5<BR>

&nbsp;6&nbsp;&nbsp;&nbsp;&nbsp; my $row1 = $main-&gt;Frame;<BR>

&nbsp;7&nbsp;&nbsp;&nbsp;&nbsp; $row1-&gt;pack(-side =&gt; 'top');

<BR>

&nbsp;8&nbsp;&nbsp;&nbsp;&nbsp; my $row2 = $main-&gt;Frame;<BR>

&nbsp;9&nbsp;&nbsp;&nbsp;&nbsp; $row2-&gt;pack(-side =&gt; 'bottom');

<BR>

10&nbsp;&nbsp;&nbsp;&nbsp; $row1-&gt;Label(-text =&gt; 'Left',

<BR>

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

=&gt; 'sunken' )-&gt;pack(-side =&gt; 'left');<BR>

11&nbsp;&nbsp;&nbsp;&nbsp; $row1-&gt;Label(-text =&gt; 'Right',

<BR>

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

=&gt; 'sunken' )-&gt;pack(-side =&gt; 'right');<BR>

12&nbsp;&nbsp;&nbsp;&nbsp; $row2-&gt;Label(-text =&gt; 'Left 2',

<BR>

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

=&gt; 'ridge' )-&gt;pack(-side =&gt; 'left');<BR>

13&nbsp;&nbsp;&nbsp;&nbsp; $row2-&gt;Label(-text =&gt; 'Right

2', <BR>

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

=&gt; 'ridge' )-&gt;pack(-side =&gt; 'right');<BR>

14&nbsp;&nbsp;&nbsp;&nbsp; MainLoop;</FONT></TT>

</BLOCKQUOTE>

<HR>

<P>

In Listing 17.6, we used two frames: one on the top and the other

on the bottom. These frames are called <TT><FONT FACE="Courier">$row1</FONT></TT>

and <TT><FONT FACE="Courier">$row2</FONT></TT> and are created

in lines 6 through 9. Then lines 10 and 11 create the two labels

in <TT><FONT FACE="Courier">$row1</FONT></TT>, and in lines 12

and 13, two more labels are created on the second frame. In this

fashion we have packed labels on frames, and the frames are then

packed onto the main window. You can think of this as building

a Mayan temple of sorts with widgets being placed one on top of

the other.

<P>

Note that <TT><FONT FACE="Courier">pack</FONT></TT> itself is

given parameters in this example. The default behavior of pack

is to have <TT><FONT FACE="Courier">-side =&gt; 'top'</FONT></TT>,

that is, align everything using the top edge. You can override

this behavior by specifying a different packing style such as

&quot;left&quot;, &quot;right&quot;, or &quot;bottom&quot;.

<P>

The <TT><FONT FACE="Courier">Tk*</FONT></TT> distribution has

a file called <TT><FONT FACE="Courier">popup</FONT></TT> that

uses the <TT><FONT FACE="Courier">-anchor</FONT></TT> option to

configure the layout of the <TT><FONT FACE="Courier">Radiobutton</FONT></TT>

widgets. The output of this demo is shown in Figure 17.6. Run

the following program to get the listing with line numbers for

reference.

<P>

<A HREF="f17-6.gif" tppabs="http://www.mcp.com/815097600/0-672/0-672-30891-6/f17-6.gif"><B>Figure 17.6 :</B><I>Using the -anchor widget option.</I></A>

<BLOCKQUOTE>

<TT><FONT FACE="Courier">$ nl popup | more</FONT></TT>

</BLOCKQUOTE>

<P>

There is also the <TT><FONT FACE="Courier">-anchor</FONT></TT>

configuration option for widgets. There are introductions to the

nine possible <TT><FONT FACE="Courier">-anchor</FONT></TT> values,

eight corresponding to the points on a compass and the ninth as

the center position. The nine possible values are set around 

line 22 with <TT><FONT FACE="Courier">list</FONT></TT>:

<BLOCKQUOTE>

<TT><FONT FACE="Courier">foreach $r ([qw(nw n ne)],[qw(w c e)],[qw(sw

s se)])</FONT></TT>

</BLOCKQUOTE>

<P>

In the beginning of the file, we  create a small popup window

to show when a button is clicked. Rather than create this window

every time, a button is created but is not shown immediately.

Around lines 13 through 17, the subroutine <TT><FONT FACE="Courier">Show</FONT></TT>

shows this window and requests it to be invisible after one second.

<P>

In the subroutine <TT><FONT FACE="Courier">Anchor</FONT></TT>,

a master frame is created (see line 21) with a ridge around it.

Then three frames are placed on it, each with three buttons showing

the anchor positions (see lines 25 through 29). The positions

and the labels for the buttons are shown in line 22.

<P>

When setting lots of uneven widgets on the same frame, you can

make their borders the same size by using <TT><FONT FACE="Courier">-fill

=&gt; 'style'</FONT></TT>. The style can be <TT><FONT FACE="Courier">none

| x | y | both</FONT></TT>. See the modified version of the radio

button application and contrast the code in Listing 17.7 with

the code in Listing 17.4. The output is shown in Figure 17.7,

which you can compare with Figure 17.3 to see how the buttons

with their borders are now shown and resized.<P>

<A HREF="f17-7.gif" tppabs="http://www.mcp.com/815097600/0-672/0-672-30891-6/f17-7.gif"><B>Figure 17.7 :</B><I>Using the <FONT FACE="Couier">-fill</FONT> option.</I></A>

<HR>

<BLOCKQUOTE>

<B>Listing 17.7. Using the </B><TT><B><FONT FACE="Courier">-fill</FONT></B></TT><B>

option.<BR>

</B>

</BLOCKQUOTE>

<BLOCKQUOTE>

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

&nbsp;2 <BR>

&nbsp;3 use Tk;<BR>

&nbsp;4 <BR>

&nbsp;5 #<BR>

&nbsp;6 # Using Checkbuttons and the -fill option.<BR>

&nbsp;7 #<BR>

&nbsp;8 <BR>

&nbsp;9 my $main = new MainWindow;<BR>

10 $main-&gt;title(&quot;fill&quot;);<BR>

11 <BR>

12 $main-&gt;Checkbutton(-text =&gt; 'One',<BR>

13&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; -relief =&gt; 'ridge',

<BR>

14&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; -command =&gt; sub {print

&quot;One \n&quot;; } )-&gt;pack(-fill =&gt; 'x');<BR>

15 $main-&gt;Checkbutton(-text =&gt; 'Two',<BR>

16&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; -relief =&gt; 'sunken',

<BR>

17&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; -command =&gt; sub {print

&quot;Two \n&quot;; } )-&gt;pack(-fill =&gt; 'x');<BR>

18 $main-&gt;Checkbutton(-text =&gt; 'Three',<BR>

19&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; -relief =&gt; 'groove',

<BR>

20&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; -command =&gt; sub {print

&quot;Three \n&quot;; } )-&gt;pack(-fill =&gt; 'x');<BR>

20<BR>

21 MainLoop;</FONT></TT>

</BLOCKQUOTE>

<HR>

<P>

When laying out your widgets, look at their behavior with multiple

<TT><FONT FACE="Courier">resize</FONT></TT> operations. The <TT><FONT FACE="Courier">-expand</FONT></TT>

option of either <TT><FONT FACE="Courier">pack</FONT></TT> or

the widget itself can be used to set whether the widget expands

or shrinks with its parent. Add the statement for packing the

buttons in Listing 17.8; the buttons will shrink or expand as

the main window is resized.

<HR>

<BLOCKQUOTE>

<B>Listing 17.8. Using the Expand button.<BR>

</B>

</BLOCKQUOTE>

<BLOCKQUOTE>

<TT><FONT FACE="Courier">1 $main-&gt;Checkbutton(-text =&gt; 'One',

<BR>

2&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-relief =&gt;

'ridge',<BR>

3&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-command =&gt;

sub {print &quot;One \n&quot;; } )-&gt;pack(-fill =&gt; 'x', -expand

=&gt; '1');<BR>

4 $main-&gt;Checkbutton(-text =&gt; 'Two',<BR>

5&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-relief =&gt;

'sunken',<BR>

6&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-command =&gt;

sub {print &quot;Two \n&quot;; } )-&gt;pack(-fill =&gt; 'x', -expand

=&gt; '1');<BR>

7 $main-&gt;Checkbutton(-text =&gt; 'Three',<BR>

8&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-relief =&gt;

'groove',<BR>

9&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-command =&gt;

sub {print &quot;Three \n&quot;; } )-&gt;pack(-fill =&gt; 'x',

-expand =&gt; '1');</FONT></TT>

</BLOCKQUOTE>

<HR>

<P>

The output of this change to Listing 17.7 is shown in Figure 17.8. Remember to make this change to all the <TT><FONT FACE="Courier">pack</FONT></TT>

calls for the buttons.<P>

<A HREF="f17-8.gif" tppabs="http://www.mcp.com/815097600/0-672/0-672-30891-6/f17-8.gif"><B>Figure 17.8 :</B><I>Using the <FONT FACE="Courier"-expend</FONT> option.</I></A><P>



<H3><A NAME="UsingtheListboxandScrollbarWidgets">Using the <TT><FONT SIZE=4 FACE="Courier">Listbox</FONT></TT><FONT SIZE=4>

and </FONT><TT><FONT SIZE=4 FACE="Courier">Scrollbar</FONT></TT><FONT SIZE=4>

Widgets</FONT></A></I></H3>

<P>

Now that you know how to place items on a frame widget, let's

see how you create a list of scrollable items. For this exercise,

you'll use the <TT><FONT FACE="Courier">Listbox</FONT></TT> and

<TT><FONT FACE="Courier">Scrollbar</FONT></TT> widgets. The output

we are trying to get is shown in Figure 17.9. The code to get

this output is shown in Listing 17.9.<P>

<A HREF="f17-9.gif" tppabs="http://www.mcp.com/815097600/0-672/0-672-30891-6/f17-9.gif"><B>Figure 17.9 :</B><I>Using the <FONT FACE="Courier">Listbox </FONT>and <FONT FACE="Courier"> Scrollbar</FONT> widgers.</I></A><P>

<HR>

<BLOCKQUOTE>

<B>Listing 17.9. Using the </B><TT><B><FONT FACE="Courier">Listbox</FONT></B></TT><B>

and </B><TT><B><FONT FACE="Courier">Scrollbar</FONT></B></TT><B>

widgets.<BR>

</B>

</BLOCKQUOTE>

<BLOCKQUOTE>

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

&nbsp;2 use Tk;<BR>

&nbsp;3<BR>

&nbsp;4 my $main = new MainWindow;<BR>

&nbsp;5 #<BR>

&nbsp;6 # Provide another title ...<BR>

&nbsp;7 #<BR>

&nbsp;8 $main-&gt;Label(-relief =&gt; raised,<BR>

&nbsp;9&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-text

=&gt; &quot;Tax Confusion&quot; )-&gt;pack(-side =&gt; 'top',

-fill =&gt; 'x');<BR>

10 $main-&gt;title(&quot;Test Listbox&quot;);<BR>

11 $w_list = $main-&gt;Listbox(-relief =&gt; 'raised',<BR>

12&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-setgrid

=&gt; 'yes');<BR>

13 #<BR>

14 # Create a list of words of wisdom<BR>

15 #<BR>

16 my @items = qw( Passive activity income <BR>

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;does

not include the following:<BR>

⌨️ 快捷键说明

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