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

📄 ch17.htm

📁 《Perl 5 Unreleased》
💻 HTM
📖 第 1 页 / 共 5 页
字号:
&nbsp;8 my %hello = ('-text' , 'Hello, world',<BR>

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

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

&nbsp;9 my %greet = ('-text' , 'Greetings',<BR>

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

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

10 $mw-&gt;Button(%hello)-&gt;pack;<BR>

11 $mw-&gt;Button(%greet)-&gt;pack('-side', &quot;left&quot;);

<BR>

12 MainLoop;</FONT></TT>

</BLOCKQUOTE>

<HR>

<P>

As you can see, you have a lot of flexibility in how you write

your application. If you need more information on references,

please refer to <A HREF="ch3.htm" tppabs="http://www.mcp.com/815097600/0-672/0-672-30891-6/ch3.htm" >Chapter 3</A>, &quot;References,&quot;

to see why Listing 17.3 works.

<H2><A NAME="WidgetsasBuildingBlocks"><FONT SIZE=5 COLOR=#FF0000>Widgets

as Building Blocks</FONT></A></H2>

<P>

Basically, a widget can be &quot;created&quot; by simply calling

the <TT><FONT FACE="Courier">new</FONT></TT> method of the class

name. For example, to create a new window, you use the following

statement:

<BLOCKQUOTE>

<TT><FONT FACE="Courier">my $main = new MainWindow;<BR>

&nbsp;&nbsp;... <I>code here to add widgets</I> ...<BR>

MainLoop;</FONT></TT>

</BLOCKQUOTE>

<P>

This statement sets aside the necessary system memory, and so

on for a new <TT><FONT FACE="Courier">MainWindow</FONT></TT> widget.

The<TT><FONT FACE="Courier"> MainLoop;</FONT></TT> call causes

it to appear. The object &quot;created&quot; then can be called

via the variable <TT><FONT FACE="Courier">$main</FONT></TT>.

<P>

It's a good idea to hang on to each object as it's created in

the hierarchy. This way you can refer to the object explicitly

should you need to change its parameters. Thus, the return values

of most of your calls to create items are assigned to a variable:

<BLOCKQUOTE>

<TT><FONT FACE="Courier">$mybutton = $main-&gt;Button();</FONT></TT>

</BLOCKQUOTE>

<P>

Now you can use <TT><FONT FACE="Courier">$mybutton</FONT></TT>

to refer to this <TT><FONT FACE="Courier">Button</FONT></TT> object.

<H2><A NAME="ConfiguringWidgets"><FONT SIZE=5 COLOR=#FF0000>Configuring

Widgets</FONT></A></H2>

<P>

Widgets are configurable to show different types of viewing styles.

You can provide a number of configuration parameters via calls

to the <TT><FONT FACE="Courier">configure()</FONT></TT> function.

Configuration parameters also can be sent in when the widget is

created. For example, these two statements are equivalent:

<BLOCKQUOTE>

<TT><FONT FACE="Courier">my $button = $main-&gt;Button();<BR>

$button-&gt;configure(-text =&gt; 'Hello!');</FONT></TT>

</BLOCKQUOTE>

<P>

and

<BLOCKQUOTE>

<TT><FONT FACE="Courier">my $button = new $main-&gt;Button(-text

=&gt; 'Hello');</FONT></TT>

</BLOCKQUOTE>

<P>

The &quot;Hello, world&quot; application is simple enough to demonstrate

the use of <TT><FONT FACE="Courier">Button</FONT></TT> widgets

in <TT><FONT FACE="Courier">Perl/Tk</FONT></TT>. You do have access

to more widgets that you can use to create your own widgets. Check

the subdirectories under the <TT><FONT FACE="Courier">Tk-b9.01</FONT></TT>

tree to see what the widgets are. The <TT><FONT FACE="Courier">Canvas</FONT></TT>

widget is in the <TT><FONT FACE="Courier">Canvas</FONT></TT> subdirectory.

<P>

The following <TT><FONT FACE="Courier">Tk</FONT></TT> widgets

are available under <TT><FONT FACE="Courier">perl/Tk</FONT></TT>:

<P>

<CENTER>

<TABLE BORDERCOLOR=#000000 BORDER=1 WIDTH=80%>

<TR VALIGN=TOP><TD WIDTH=128><TT><FONT FACE="Courier">Checkbutton</FONT></TT>

</TD><TD WIDTH=403>Allows all, some, or no selections from multiple selections.

</TD></TR>

<TR VALIGN=TOP><TD WIDTH=128><TT><FONT FACE="Courier">Button</FONT></TT>

</TD><TD WIDTH=403>It's set to <TT><FONT FACE="Courier">ON</FONT></TT> or <TT><FONT FACE="Courier">OFF</FONT></TT>.

</TD></TR>

<TR VALIGN=TOP><TD WIDTH=128><TT><FONT FACE="Courier">Radiobutton</FONT></TT>

</TD><TD WIDTH=403>One of many selections.</TD></TR>

<TR VALIGN=TOP><TD WIDTH=128><TT><FONT FACE="Courier">Listbox</FONT></TT>

</TD><TD WIDTH=403>Allows user to select from a list of items.

</TD></TR>

<TR VALIGN=TOP><TD WIDTH=128><TT><FONT FACE="Courier">Scrollbar</FONT></TT>

</TD><TD WIDTH=403>For pointer usage.</TD></TR>

<TR VALIGN=TOP><TD WIDTH=128><TT><FONT FACE="Courier">Entry</FONT></TT></TD>

<TD WIDTH=403>Text entry widget.</TD></TR>

<TR VALIGN=TOP><TD WIDTH=128><TT><FONT FACE="Courier">Text</FONT></TT></TD>

<TD WIDTH=403>Text display widget.</TD></TR>

<TR VALIGN=TOP><TD WIDTH=128><TT><FONT FACE="Courier">Canvas</FONT></TT>

</TD><TD WIDTH=403>For drawing on.</TD></TR>

<TR VALIGN=TOP><TD WIDTH=128><TT><FONT FACE="Courier">Frame</FONT></TT></TD>

<TD WIDTH=403>For placing other widgets on.</TD></TR>

<TR VALIGN=TOP><TD WIDTH=128><TT><FONT FACE="Courier">Toplevel</FONT></TT>

</TD><TD WIDTH=403>For a new independent window.</TD></TR>

<TR VALIGN=TOP><TD WIDTH=128><TT><FONT FACE="Courier">Menu</FONT></TT></TD>

<TD WIDTH=403>An area set aside for menu buttons.</TD></TR>

<TR VALIGN=TOP><TD WIDTH=128><TT><FONT FACE="Courier">Menubutton</FONT></TT>

</TD><TD WIDTH=403>For a menu button on an area for a menu.</TD>

</TR>

</TABLE></CENTER>

<P>

<P>

Each of these widgets comes with its own set of configurable parameters.

As you work with these widgets you'll become familiar with the

parameters associated with them. The best way to know which parameters

are configurable is to check the Perl/<TT><FONT FACE="Courier">Tk</FONT></TT>

reference manuals to see what parameters are available for each

type of widget.

<P>

The <TT><FONT FACE="Courier">MainWindow</FONT></TT> widget is

required for your applications. This widget serves as the main

window for your <TT><FONT FACE="Courier">Perl/Tk</FONT></TT> applications.

Several functions exist for use as methods for this <TT><FONT FACE="Courier">MainWindow</FONT></TT>

object. For example, to set the title in the window, use the following

command:

<BLOCKQUOTE>

<TT><FONT FACE="Courier">$main-&gt;title(&quot; A sample window&quot;);</FONT></TT>

</BLOCKQUOTE>

<P>

I cover some of these methods in the next few sections. Let's

first see how we can place widgets on the main window.

<H3><A NAME="UsingCheckButtonWidgets">Using <TT><FONT SIZE=4 FACE="Courier">CheckButton</FONT></TT><FONT SIZE=4>

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

<P>

Using check buttons is relatively easy. See Listing 17.4. The

output is shown in Figure 17.2. The three buttons are shown side-by-side

in the same window. If the parameters specifying the sides on

the left are removed, the buttons appear as shown in Figure 17.3.

You get this output by uncommenting the lines that create the

buttons in Listing 17.4.<P>

<A HREF="f17-2.gif" tppabs="http://www.mcp.com/815097600/0-672/0-672-30891-6/f17-2.gif"><B>Figure 17.2 :</B><I>Using Check buttons.</I></A><P>

<A HREF="f17-3.gif" tppabs="http://www.mcp.com/815097600/0-672/0-672-30891-6/f17-3.gif"><B>Figure 17.3 :</B><I>Using check buttons without packing.</I></A>

<HR>

<BLOCKQUOTE>

<B>Listing 17.4. Using check buttons.<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&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#

<BR>

&nbsp;6&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#

Using Checkbuttons.<BR>

&nbsp;7&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#

<BR>

&nbsp;8 <BR>

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

10 <BR>

11 $main-&gt;Checkbutton(-text =&gt; 'One'&nbsp;&nbsp;)-&gt;pack;

<BR>

12 $main-&gt;Checkbutton(-text =&gt; 'Two'&nbsp;&nbsp;)-&gt;pack;

<BR>

13 $main-&gt;Checkbutton(-text =&gt; 'Three' )-&gt;pack;<BR>

14 <BR>

15 #<BR>

16 # Uncomment these lines to get the buttons to be side by side.

<BR>

17 #<BR>

18 # $main-&gt;Checkbutton(-text =&gt; 'One')-&gt;pack(-side =&gt;

'left');<BR>

19 # $main-&gt;Checkbutton(-text =&gt; 'Two')-&gt;pack(-side =&gt;

'left');<BR>

