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

📄 ch21_02.htm

📁 by Randal L. Schwartz and Tom Phoenix ISBN 0-596-00132-0 Third Edition, published July 2001. (See
💻 HTM
📖 第 1 页 / 共 3 页
字号:
<html><head><title>Geometry Managers (Perl in a Nutshell, 2nd Edition)</title><link rel="stylesheet" type="text/css" href="../style/style1.css" /><meta name="DC.Creator" content="Stephen Spainhour" /><meta name="DC.Format" content="text/xml" scheme="MIME" /><meta name="DC.Language" content="en-US" /><meta name="DC.Publisher" content="O'Reilly &amp; Associates, Inc." /><meta name="DC.Source" scheme="ISBN" content="0596002416L" /><meta name="DC.Subject.Keyword" content="stuff" /><meta name="DC.Title" content="Perl in a Nutshell, 2nd Edition" /><meta name="DC.Type" content="Text.Monograph" /></head><body bgcolor="#ffffff"><img src="gifs/smbanner.gif" usemap="#banner-map" border="0" alt="Book Home" /><map name="banner-map"><area shape="rect" coords="1,-2,616,66" href="index.htm" alt="Java and XSLT" /><area shape="rect" coords="629,-11,726,25" href="jobjects/fsearch.htm" alt="Search this book" /></map><div class="navbar"><table width="684" border="0"><tr><td align="left" valign="top" width="228"><a href="ch21_01.htm"><img src="../gifs/txtpreva.gif" alt="Previous" border="0" /></a></td><td align="center" valign="top" width="228" /><td align="right" valign="top" width="228"><a href="ch21_03.htm"><img src="../gifs/txtnexta.gif" alt="Next" border="0" /></a></td></tr></table></div><h2 class="sect1">21.2. Geometry Managers</h2><p><a name="INDEX-2701" /><a name="INDEX-2702" /><a name="INDEX-2703" /><a name="INDEX-2704" />Creating widgets and determining howto display them are done with separate commands. You can create awidget with one of the widget creation methods (such as<tt class="literal">Button</tt>, <tt class="literal">Canvas</tt>, etc.), but youdisplay them using a geometry manager. The three geometry managersare <tt class="literal">pack</tt>, <tt class="literal">grid</tt>,<tt class="literal">place</tt>, and <tt class="literal">form</tt>.<tt class="literal">pack</tt> is by far the most commonly used.</p><p>You can either pack a widget as you create it or create the widgetobject and pack it separately. For example, the previous"Hello World" example might haveread:</p><blockquote><pre class="code">#!/usr/bin/perl -wuse Tk;my $mw = MainWindow-&gt;new;$button = $mw-&gt;Button(-text =&gt; "Hello World!", -command =&gt;sub{exit});$button-&gt;pack;MainLoop;</pre></blockquote><a name="perlnut2-CHP-21-SECT-2.1" /><div class="sect2"><h3 class="sect2">21.2.1. The pack Geometry Manager</h3><p><a name="INDEX-2705" />With the<tt class="literal">pack</tt> geometry manager, widgets cannot overlap orcover each other, either partially or completely. Once a widget ispacked into a window, the next widget is packed in the remainingspace around it. <tt class="literal">pack</tt> sets up an"allocation rectangle" for eachwidget, determined by the dimensions of the parent window and thepositioning of the widgets already packed into it. This means thatthe order in which you pack your widgets is very important.</p><p>By default, <tt class="literal">pack</tt> places widgets at the top centerof the allocation rectangle. However, you can use options to<tt class="literal">pack</tt> to control where a widget is placed and howmuch padding is placed around it. Options for <tt class="literal">pack</tt>are:</p><dl><dt><b><tt class="literal">-side =&gt;</tt> <em class="replaceable">side</em></b></dt><dd><a name="INDEX-2706" />Puts the widget against the specifiedside of the window. Values for <em class="replaceable"><tt>side</tt></em> are<tt class="literal">left</tt>, <tt class="literal">right</tt>,<tt class="literal">top</tt>, and <tt class="literal">bottom</tt>. The default is<tt class="literal">top</tt>.</p></dd><dt><b><tt class="literal">-fill =&gt;</tt> <em class="replaceable">direction</em></b></dt><dd><a name="INDEX-2707" />Causes the widget to fill theallocation rectangle in the specified direction. Values for<em class="replaceable"><tt>direction</tt></em> are <tt class="literal">none</tt>,<tt class="literal">x</tt>, <tt class="literal">y</tt>, and<tt class="literal">both</tt>. The default is <tt class="literal">none</tt>.</p></dd><dt><b><tt class="literal">-expand =&gt;</tt> <em class="replaceable">boolean</em></b></dt><dd><a name="INDEX-2708" />Causes the allocation rectangle to fillthe remaining space available in the window. Values are<tt class="literal">yes</tt>, <tt class="literal">no</tt>, <tt class="literal">1</tt>,and <tt class="literal">0</tt>. The default is <tt class="literal">0</tt>(<tt class="literal">no</tt>).</p></dd><dt><b><tt class="literal">-anchor =&gt;</tt> <em class="replaceable">position</em></b></dt><dd><a name="INDEX-2709" />Anchors the widget inside theallocation rectangle. Values for <em class="replaceable"><tt>position</tt></em>are <tt class="literal">n</tt>, <tt class="literal">ne</tt>,<tt class="literal">e</tt>, <tt class="literal">se</tt>, <tt class="literal">s</tt>,<tt class="literal">sw</tt>, <tt class="literal">w</tt>, <tt class="literal">nw</tt>,and <tt class="literal">center</tt>. The default is<tt class="literal">center</tt>.</p></dd><dt><b><tt class="literal">-after =&gt;</tt> <tt class="literal">$</tt><em class="replaceable">widget</em></b></dt><dd><a name="INDEX-2710" />Puts the widget after another widget inpacking order.</p></dd><dt><b><tt class="literal">-before =&gt;</tt> <tt class="literal">$</tt><em class="replaceable">widget</em></b></dt><dd><a name="INDEX-2711" />Puts the widget before another widgetin packing order.</p></dd><dt><b><tt class="literal">-in =&gt;</tt> <tt class="literal">$</tt><em class="replaceable">window</em></b></dt><dd><a name="INDEX-2712" />Packs the widget inside another windowrather than inside its parent.</p></dd><dt><b><tt class="literal">-ipadx =&gt;</tt> <em class="replaceable">amount</em></b></dt><dd><a name="INDEX-2713" />Increases the size of the widgethorizontally by <em class="replaceable"><tt>amount</tt></em> x 2.<em class="replaceable"><tt>amount</tt></em> can be represented as a numberfollowed by <tt class="literal">c</tt> (centimeters), <tt class="literal">i</tt>(inches), <tt class="literal">m</tt> (millimeters), and<tt class="literal">p</tt> (printer points). Pixels are the default units.</p></dd><dt><b><tt class="literal">-ipady =&gt;</tt> <em class="replaceable">amount</em></b></dt><dd>Increases the size of the widget vertically by<em class="replaceable"><tt>amount</tt></em> x 2.<tt class="literal">amount</tt> can be represented as a number followed by<tt class="literal">c</tt> (centimeters), <tt class="literal">i</tt> (inches),<tt class="literal">m</tt> (millimeters), and <tt class="literal">p</tt> (printerpoints). Pixels are the default units.</p></dd><dt><b><tt class="literal">-padx =&gt;</tt> <em class="replaceable">amount</em></b></dt><dd><a name="INDEX-2714" />Places padding on the left and rightof the widget. <em class="replaceable"><tt>amount</tt></em> can be representedas a number followed by <tt class="literal">c</tt> (centimeters),<tt class="literal">i</tt> (inches), <tt class="literal">m</tt> (millimeters),and <tt class="literal">p</tt> (printer points). Pixels are the defaultunits.</p></dd><dt><b><tt class="literal">-pady</tt> <em class="replaceable">amount</em></b></dt><dd>Places padding on the top and bottom of the widget.<em class="replaceable"><tt>amount</tt></em> can be represented as a numberfollowed by <tt class="literal">c</tt> (centimeters), <tt class="literal">i</tt>(inches), <tt class="literal">m</tt> (millimeters), and<tt class="literal">p</tt> (printer points). Pixels are the default units.</p></dd></dl><a name="perlnut2-CHP-21-SECT-2.1.1" /><div class="sect3"><h3 class="sect3">21.2.1.1. pack methods</h3><p>The following methods are associated with widgets managed with<tt class="literal">pack</tt>:</p><dl><dt><b><tt class="literal">packForget</tt></b></dt><dd><a name="INDEX-2715" />Causes a widget to be removed fromview:</p><blockquote><pre class="code">$widget-&gt;packForget;</pre></blockquote><p>The widget is not destroyed, but it is no longer managed by<tt class="literal">pack</tt>. The widget is removed from the packingorder, so if it was repacked later, it would appear at the end of thepacking order.</p></dd><dt><b><tt class="literal">packInfo</tt></b></dt><dd><a name="INDEX-2716" />Returns a list containing all packinformation about that widget:</p><blockquote><pre class="code">$info = $widget-&gt;packInfo;</pre></blockquote></dd><dt><b><tt class="literal">packPropagate</tt></b></dt><dd><a name="INDEX-2717" />Enables or suppresses automaticresizing of a Toplevel or Frame widget to accommodate items packedinside of it. Automatic resizing is on by default; the following lineturns off automatic resizing:</p><blockquote><pre class="code">$widget-&gt;packPropagate(0);</pre></blockquote></dd><dt><b><tt class="literal">packSlaves</tt></b></dt><dd><a name="INDEX-2718" />Returns an ordered list of all thewidgets packed into the parent widget:</p><blockquote><pre class="code">$children = $widget-&gt;packSlaves;</pre></blockquote></dd></dl></div><a name="INDEX-2719" /></div><a name="perlnut2-CHP-21-SECT-2.2" /><div class="sect2"><h3 class="sect2">21.2.2. The grid Geometry Manager</h3><p><a name="INDEX-2720" />The<tt class="literal">grid</tt> geometry manager divides the window into agrid composed of columns and rows starting at 0,0 in the upperlefthand corner. The resulting grid resembles a spreadsheet, witheach widget assigned a cell according to the options to<tt class="literal">grid</tt>. To create a grid, create a frame packedinside the parent window and then grid the widgets within the frame.</p><p>You can specify explicit rows and columns using options to<tt class="literal">grid</tt>. However, if several widgets are meant toappear in the same row, you can use a single <tt class="literal">grid</tt>command with a list of widgets rather than calling<tt class="literal">grid</tt> for each one. The first widget invokes the<tt class="literal">grid</tt> command, and all other widgets for thatcolumn are specified as options to <tt class="literal">grid</tt>. Anysubsequent <tt class="literal">grid</tt> commands increment the row by oneand start again.</p><p>You can use special characters as placeholders:</p><dl><dt><b><tt class="literal">-</tt> (minus sign)</b></dt><dd><a name="INDEX-2721" /><a name="INDEX-2722" />The previous widget should spanthis column as well. May not follow <tt class="literal">^</tt> or<tt class="literal">x</tt>.</p></dd><dt><b><tt class="literal">x</tt></b></dt><dd><a name="INDEX-2723" />Leaves a blank space.</p></dd><dt><b><tt class="literal">^</tt></b></dt><dd><a name="INDEX-2724" /><a name="INDEX-2725" />The widget above this one (samecolumn, previous row) should span this row.</p></dd></dl><p>Options to <tt class="literal">grid</tt> are:</p><dl><dt><b><tt class="literal">-column =&gt;</tt> <em class="replaceable">n</em></b></dt><dd><a name="INDEX-2726" />Thecolumn in which to place the widget. <em class="replaceable"><tt>n</tt></em> isany integer &gt;= 0.</p></dd><dt><b><tt class="literal">-row =&gt;</tt> <em class="replaceable">m</em></b></dt><dd><a name="INDEX-2727" />The row in which to placethe widget. <em class="replaceable"><tt>m</tt></em> is any integer &gt;= 0.</p></dd><dt><b><tt class="literal">-columnspan =&gt;</tt> <em class="replaceable">n</em></b></dt><dd><a name="INDEX-2728" />Thenumber of columns for the widget to span, beginning with the columnspecified with <tt class="literal">-column</tt>.<em class="replaceable"><tt>n</tt></em> is any integer &gt; 0.</p></dd><dt><b><tt class="literal">-rowspan =&gt;</tt> <em class="replaceable">m</em></b></dt><dd><a name="INDEX-2729" />Thenumber of rows for the widget to span, beginning with the rowspecified with <tt class="literal">-row</tt>. <em class="replaceable"><tt>m</tt></em>is any integer &gt; 0.</p></dd><dt><b><tt class="literal">-sticky =&gt;</tt> <em class="replaceable">sides</em></b></dt><dd><a name="INDEX-2730" />Sticks widget to specifiedside(s). <em class="replaceable"><tt>sides</tt></em> contains characters<tt class="literal">n</tt>, <tt class="literal">s</tt>, <tt class="literal">e</tt>, or<tt class="literal">w</tt>.

⌨️ 快捷键说明

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