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

📄 135.html

📁 Tcl 语言的入门级图书
💻 HTML
字号:
<HTML><TITLE>Geometry Management: An Array of Cells</TITLE><BODY BGCOLOR="#FFF0E0" VLINK="#0FBD0F" TEXT="#101000" LINK="#0F0FDD">
<A NAME="top"><H1>An Array of Cells</H1></A>


<P>  The <TT>grid</TT> geometry manager lets you create two dimensional layouts of
widgets.  Think of it as dividing its space into a retangular array of cells &#150; something like Figure 13.5a, but without the lines.  Cells are something
like slices.  Like slices they are associated with widgets.  Like slices they
may be larger than the widgets they are associated with.  This may be because
of another larger widget in the same row or column or it may be because the
user has resized the window.  Cells always expand in both directions.

<P><CENTER><TABLE BORDER>
<CAPTION><ADDR>Figure 13.5a: The concept of grid.</ADDR></CAPTION>
<TR ALIGN=center><TD><IMG SRC="F13x5a.JPG" tppabs="http://www.mapfree.com/sbf/tcl/book/select/Figs/F13x5a.JPG">
</TD></TR>
</TABLE></CENTER><P>

<P> The <TT>grid</TT> analogue for <TT>-fill both</TT> is <TT>-sticky news</TT>.  It causes a
widget to be expanded to fill its entire cell.  The analogue for
<TT>pack</TT>'s default, <TT>-fill none</TT>, is <TT>-sticky {}</TT>.

<P> As mentioned above in 
<A HREF="13.1.html" tppabs="http://www.mapfree.com/sbf/tcl/book/select/Html/13.1.html">The <TT>pack</TT> and <TT>grid</TT> Action Families</A>, widgets are placed under geometry
management by <TT>grid</TT> with the following pattern.

<P><DL>
<P> <DT><PRE>grid <CITE>SLAVES ?OPTIONS?</CITE></PRE><DD>
</DL></P>
The widgets named in <CITE>SLAVES</CITE> should all have the same parent <CITE>P</CITE>.
(For <TT>pack configure</TT> this rule is followed by convention in this book.
For <TT>grid configure</TT> it is enforced by Tk.)  <CITE>P</CITE> will be their master
under grid geometry management.  They will appear in one row in the same order
they are given in <CITE>SLAVES</CITE>.  This row will be a new row.  If the grid
already has a row, the new row will appear at the bottom.

<P>  If the letter "x" appears in <CITE>SLAVES</CITE>, it acts as a placeholder
for a cell that contains no widget.

<P>  Figure 13.5b shows a simple use of <TT>grid</TT> to put three labels in a row.
The script simply creates the labels and then places them with one <TT>grid</TT>
command.  Figure 13.5c shows how to put two entries under these headers.  The
middle column is left vacant.  The <TT>x</TT> argument sees to that.


<P><CENTER><TABLE BORDER>
<CAPTION><ADDR>Figure 13.5b: A three cell grid.</ADDR></CAPTION>
<TR ALIGN=center><TD><IMG SRC="F13x5b.JPG" tppabs="http://www.mapfree.com/sbf/tcl/book/select/Figs/F13x5b.JPG">
</TD></TR>
<TR><TD><PRE>frame .table
label .table.col1header -text "First Trimester" -padx 1m
label .table.col2header -text "Second Trimester" -padx 1m
label .table.col3header -text "Third Trimester" -padx 1m
grid .table.col1header .table.col2header .table.col3header
pack .table </PRE></TD></TR>
</TABLE></CENTER><P>


<P><CENTER><TABLE BORDER>
<CAPTION><ADDR>Figure 13.5c: A six cell grid.</ADDR></CAPTION>
<TR ALIGN=center><TD><IMG SRC="F13x5c.JPG" tppabs="http://www.mapfree.com/sbf/tcl/book/select/Figs/F13x5c.JPG">
</TD></TR>
<TR><TD><PRE>frame .table
label .table.col1header -text "First Trimester" -padx 1m
label .table.col2header -text "Second Trimester" -padx 1m
label .table.col3header -text "Third Trimester" -padx 1m
grid .table.col1header .table.col2header .table.col3header
entry .table.class11 
entry .table.class13 
grid   .table.class11  x .table.class13 -padx 1m
pack .table </PRE></TD></TR>
</TABLE></CENTER><P>

<P>  Here is the way to position an individual widget:

<P><CENTER><TABLE BORDER><TR><TD><DL>
<DT><STRONG><PRE>grid <CITE>SLAVE</CITE> <NAME=#S13.5rowgrid>-row</A> <CITE>ROW_NUMBER</CITE> <NAME=#S13.5columngrid>-column</A> <CITE>COLUMN_NUMBER ?OPTIONS?</CITE></PRE></STRONG><DD>
positions the widget <CITE>SLAVE</CITE> at the row and column given by <CITE>ROW_NUMBER</CITE> 
and <CITE>COLUMN_NUMBER</CITE>.
Both column and row numbers start with 0.
</DL></TD></TR></TABLE></CENTER></P>

<P>  As an example, this will place the missing entry widget in Figure 13.5c.

<PRE>
entry .table.class12
grid .table.class12 -row 1 -column 1 -padx 1m
</PRE>