20 # $main-&gt;Checkbutton(-text =&gt; 'Three ')-&gt;pack(-side

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

21 <BR>

22 MainLoop;</FONT></TT>

</BLOCKQUOTE>

<HR>

<H3><A NAME="UsingRadioButtonWidgets">Using <TT><FONT SIZE=4 FACE="Courier">RadioButton</FONT></TT><FONT SIZE=4>

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

<P>

Radio buttons let you select one of many options, whereas check

buttons allow you to select as many as possible. When using check

buttons, you can press more than one button at a time. With radio

buttons, you are showing several alternatives to an option. The

value of the option is only one of several presented.

<P>

You have to specify the <TT><FONT FACE="Courier">variable</FONT></TT>

option on each radio button. The value of the <TT><FONT FACE="Courier">-variable</FONT></TT>

option is the escaped global Perl variable that will be assigned

the value. All related radio buttons have to be assigned the same

variable. Pressing each radio button causes the value of the variable

to be set. Thus, each radio button has to set the value of the

variable, too, with the <TT><FONT FACE="Courier">-value</FONT></TT>

configuration parameter. See Listing 17.5 for a sample script.

The output is shown in Figure 17.4.<P>

<A HREF="f17-4.gif" tppabs="http://www.mcp.com/815097600/0-672/0-672-30891-6/f17-4.gif"><B>Figure 17.4 :</B><I>Using radio buttons in <TT><FONT FACE="Couier">Perl/TK </FONT></TT>scripts.</I></A>

