📄 ch17.htm
字号:
8 my %hello = ('-text' , 'Hello, world',<BR>
<FONT FACE="ZAPFDINGBATS">Â</FONT>'-command' , sub {print
STDOUT "Hello, world\n"; });<BR>
9 my %greet = ('-text' , 'Greetings',<BR>
<FONT FACE="ZAPFDINGBATS">Â</FONT>'-command' , sub {print
STDOUT "Greetings\n"; exit;});<BR>
10 $mw->Button(%hello)->pack;<BR>
11 $mw->Button(%greet)->pack('-side', "left");
<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>, "References,"
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 "created" 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>
... <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 "created" 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->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->Button();<BR>
$button->configure(-text => 'Hello!');</FONT></TT>
</BLOCKQUOTE>
<P>
and
<BLOCKQUOTE>
<TT><FONT FACE="Courier">my $button = new $main->Button(-text
=> 'Hello');</FONT></TT>
</BLOCKQUOTE>
<P>
The "Hello, world" 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->title(" A sample window");</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"> 1 #!/usr/bin/perl<BR>
2 <BR>
3 use Tk;<BR>
4 <BR>
5 #
<BR>
6 #
Using Checkbuttons.<BR>
7 #
<BR>
8 <BR>
9 my $main = new MainWindow;<BR>
10 <BR>
11 $main->Checkbutton(-text => 'One' )->pack;
<BR>
12 $main->Checkbutton(-text => 'Two' )->pack;
<BR>
13 $main->Checkbutton(-text => 'Three' )->pack;<BR>
14 <BR>
15 #<BR>
16 # Uncomment these lines to get the buttons to be side by side.
<BR>
17 #<BR>
18 # $main->Checkbutton(-text => 'One')->pack(-side =>
'left');<BR>
19 # $main->Checkbutton(-text => 'Two')->pack(-side =>
'left');<BR>
20 # $main->Checkbutton(-text => 'Three ')->pack(-side
=> '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"> 1 #!/usr/bin/perl<BR>
2<BR>
3 use Tk;<BR>
4 #
<BR>
5 #
Using RadioButtons.<BR>
6 #
<BR>
7 my $main = new MainWindow;<BR>
8 $main->title("Select Delicacy");
<BR>
9 #<BR>
10 # Declare the global variable<BR>
11 #<BR>
12 $animal = 'anything';<BR>
13 $main->Radiobutton(-text => 'Yak', -variable => \$animal,
<BR>
14
-value => "Yak",<BR>
15
-command => sub {print "$animal \n"; } ,<BR>
16 )->pack(-side => 'left');<BR>
17 $main->Radiobutton(-text => 'Camel', -variable =>
\$animal,<BR>
18
-value => "Camel",<BR>
19
-command => sub {print "$animal \n"; } ,<BR>
20 )->pack(-side => 'left');<BR>
21 $main->Radiobutton(-text => 'Llama', -variable =>
\$animal,<BR>
22
-value => "Llama",<BR>
23
-command => sub {print "$animal \n"; }<BR>
24
)->pack(-side => '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 + -