<P> <STRONG>Remark</STRONG> <DL><DD> The use of the frame named <TT>.table</TT> may seem
unnecessary and in a sense it is.  However, when a widget becomes a master
for a grid, certain variables are kept by Tk for keeping track of the
grid.  These variables are not cleared by destroying all the slaves in the
grid.  You must destroy the master too.
<P>  A consequence of not having the <TT>.table</TT> frame would be that after
running <TT>cleanTk</TT> (or some other program that destroys all widgets), any
new grid you started would not be truly new.  In fact its first row number
would be the row number after the last row of the old grid.  
</DL>

<P>  Returning to the use of <TT>grid</TT> to define an entire row, if you follow a
widget name in <CITE>?SLAVES?</CITE> with an argument consisting just of a hyphen
<TT>-</TT>, then the cell for that widget will be an extra column wide.  We can
use this and the <TT>-sticky</TT> option to add a third row to the widget of
Figure 13.5c.  For completeness, Figure 13.5d recaps all the steps previously
taken.

<P><CENTER><TABLE BORDER>
<CAPTION><ADDR>Figure 13.5d: A widget stretched between three columns.</ADDR></CAPTION>
<TR ALIGN=center><TD><IMG SRC="F13x5d.JPG" tppabs="http://www.mapfree.com/sbf/tcl/book/select/Figs/F13x5d.JPG">
</TD></TR>
<TR><TD><PRE>frame .table
label .table.col1header -text "First Trimester" -padx 1m
label .table.col2header -text "Second Trimester" -padx 1m
label .table.col3header -text "Third Trimester" -padx 1m
grid .table.col1header .table.col2header .table.col3header
entry .table.class11 
entry .table.class13
grid   .table.class11  x .table.class13 -padx 1m
pack .table
entry .table.class12
grid .table.class12 -row 1 -column 1 -padx 1m
entry .table.long
grid .table.long - - -sticky news -padx 1m </PRE></TD></TR>
</TABLE></CENTER><P>

<P> Another way to accomplish the same thing is to use the
<TT><NAME=#S13.5clmnspngrd>-columnspan</A></TT> option when placing an individual widget with
the <TT>-row</TT> and <TT>-column</TT> options.  The value of <TT>-columnspan</TT> is
simply the number of columns you want the widget to use.  This method extends
naturally to a way to place a widget in multiple rows.  There is a
<TT><NAME=#S13.5rowspangrid>-rowspan</A></TT> option for this that works analogously to
<TT>-columnspan</TT>.

<P><CENTER><TABLE BORDER>
<CAPTION><ADDR>Figure 13.5e: Exercise 13.5a.</ADDR></CAPTION>
<TR ALIGN=center><TD><IMG SRC="F13x5e.JPG" tppabs="http://www.mapfree.com/sbf/tcl/book/select/Figs/F13x5e.JPG">
</TD></TR>
</TABLE></CENTER><P>

<P><A NAME="13.5a">
<STRONG>Exercise 13.5a</STRONG> </A><DL><DD>
 Use <TT>grid</TT> to create the pattern shown in Figure 13.5e.
Your script should first create a 
4 x 3 
array of 12 black boxes and, at the
same time, create a data structure that can be used to map row and column to
the name of the widget located at that position.  Then it should destroy
the widget in the second row and first column.  Finally, it should extend
the widget in the first row and column so that it appears over two rows
as shown.   
<P>
<A HREF="13.9.html#Sol13.5a" tppabs="http://www.mapfree.com/sbf/tcl/book/select/Html/13.9.html#Sol13.5a">Solution</A></DL>



<!-- Linkbar -->
<P><CENTER><FONT SIZE=2><NOBR>
<STRONG>From</STRONG>
<A HREF="javascript:if(confirm('http://www.mapfree.com/sbf/tcl/book/home.html  \n\nThis file was not retrieved by Teleport Pro, because it is addressed on a domain or path outside the boundaries set for its Starting Address.  \n\nDo you want to open it from the server?'))window.location='http://www.mapfree.com/sbf/tcl/book/home.html'" tppabs="http://www.mapfree.com/sbf/tcl/book/home.html">Tcl/Tk For Programmers</A><WBR>
<STRONG>Previous</STRONG>
<A HREF="13.4.html" tppabs="http://www.mapfree.com/sbf/tcl/book/select/Html/13.4.html">section</A><WBR>
<STRONG>Next</STRONG>
<A HREF="13.6.html" tppabs="http://www.mapfree.com/sbf/tcl/book/select/Html/13.6.html">section</A><WBR>
<STRONG>All</STRONG>
<A HREF="13.html" tppabs="http://www.mapfree.com/sbf/tcl/book/select/Html/13.html">sections</A><WBR>
<STRONG>Author</STRONG>
<A HREF="javascript:if(confirm('http://www.mapfree.com/mp/jaz/home.html  \n\nThis file was not retrieved by Teleport Pro, because it is addressed on a domain or path outside the boundaries set for its Starting Address.  \n\nDo you want to open it from the server?'))window.location='http://www.mapfree.com/mp/jaz/home.html'" tppabs="http://www.mapfree.com/mp/jaz/home.html">J. A. Zimmer</A><WBR>
<STRONG>Copyright</STRONG>
<A HREF="copyright.html" tppabs="http://www.mapfree.com/sbf/tcl/book/select/Html/copyright.html">Notice</A><WBR>
<P>
<I>Jun 17, 1998</I>
 </NOBR></FONT></CENTER></BODY></HTML>


⌨️ 快捷键说明

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