<HR>

<BLOCKQUOTE>

<B>Listing 17.5. Using radio buttons.<BR>

</B>

</BLOCKQUOTE>

<BLOCKQUOTE>

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

&nbsp;2<BR>

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

&nbsp;4&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#

<BR>

&nbsp;5&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#

Using RadioButtons.<BR>

&nbsp;6&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#

<BR>

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

&nbsp;8&nbsp;&nbsp;$main-&gt;title(&quot;Select Delicacy&quot;);

<BR>

&nbsp;9&nbsp;&nbsp;#<BR>

10 # Declare the global variable<BR>

11 #<BR>

12 $animal = 'anything';<BR>

13 $main-&gt;Radiobutton(-text =&gt; 'Yak', -variable =&gt; \$animal,

<BR>

14&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

-value =&gt; &quot;Yak&quot;,<BR>

15&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

-command =&gt; sub {print &quot;$animal \n&quot;; } ,<BR>

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

17 $main-&gt;Radiobutton(-text =&gt; 'Camel', -variable =&gt;

\$animal,<BR>

18&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

-value =&gt; &quot;Camel&quot;,<BR>

19&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

-command =&gt; sub {print &quot;$animal \n&quot;; } ,<BR>

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

21 $main-&gt;Radiobutton(-text =&gt; 'Llama', -variable =&gt;

\$animal,<BR>

22&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

-value =&gt; &quot;Llama&quot;,<BR>

23&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

-command =&gt; sub {print &quot;$animal \n&quot;; }<BR>

24&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

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

25 MainLoop;</FONT></TT>

</BLOCKQUOTE>

<HR>

<P>

Let's examine the new lines of code in Listing 17.5 in detail.

Line 8 sets the title of the application window to <TT><FONT FACE="Courier">Select

Delicacy</FONT></TT>. Line 12 declares and sets the value of the

global variable <TT><FONT FACE="Courier">$animal</FONT></TT> to

<TT><FONT FACE="Courier">anything</FONT></TT>.

<P>

Lines 13 through 16 specify the first <TT><FONT FACE="Courier">Radiobutton</FONT></TT>

with the title <TT><FONT FACE="Courier">Yak</FONT></TT>. The variable

whose value will be set when this button is pressed is specified

in the <TT><FONT FACE="Courier">-variable</FONT></TT> parameter

value. Note the escaped dollar sign for the name of the variable

<TT><FONT FACE="Courier">$animal</FONT></TT>. The <TT><FONT FACE="Courier">-command</FONT></TT>

configuration parameter specifies the anonymous subroutine to

call when the button is pressed. The subroutine simply prints

the value of the variable. You can add your code here.

<P>

In lines 17 through 20 and lines 21 through 24, two more radio

buttons are specified that set the values of <TT><FONT FACE="Courier">$animal</FONT></TT>

to <TT><FONT FACE="Courier">'Camel'</FONT></TT> and <TT><FONT FACE="Courier">'Llama'</FONT></TT>,

respectively.

<H3><A NAME="CallingSubroutinesWhenaButtonIsPre">Calling Subroutines

When a Button Is Pressed</A></H3>

<P>

You can add subroutines to call when a button is pressed. The

examples in Listing 17.1 and 17.5 are two examples of this. You

may specify the <TT><FONT FACE="Courier">-command</FONT></TT>

option in the call to <TT><FONT FACE="Courier">create</FONT></TT>

and <TT><FONT FACE="Courier">pack</FONT></TT> the button (in Listing

⌨️ 快捷键说明

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