📄 132.html
字号:
<HTML><TITLE>Geometry Management: Slicing</TITLE><BODY BGCOLOR="#FFF0E0" VLINK="#0FBD0F" TEXT="#101000" LINK="#0F0FDD">
<A NAME="top"><H1>Slicing</H1></A>
<P> The <TT>pack</TT> geometry manager is constraint based, which means that it
decides itself where subwindows go and how big they will be and that the basis
for its decisions are constraints it gets from the window manager, from the
subwidgets being packed, and from you. Your constraints are specified when
you invoke the <TT>pack configure</TT> action. When <TT>pack</TT> cannot satisfy all
constraints, it makes its own decisions about what is important. These
decisions always assigns greatest importance to overall geometry received from
the window manager.
<P> As mentioned in the previous section, widgets are placed under geometry
management by <TT>pack</TT> this way:
<P><DL>
<P> <DT><PRE>pack <CITE>SLAVES ?OPTIONS?</CITE></PRE><DD>
</DL></P>
Sample invocations are
<PRE>
pack .frame1 -side top
pack .frame2 -side top
</PRE>
These two statements are equivalent to
<PRE>
pack .frame1 .frame2 -side top
</PRE>
or even to
<PRE>
pack .frame1 .frame2
</PRE>
because the default value for the <TT><NAME=#S13.2sidepack>-side</A></TT> option is "top."
<P> As these examples imply, when multiple widgets are packed with one <TT>pack</TT>
statement, the options apply to all of them.
<P> A good source of building blocks for showing how <TT>pack</TT> works is the
<TT>box</TT> procedure of Script ES12.4c. Figure 13.2a shows an example of its
use. In that script, <TT>box</TT> is used three times to produce horizontal
rectangles of varying colors. The widget name passed to <TT>box</TT> is the name
of the parent widget – <TT>box</TT> itself names the frame widget it creates and
returns that name. The third argument can be "h" or "v" for a
horizontal or vertical rectangle. It can also be missing in which case box
produces a square.
<P> Although all subwindows go on a two-dimensional plane, we can organize them
by thinking in one dimension at a time. The trick is to slice a window
horizontally or vertically and then to slice each of the slices in the other
direction as required. Slices of slices can be sliced, if necessary.
<P> Again look at Figure 13.2a. This time, notice the horizontal
<CITE><NAME=#G13.2slicing>slicing</A></CITE> of the root window. Similarly, Figure 13.2b shows a
vertical slicing. Using the "left" value for the <TT>-side</TT> option creates
vertical slices from left to right.
<P><CENTER><TABLE BORDER>
<CAPTION><ADDR>Figure 13.2a: Horizontal slicing.</ADDR></CAPTION>
<TR ALIGN=center><TD><IMG SRC="F13x2a.JPG" tppabs="http://www.mapfree.com/sbf/tcl/book/select/Figs/F13x2a.JPG">
</TD></TR>
<TR><TD><PRE>pack [box . white h]
pack [box . grey50 h]
pack [box . black h] </PRE></TD></TR>
</TABLE></CENTER><P>
<P><CENTER><TABLE BORDER>
<CAPTION><ADDR>Figure 13.2b: Vertical slicing.</ADDR></CAPTION>
<TR ALIGN=center><TD><IMG SRC="F13x2b.JPG" tppabs="http://www.mapfree.com/sbf/tcl/book/select/Figs/F13x2b.JPG">
</TD></TR>
<TR><TD><PRE>pack [box . white v] -side left
pack [box . grey50 v] -side left
pack [box . black v] -side left </PRE></TD></TR>
</TABLE></CENTER><P>
<P> Conceptually, the easiest way to create complicated windows is to use
invisible frames when you want to reslice a horizontal slice vertically
or vice versa. For example, to create the pattern in Figure 13.2c, the
script can begin by creating horizontal slices as follows:
<PRE>
frame .top
frame .bottom
pack .top .bottom
</PRE>
<P> After <TT>.top</TT> and
<TT>.bottom</TT> are created, it is possible to create vertical slices within each
of them. The <TT>pack</TT> geometry manager must consider these vertical slices
to be slaves of <TT>.top</TT> and <TT>.bottom</TT> so we will name them as
children of <TT>.top</TT> and <TT>.bottom</TT>.
<P> For example, the two vertical slices in the <TT>.top</TT> frame could be named
<TT>.top.left</TT> and <TT>.top.right</TT>. Or, they could be created using the
<TT>box</TT> command using <TT>.top</TT> as the parent argument. It is this second
approach that you see in the script of Figure 13.2c.
<P><CENTER><TABLE BORDER>
<CAPTION><ADDR>Figure 13.2c: Vertical slices within horizontal slices.</ADDR></CAPTION>
<TR ALIGN=center><TD><IMG SRC="F13x2c.JPG" tppabs="http://www.mapfree.com/sbf/tcl/book/select/Figs/F13x2c.JPG">
</TD></TR>
<TR><TD><PRE>## create the widget for the top slice
<P> frame .top
<P> pack [box .top white] -side left
<P> pack [box .top grey50] -side left
## create the widget for the bottom slice
<P> frame .bottom
<P> pack [box .bottom grey50] -side left
<P> pack [box .bottom black] -side left
## pack the horizontal slices
<P> pack .top .bottom </PRE></TD></TR>
</TABLE></CENTER><P>
<P> In Figure 13.2c you cannot see either the <TT>.top</TT> or the <TT>.bottom</TT>
frame. Their only purpose is to provide a place where slicing can be done in
the opposite direction.
<P><A NAME="13.2a">
<STRONG>Exercise 13.2a</STRONG> </A><DL><DD>
Slice the root window vertically into three slices
so that the left
and right slices are white rectangles and the middle slice consists
of a red square stacked on a blue square stacked on a yellow square.
<P>
<A HREF="13.9.html#Sol13.2a" tppabs="http://www.mapfree.com/sbf/tcl/book/select/Html/13.9.html#Sol13.2a">Solution</A></DL>
<P><A NAME="13.2b">
<STRONG>Exercise 13.2b</STRONG> </A><DL><DD>
Alter the previous exercise so that the middle blue
square is sliced vertically into equal-sized black and blue rectangles.
Do this by using <TT>box</TT> to create black and blue squares
and then halving their widths. Do not look the original widths up
in <TT>box</TT>'s source code. Instead, obtain it from an existing
box with the <TT>cget</TT> widget command action.
<P>
<A HREF="13.9.html#Sol13.2b" tppabs="http://www.mapfree.com/sbf/tcl/book/select/Html/13.9.html#Sol13.2b">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.1.html" tppabs="http://www.mapfree.com/sbf/tcl/book/select/Html/13.1.html">section</A><WBR>
<STRONG>Next</STRONG>
<A HREF="13.3.html" tppabs="http://www.mapfree.com/sbf/tcl/book/select/Html/13.3.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 + -