📄 ch17.htm
字号:
17 Income
for an activity <BR>
that
is not a passive activity - IRS Form 8583);<BR>
18 foreach (@items) {<BR>
19 $w_list->insert("end",
$_);<BR>
20 }<BR>
21 #<BR>
22 # Create the scrollbar<BR>
23 #<BR>
24 $w_scroll = $main->Scrollbar(-command => ['yview', $w_list]
<BR>
25 )->pack(-side
=> 'right', -fill => 'y');<BR>
26 #<BR>
27 # Now tie the scrollbar to listbox.<BR>
28 #<BR>
29 $w_list->configure( -yscrollcommand => ['set', $w_scroll]);
<BR>
30 #<BR>
31 # show the listbox.<BR>
32 #<BR>
33 $w_list->pack(-fill => 'y');<BR>
34 MainLoop;</FONT></TT>
</BLOCKQUOTE>
<HR>
<P>
Scrollbars are commonly used to update the items shown in <TT><FONT FACE="Courier">Listbox</FONT></TT>,
<TT><FONT FACE="Courier">Canvas</FONT></TT>, or <TT><FONT FACE="Courier">Text</FONT></TT>
widgets when the slider of the scrollbar is moved by the user.
In Listing 17.9 (line 24), the scrollbar is created with the <TT><FONT FACE="Courier">yview</FONT></TT>
option. For a horizontal scrollbar, use the <TT><FONT FACE="Courier">xview</FONT></TT>
option. In line 29, the listbox is tied to the scrollbar to perform
the scrolling action in the <I>y</I> direction.
<P>
Note also how the <TT><FONT FACE="Courier">label</FONT></TT>,
<TT><FONT FACE="Courier">Listbox</FONT></TT>, and <TT><FONT FACE="Courier">Scrollbar</FONT></TT>
are packed in the window. Had the <TT><FONT FACE="Courier">-fill</FONT></TT>
option not been used, the widgets would not be shown in the entire
height or width of the window.
<P>
To allow more than one listbox to contain a "selection"
(or at least a highlighted item), specify this configuration option:
<BLOCKQUOTE>
<TT><FONT FACE="Courier">-exportselection => 0</FONT></TT>
</BLOCKQUOTE>
<H3><A NAME="UsingTextWidgets">Using <TT><FONT SIZE=4 FACE="Courier">Text</FONT></TT><FONT SIZE=4>
Widgets</FONT></A></H3>
<P>
The <TT><FONT FACE="Courier">Text</FONT></TT> widget is simply
a widget that enables user entry. The default way to create a
<TT><FONT FACE="Courier">Text</FONT></TT> widget is shown here:
<BLOCKQUOTE>
<TT><FONT FACE="Courier">#!/usr/bin/perl -w<BR>
use Tk;<BR>
<BR>
my $mw = MainWindow->new();<BR>
$txt = $mw->Text(-width => '80', -height => '100') ->
pack;<BR>
MainLoop;</FONT></TT>
</BLOCKQUOTE>
<P>
Note that the default size of a <TT><FONT FACE="Courier">Text</FONT></TT>
widget is very big for most screens. You might want to limit the
size with the <TT><FONT FACE="Courier">-height</FONT></TT> and
<TT><FONT FACE="Courier">-width</FONT></TT> options to set the
same size. The values are given in the number of characters, not
the number of pixels.
<P>
Using the code in the <TT><FONT FACE="Courier">Scrollbar</FONT></TT>
and <TT><FONT FACE="Courier">Listbox</FONT></TT> examples, you
can attach scrollbars to create fairly sophisticated editors very
quickly using <TT><FONT FACE="Courier">Perl/Tk</FONT></TT>.
<H3><A NAME="SpecifyingFontsforTextandOtherWidg">Specifying Fonts
for Text and Other Widgets</A></H3>
<P>
To specify the font configuration option of your widget, use the
<TT><FONT FACE="Courier">-font</FONT></TT> option. See the following
example:
<BLOCKQUOTE>
<TT><FONT FACE="Courier">my $mw = MainWindow->new();<BR>
$txt = $mw->Text(-width => '40', -height => '20', -font
=> 'fixed') -> pack;</FONT></TT>
</BLOCKQUOTE>
<P>
The <TT><FONT FACE="Courier">'fixed'</FONT></TT> value is the
name of the font. You can check your <TT><FONT FACE="Courier">fonts.alias</FONT></TT>
file for more font names to use or look at the output from the
<TT><FONT FACE="Courier">xlsfont</FONT></TT> command.
<H3><A NAME="TextEntryWidgets"><TT><FONT SIZE=4 FACE="Courier">Text
Entry</FONT></TT><FONT SIZE=4> Widgets</FONT></A></H3>
<P>
You want to call the <TT><FONT FACE="Courier">get()</FONT></TT>
function on the return value of the widget itself. Here is how
it may be used in a simplified version of Example 1.1 from the
<TT><FONT FACE="Courier">Tk::UserGuide</FONT></TT>, where a <TT><FONT FACE="Courier">Button</FONT></TT>
is set up to call a sub where the call to <TT><FONT FACE="Courier">get</FONT></TT>
lies. Check the POD file, UserGuide.pod, in the distribution for
more information. The output is shown in Figure 17.10.<P>
<A HREF="f17-10.gif" tppabs="http://www.mcp.com/815097600/0-672/0-672-30891-6/f17-10.gif"><B>Figure 17.10 :</B><I>Using the <FONT FACE="Courier"> Text Entry </FONT> widget.</I></A>
<HR>
<BLOCKQUOTE>
<B>Listing 17.10. Using the </B><TT><B><FONT FACE="Courier">Text
Entry</FONT></B></TT><B> widget.<BR>
</B>
</BLOCKQUOTE>
<BLOCKQUOTE>
<TT><FONT FACE="Courier"> 1 #!/usr/bin/perl -w<BR>
2 <BR>
3 use Tk;<BR>
4 <BR>
5 my $mw = MainWindow -> new();
<BR>
6 my $entry = $mw -> Entry();
<BR>
7 $entry -> pack;<BR>
8 <BR>
9 $txt = $mw->Text(-width =>
'10', -height => '10')->pack;<BR>
10 <BR>
11 $mw->Button(-text => 'Dino',
<BR>
12
-command => sub{Echo($entry, $txt)}<BR>
13
)->pack;<BR>
14 MainLoop;<BR>
15 <BR>
16 sub Echo {<BR>
17 my ($widget,
$txt) = @_;<BR>
18 <BR>
19
#<BR>
20
# Show the values of the Text Entry widget<BR>
21
#<BR>
22 my $entered
= $widget -> get(); # Get the input<BR>
23 print
"The string \"$entered\" was entered.\n";
<BR>
24 <BR>
25
#<BR>
26
# Show the values of the Text widget<BR>
27
#<BR>
28 my $text
= $txt ->Contents(); # Get the input<BR>
29 print
"The Text \"$text\" was entered.\n";<BR>
30 }</FONT></TT>
</BLOCKQUOTE>
<HR>
<P>
For collecting passwords, set the value of the <TT><FONT FACE="Courier">-show</FONT></TT>
option to zero:
<BLOCKQUOTE>
<TT><FONT FACE="Courier">(-show => 0);</FONT></TT>
</BLOCKQUOTE>
<H2><A NAME="HandlingtheKeyboardwithKeyBindings"><FONT SIZE=5 COLOR=#FF0000>Handling
the Keyboard with Key Bindings</FONT></A></H2>
<P>
Using the pointer interface is not the only way to collect input
in your <TT><FONT FACE="Courier">Perl/Tk</FONT></TT> programs.
There are many default key bindings built into the widgets of
<TT><FONT FACE="Courier">Perl/Tk</FONT></TT>. Making proper use
of them often involves setting up the right callback. Read the
documentation in <TT><FONT FACE="Courier">BindTable.pod</FONT></TT>
in the <TT><FONT FACE="Courier">Tk</FONT></TT> package for more
detailed help with this subject.
<P>
The way to bind a key to a widget is to use
<BLOCKQUOTE>
<TT><FONT FACE="Courier">$widget -> bind('<keyname>'
=> action);</FONT></TT>
</BLOCKQUOTE>
<P>
where <TT><FONT FACE="Courier">$widget</FONT></TT> is the object
to which the keys are bound. For global bindings you have to bind
to <TT><FONT FACE="Courier"><All></FONT></TT>. For specific
bindings you need to bind to each widget.
<P>
Use the following script on each <TT><FONT FACE="Courier">.pm</FONT></TT>
file for which you want to find key bindings:
<BLOCKQUOTE>
<TT><FONT FACE="Courier">#!/usr/bin/perl<BR>
while (<>) {<BR>
print
if s/.*(<[^>]*>).*/$1/g;<BR>
}</FONT></TT>
</BLOCKQUOTE>
<P>
When run on the <TT><FONT FACE="Courier">Listbox.pm</FONT></TT>
file, this script reveals a lot of key bindings. Some modules
even show <TT><FONT FACE="Courier"><HANDLE></FONT></TT>
and <TT><FONT FACE="Courier"><ARGV></FONT></TT>, so you
have to know what to ignore. Also, bound keys are inherited, so
listing one module's bindings may not be complete if any properties
are inherited from other sources. The output from the <TT><FONT FACE="Courier">Listbox.pm</FONT></TT>
file is as follows:
<BLOCKQUOTE>
<TT><FONT FACE="Courier"><1><BR>
<B1-Motion><BR>
<ButtonRelease-1><BR>
<Shift-1><BR>
<Control-1><BR>
<B1-Leave><BR>
<B1-Enter><BR>
<Up><BR>
<Shift-Up><BR>
<Down><BR>
<Shift-Down><BR>
<Control-Home><BR>
<Shift-Control-Home><BR>
<Control-End><BR>
<Shift-Control-End><BR>
<space><BR>
<Select><BR>
<Control-Shift-space><BR>
<Shift-Select><BR>
<Escape><BR>
<Control-slash><BR>
<Control-backslash><BR>
<2><BR>
<B2-Motion><BR>
<P>
Note the <1> and <2> in the output.<BR>
</FONT></TT>
</BLOCKQUOTE>
<CENTER>
<TABLE BORDERCOLOR=#000000 BORDER=1 WIDTH=80%>
<TR VALIGN=TOP><TD ><B>Tip</B></TD></TR>
<TR VALIGN=TOP><TD >
<BLOCKQUOTE>
Do not use the <TT><FONT FACE="Courier">%k</FONT></TT> symbols for <TT><FONT FACE="Courier">Tcl/Tk</FONT></TT> in Perl scripts. The <TT><FONT FACE="Courier">%k</FONT></TT> symbols will be misinterpreted as nonexistent Perl hashes.
</BLOCKQUOTE>
</TD></TR>
</TABLE></CENTER>
<P>
<P>
Listing 17.11 illustrates how to read the button and key bindings
from <TT><FONT FACE="Courier">Xevents</FONT></TT>.
<HR>
<BLOCKQUOTE>
<B>Listing 17.11. A sample key bindings display program.<BR>
</B>
</BLOCKQUOTE>
<BLOCKQUOTE>
<TT><FONT FACE="Courier"> 1 #!/usr/bin/perl -w<BR>
2 use Tk;<BR>
3 <BR>
4 $mw = MainWindow->new();<BR>
5 $frame = $mw->Frame( -height
=> '6c', -width => '6c',<BR>
6 -background
=> 'black');<BR>
7 $frame->pack; #
show the frame<BR>
8 $mw->bind( '<Any-KeyPress>'
=> \&echo); # for all keys<BR>
9 $mw->bind( '<ButtonPress>'
=> \&echoPress); # for all keys<BR>
10 $mw->bind( '<ButtonRelease>'
=> \&echoRel); # for all keys<BR>
11 <BR>
12  
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -