210.html
来自「Python Ebook Python&XML」· HTML 代码 · 共 489 行 · 第 1/2 页
HTML
489 行
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
<META NAME="Robots" content="INDEX,NOFOLLOW">
<META HTTP-EQUIV="Pragma" CONTENT="no-cache">
<TITLE>Safari | Python Developer's Handbook -> Geometry Management</TITLE>
<LINK REL="stylesheet" HREF="oreillyi/oreillyN.css">
</HEAD>
<BODY bgcolor="white" text="black" link="#990000" vlink="#990000" alink="#990000" leftmargin="0" topmargin="0" marginwidth="0" marginheight="0">
<table width="100%" cellpadding=5 cellspacing=0 border=0 class="navtopbg"><tr><td><font size="1"><p class="navtitle"><a href="8.html" class="navtitle">Web Development</a> > <a href="0672319942.html" class="navtitle">Python Developer's Handbook</a> > <a href="206.html" class="navtitle">15. Tkinter</a> > <span class="nonavtitle">Geometry Management</span></p></font></td><td align="right" valign="top" nowrap><font size="1"><a href="main.asp?list" class="safnavoff">See All Titles</a></font></td></tr></table>
<TABLE width=100% bgcolor=white border=0 cellspacing=0 cellpadding=5><TR><TD>
<TABLE border=0 width="100%" cellspacing=0 cellpadding=0><TR><td align=left width="15%" class="headingsubbarbg"><a href="209.html" title="Tkinter"><font size="1">< BACK</font></a></td><td align=center width="70%" class="headingsubbarbg"><font size="1"><a href="popanote.asp?pubui=oreilly&bookname=0672319942&snode=210" target="_blank" title="Make a public or private annnotation">Make Note</a> | <a href="210.html" title="Use a Safari bookmark to remember this section">Bookmark</a></font></td><td align=right width="15%" class="headingsubbarbg"><a href="211.html" title="Handling Tkinter Events"><font size="1">CONTINUE ></font></a></td></TR></TABLE>
<a href="5%2F31%2F2002+4%3A48%3A45+PM.html" TABINDEX="-1"><img src=images/spacer.gif border=0 width=1 height=1></a><font color=white size=1>152015024128143245168232148039199167010047123209178152124239215162148036103063008216041062</font><a href="read7.asp?bookname=0672319942&snode=210&now=5%2F31%2F2002+4%3A48%3A45+PM" TABINDEX="-1"><img src=images/spacer.gif border=0 width=1 height=1></a><br>
<FONT>
<h3>
Geometry Management</h3>
<p>All Tkinter widgets have access to specific <i>geometry management</I> methods, which have the purpose of organizing widgets throughout the parent widget area. These methods are grouped in three distinct classes that provide a nice way to lay out child widgets in their parent widget. Tkinter exposes the following geometry manager classes: <TT Class="monofont">pack,</TT>
<TT clasS="monofont">grid,</TT> and <Tt class="monofont">place.</tt>
</p>
<ul>
<li>
<a name="idx1073748398"></a>
<a NamE="idx1073748399"></a>
<p><tT clasS="monofont">pack</tt>棤
This geometry manager organizes widgets in blocks before placing them in the parent widget.
</p>
</LI>
<LI>
<a namE="idx1073748400"></A>
<A Name="idx1073748401"></a>
<P><TT Class="monofont">grid</TT>棤
This geometry manager organizes widgets in a table-like structure in the parent widget.
</P>
</Li>
<li>
<a name="idx1073748402"></a>
<a name="idx1073748403"></a>
<p><tt clAss="monofont">place</Tt>棤
This geometry manager organizes widgets by placing them in a specific position in the parent widget.
</p>
</lI>
</ul>
<p>Each one of these geometry managers has a specific purpose. The <tT claSS="monofont">pack</TT> manager, for example, is convenient for application windows'design. On the other hand, the <tt clASS="monofont">grid</Tt> manager is perfect for designing dialogs because you can easily arrange the position of several widgets using an easy-to-figure-out table structure, behind the scenes. And last, but not least, we have the <tt cLASS="monofont">place</tt> manager. This manager is perfect for placing a widget in a specific position in a frame or window. However, it is not that useful to design complex structures because it requires a lot of specific information about the coordinates of the widget.</p>
<p>The usage of these methods is very simple. When you create a widget, such as</P>
<PRE>
b = Button(root, text="Quit", padx=5, justify=CENTER)
</pre>
<p>You can apply the geometry method directly on the created object.</p>
<pre>
b.pack(side=RIGHT)
</pre>
<p>Or, if you do not want to create one more object instance, you can simply call the geometry method directly from the creation line:</p>
<pre>
Button(root, text="Quit", padx=5, justify=CENTER).pack(side=RIGHT)
</pre>
<h4>
<Tt cLass="monofont">
<A name="idx1073748404"></A>
<a naME="idx1073748405"></A>pack()</Tt>
</h4>
<p>The <tT CLAss="monofont">pack</tt> manager adds the widgets to the frame or the window based in the order that the widgets are packed. After creating a Frame widget, you can start adding widgets to it (the area where the Frame stores a widget is called a <A NAMe="idx1073748406"></a>
<i>parcel</i>). If you want to place a group of widgets next to each other, you can use the same <A NAMe="idx1073748407"></a>
<a name="idx1073748408"></a>
<i>anchor</i> option for all of them. Therefore, they will be stored in the same parcel of the parent frame. If you don't specify any option, the widgets are added from top to bottom in the available spaces. Additionally, you can specify the frame <i>side</i> where you want to place the widget. The final widget position is based on the size of the parent frame as well as on the position of the other widgets already placed. Note that if you use Frames, you will spend much less time designing your interface.</p>
<p>The <tt clasS="monofont">pack</tt> method provides the following options, which can be informed as direct assignments or as a dictionary variable:<A namE="idx1073748409"></a>
<a naMe="idx1073748410"></a>
<a NAME="idx1073748411"></a>
<a naME="idx1073748412"></A>
<A name="idx1073748413"></A>
<A NAme="idx1073748414"></a>
<a NAME="idx1073748415"></a>
<a name="idx1073748416"></a>
<a name="idx1073748417"></a>
<a name="idx1073748418"></a>
<a NamE="idx1073748419"></a>
</p>
<bLockqUote>
<P>
<A NAme="idx1073748420"></a>
<a NAME="idx1073748421"></a>
<p><tt CLASs="monofont">expand</tt>棤
This option expands the widget to use all the remaining space after the other widgets have been informed. This is an important attribute to set so that the correct widgets use the extra space when the window is resized.
</p>
</P>
<P>
<A Name="idx1073748422"></a>
<a name="idx1073748423"></a>
<p><tt class="monofont">fill</tt>棤
This option defines how the widget should fill up the space provided by its parcel. Possible values: <Tt cLass="monofont">x,</Tt>
<tt cLass="monofont">y,</TT>
<TT clasS="monofont">both,</TT> and <Tt claSS="monofont">none.</TT>
</p>
</p>
<p>
<a NAME="idx1073748424"></a>
<a name="idx1073748425"></a>
<p><tt class="monofont">ipadx,</tt>
<tt cLasS="monofont">ipady</tt>棤
These options are used along with the <tT clasS="monofont">fill</tt> option to define the space, in pixels, around the widget.
</p>
</P>
<P>
<A Name="idx1073748426"></a>
<A NAMe="idx1073748427"></a>
<p><tT CLAss="monofont">padx,</tt>
<TT CLass="monofont">pady</tt>棤
These options define the space, in pixels, between widgets.
</p>
</p>
<p>
<a name="idx1073748428"></a>
<a name="idx1073748429"></a>
<P><tt ClasS="monofont">side</tt>棤
This option defines the side where we want to place the widget. Possible values: <tt ClasS="monofont">top,</TT>
<Tt claSS="monofont">bottom,</TT>
<tt clASS="monofont">left,</Tt> and <tt cLASS="monofont">right.</tt>
<a name="idx1073748430"></a>
</p>
</p>
</blockquote>
<P>The following lines demonstrate how you can use these options when packing a widget:</p>
<pRe>
topframe = Frame(root, relief=RAISED, borderwidth=2)
topframe.pack(side=TOP, fill=BOTH)
rightframe = Frame(root, relief=RAISED, borderwidth=2)
rightframe.pack(side=RIGHT, fill=BOTH, expand=1, padx=2, pady=2)
</prE>
<p>The default behavior is to measure the sizes in pixels, but if you prefer to use other measurement units, you just need to add a special suffix to each specific measured value. The possible values are <tt cLass="monofont">c</TT> (for onscreen centimeters), <TT clasS="monofont">m</TT> (for onscreen millimeters), <Tt claSS="monofont">i</TT> (for onscreen inches), and <tt clASS="monofont">p</Tt> (for printer's points梟ote that 1 printer point is equivalent to 1/72 inches). Check out the next line of code to understand how to use this feature. Note that the measurements are in centimeters.</p>
<pre>
rightframe.pack(side=RIGHT, fill=BOTH, expand=1, padx=4c, pady=3c)
</pre>
<h4>
<tt class="monofont">
<a namE="idx1073748431"></a>
<a Name="idx1073748432"></A>grid()</tt>
</h4>
<p>The grid geometry manager is very flexible, which makes the task of designing dialogs very simple. It creates a grid pattern in the frame and allocates a space in each cell to hold a widget. To use it, you just need to inform the row and the column where you want to insert the widget. In order to use the <Tt clASS="monofont">pack</Tt> method to collect results similar to the ones provided by this grid functionality, you would have to use a lot of frame widgets. Actually, it is quite often impossible to get the same configuration with <tt cLASS="monofont">pack()</tt> as you do with <tt CLASs="monofont">grid().</tt> You won't necessarily get all the column/row boundaries to match with <tT CLAss="monofont">pack().</tt>
</p>
<p>As an example of the <tt class="monofont">grid</tt> method, consider the dialog at <a href="210#3.html">Figure 15.3</A>.</p>
<cEnteR>
<h5>
<a naMe="3"></a>Figure 15.3. Window organized with the grid method.</h5>
<IMG BordeR="0" WIDth="117" heIGHT="175" src="graphics/15fig03.gif" aLT="graphics/15fig03.gif">
</CEnter>
<p>Now, check out the code that works behind the scenes to organize the widgets:</p>
<pre>
from Tkinter import *
root = Tk()
Label(root, text="Last Name:").grid(row=0, sticky=W)
Label(root, text="First Name:").grid(row=1, sticky=W)
Label(root, text="Phone #:").grid(row=2, sticky=W)
Label(root, text="email:").grid(row=3, sticky=W)
entry_ln = Entry(root)
entry_fn = Entry(root)
entry_ph = Entry(root)
entry_em = Entry(root)
entry_ln.grid(row=0, column=1)
entry_fn.grid(row=1, column=1)
entry_ph.grid(row=2, column=1)
entry_em.grid(row=3, column=1)
Label(root).grid(row=4, sticky=W)
Label(root, text="Skill set summary:").grid(row=5, sticky=W)
cb_gender = Checkbutton(root, text="Python")
cb_gender.grid(row=6, sticky=W)
cb_gender = Checkbutton(root, text="Perl")
cb_gender.grid(row=7, sticky=W)
b_apply = Button(root, text="Apply")
b_apply.grid(row=7, column = 1)
root.mainloop()
</pre>
<p>Note that we have to call the <tt class="monofont">grid</Tt> method for every single widget, always mentioning the row and the column where we want to place it. If for some reason, we don't use the <tT claSs="monofont">grid</tt> method for a widget, which is placed just after a group of gridded widgets, this new widget is placed in the next available position beneath the gridded widgets.</p>
<P>For your information, the coordinate numbered (<tt cLASS="monofont">0,0</tt>) is given to the intersection of the first row on the top with the first column on the left side.</p>
<p>This method implements the following <A NAMe="idx1073748433"></a>options:</p>
<bLOCKquotE>
<P>
<A Name="idx1073748434"></a>
<a name="idx1073748435"></a>
<p><tt class="monofont">row</tt>棤
The number of the row where we want to place the widget.
</P>
</p>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?