📄 ch21_01.htm
字号:
<a name="perlnut2-CHP-21-SECT-1.1.2" /></a><div class="sect3"><h3 class="sect3">21.1.1.2. The cget method</h3><p><a name="INDEX-2679" /></a>For simply retrieving the value of anoption, <tt class="literal">configure</tt> returns more information thanyou generally want. The <tt class="literal">cget</tt> method returns justthe current value.</p></div></div><a name="perlnut2-CHP-21-SECT-1.2" /></a><div class="sect2"><h3 class="sect2">21.1.2. Scrollbars</h3><p><a name="INDEX-2680" /></a><a name="INDEX-2681" /></a><a name="INDEX-2682" /></a><a name="INDEX-2683" /></a>Manywidgets have scrollbars associated with them. Scrollbars can be addedto a widget in two ways: with an independent Scrollbar widget or withthe <tt class="literal">Scrolled</tt> method when creating a widget. Forsimple scrollbars, the <tt class="literal">Scrolled</tt> method is mucheasier and therefore preferable.</p><a name="perlnut2-CHP-21-SECT-1.2.1" /></a><div class="sect3"><h3 class="sect3">21.1.2.1. Using the Scrolled method</h3><p><a name="INDEX-2684" /></a>Use the <tt class="literal">Scrolled</tt>method to create both the widget and the scrollbar in a singlecommand. For example:</p><blockquote><pre class="code">$mainwindow->Scrolled('Entry', -scrollbars => 'os' -textvariable => \$address)->pack;</pre></blockquote><p>This creates an Entry widget with an"optional" scrollbar on the bottom.The first argument to <tt class="literal">Scrolled</tt> is the type ofwidget (in this case, an Entry widget). Then use the<tt class="literal">-scrollbars</tt> option to list the location of thescrollbar ("s" for the south, orbottom, edge of the widget). Here, we specify an"optional" scrollbar with"o", meaning that the scrollbarwill appear only if needed.</p><p>Any additional options to the <tt class="literal">Scrolled</tt> method aretaken as options to the widget itself. In this case,we're setting the <tt class="literal">-textvariable</tt>option to the Entry widget.</p></div><a name="perlnut2-CHP-21-SECT-1.2.2" /></a><div class="sect3"><h3 class="sect3">21.1.2.2. The Scrollbar widget</h3><p><a name="INDEX-2685" /></a><a name="INDEX-2686" /></a><a name="INDEX-2687" /></a>For more flexibility with a scrollbar,you can use the Scrollbar widget. To do so, you need to create thetarget widget to scroll. Set the <tt class="literal">-xscrollcommand</tt>or <tt class="literal">-yscrollcommand</tt> option as appropriate,configure the scrollbar to talk to the widget, and position thescrollbar and target widget next to one another. For example:</p><blockquote><pre class="code">$scrollbar = $mainwindow->Scrollbar(-orient => 'vertical');$listbox = $mainwindow->Entry(-yscrollcommand => ['set' => $scrollbar]);$scrollbar->configure(-command => ['yview' => $listbox]);$scrollbar->pack(-side => 'right', -fill => 'y');$listbox->pack(-side => 'left', -fill => 'both');</pre></blockquote><p>First, we create the scrollbar with vertical orientation (which isactually the default). Next, we create the Listbox widget with the<tt class="literal">-yscrollcommand</tt> option to define a callback whenthe widget is scrolled vertically. The scrollbar is then configuredwith a callback that says to inform the Listbox widget when it isclicked vertically. Finally, the Scrollbar and Listbox widgets arepacked side-by-side. See further discussion of the Scrollbar widgetlater in this chapter for more information.</p></div></div><a name="perlnut2-CHP-21-SECT-1.3" /></a><div class="sect2"><h3 class="sect2">21.1.3. Callbacks</h3><p><a name="INDEX-2688" /></a><a name="INDEX-2689" /></a><a name="INDEX-2690" /></a><a name="INDEX-2691" /></a>Manywidgets allow you to define a <em class="emphasis">callback</em>, which isa command to execute when the widget is selected. For example, whenyou press an exit button, the callback might be to a routine thatcleans up and quits the program. When you click on a radio button,you might want to change the window to reflect the new preferences.</p><p><a name="INDEX-2692" /></a>Widgets that support callbacks have a<tt class="literal">-command</tt> option to provide the callback function.In the "Hello World" example shownpreviously, the callback is to <tt class="literal">sub {exit}</tt>. In thatexample, the callback is called as an anonymous subroutine. You couldalso use a reference to a subroutine (e.g.,<tt class="literal">\&routine</tt>). If you want to provide argumentsto a subroutine, you can call it as an anonymous list (e.g.,<tt class="literal">[ \&routine, $arg, $arg, ... ]</tt>).</p></div><a name="perlnut2-CHP-21-SECT-1.4" /></a><div class="sect2"><h3 class="sect2">21.1.4. Colors and Fonts</h3><p><a name="INDEX-2693" /></a><a name="INDEX-2694" /></a><a name="INDEX-2695" /></a><a name="INDEX-2696" /></a><a name="INDEX-2697" /></a>Tk was originally created for the XWindow System and is still primarily used in that environment. Forthis reason, it has inherited the font and color scheme used for theX Window System.</p><p>Colors that can be used with Tk widgets are identified either by anRGB value or by a name that has been associated with an RGB value. Ingeneral, it is easier to use a color name than an explicit RGB value.For a listing of the color names that are supported, see the<em class="emphasis">rgb.txt</em> file in your X distribution or use the<em class="emphasis">showrgb</em> command. (Most common color names aresupported, so you can say things such as"red,""pink,""green," and even"chartreuse" with confidence.)</p><p>Fonts are another matter. Under the X Window System, fonts are namedthings such as<em class="emphasis">-adobe-helvetica-medium-o-normal -- 12-120-75-75-p-67-iso8859-1</em>.Wildcards can make the fonts easier to use, butthey're still a mouthful. For a listing of fontsavailable for a particular X server, use the<em class="emphasis">xlsfonts</em> command. There are a few font"aliases" that have been definedfor your convenience (such as <tt class="literal">fixed</tt>,<tt class="literal">6x10</tt>, <tt class="literal">9x15</tt>, etc.), and youmight prefer to just stick to those.<a name="INDEX-2698" /></a><a name="INDEX-2699" /></a><a name="INDEX-2700" /></a> </p></div></div><hr width="684" align="left" /><div class="navbar"><table width="684" border="0"><tr><td align="left" valign="top" width="228"><a href="part9.htm"><img src="../gifs/txtpreva.gif" alt="Previous" border="0" /></a></td><td align="center" valign="top" width="228"><a href="index.htm"><img src="../gifs/txthome.gif" alt="Home" border="0" /></a></td><td align="right" valign="top" width="228"><a href="ch21_02.htm"><img src="../gifs/txtnexta.gif" alt="Next" border="0" /></a></td></tr><tr><td align="left" valign="top" width="228">IX. Perl/Tk</td><td align="center" valign="top" width="228"><a href="index/index.htm"><img src="../gifs/index.gif" alt="Book Index" border="0" /></a></td><td align="right" valign="top" width="228">21.2. Geometry Managers</td></tr></table></div><hr width="684" align="left" /><img src="../gifs/navbar.gif" usemap="#library-map" border="0" alt="Library Navigation Links" /><p><p><font size="-1"><a href="copyrght.htm">Copyright © 2002</a> O'Reilly & Associates. All rights reserved.</font></p><map name="library-map"><area shape="rect" coords="1,0,85,94" href="../index.htm"><area shape="rect" coords="86,1,178,103" href="../lwp/index.htm"><area shape="rect" coords="180,0,265,103" href="../lperl/index.htm"><area shape="rect" coords="267,0,353,105" href="../perlnut/index.htm"><area shape="rect" coords="354,1,446,115" href="../prog/index.htm"><area shape="rect" coords="448,0,526,132" href="../tk/index.htm"><area shape="rect" coords="528,1,615,119" href="../cookbook/index.htm"><area shape="rect" coords="617,0,690,135" href="../pxml/index.htm"> </map></body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -