📄 0518-0521.html
字号:
<HTML>
<HEAD>
<TITLE>Developer.com - Online Reference Library - 0672311739:RED HAT LINUX 2ND EDITION:tcl and tk Programming</TITLE>
<META NAME="ROBOTS" CONTENT="NOINDEX, NOFOLLOW">
<SCRIPT>
<!--
function displayWindow(url, width, height) {
var Win = window.open(url,"displayWindow",'width=' + width +
',height=' + height + ',resizable=1,scrollbars=yes');
}
//-->
</SCRIPT>
</HEAD>
-->
<!-- ISBN=0672311739 //-->
<!-- TITLE=RED HAT LINUX 2ND EDITION //-->
<!-- AUTHOR=DAVID PITTS ET AL //-->
<!-- PUBLISHER=MACMILLAN //-->
<!-- IMPRINT=SAMS PUBLISHING //-->
<!-- PUBLICATION DATE=1998 //-->
<!-- CHAPTER=25 //-->
<!-- PAGES=0499-0528 //-->
<!-- UNASSIGNED1 //-->
<!-- UNASSIGNED2 //-->
<P><CENTER>
<a href="0515-0517.html">Previous</A> | <a href="../ewtoc.html">Table of Contents</A> | <a href="0522-0525.html">Next</A>
</CENTER></P>
<A NAME="PAGENUM-518"><P>Page 518</P></A>
<P>To create and manipulate widgets, the windowing shell,
wish, must be used. To invoke wish interactively, type
wish at the UNIX prompt. The following wish prompt will appear:
</P>
<!-- CODE SNIP //-->
<PRE>
%
</PRE>
<!-- END CODE SNIP //-->
<P>Along with this, an empty window will pop up on the screen. This window is the
wish root window (called .) and all the widgets that are created will appear with
it.
</P>
<H4><A NAME="ch25_ 20">
Creating Widgets
</A></H4>
<P>This section shows how to create a widget and manipulate it. First, create a button:
</P>
<!-- CODE SNIP //-->
<PRE>button .button;
</PRE>
<!-- END CODE SNIP //-->
<P>So what did that do?
</P>
<P>Well, the widget type is specified as button, so
tk created a button. The path is .button, so tk created the button in the root window
(. is the root tk window) and named it button.
</P>
<P>So where is the button, anyway?
</P>
<P>The button isn't displayed right now; tk simply created it. In order to display the button,
you need to tell tk how to display the widget. For this, use
the pack command and give it the path to the widget you want to display:
</P>
<!-- CODE SNIP //-->
<PRE>pack .button;
</PRE>
<!-- END CODE SNIP //-->
<P>Now the button is showing, but it's blank (see Figure 25.1). This is where widget's
options come into play.
</P>
<BR>
Figure 25.1.<BR>
A plain button.<BR>
<a href="27rhu01.html"><img src="images/tn_27rhu01_jpg.jpg"></a><BR>
<A NAME="PAGENUM-519"><P>Page 519</P></A>
<H4><A NAME="ch25_ 21">
Widget Options
</A></H4>
<P>All tk widgets use standard options that control appearance and function. Most widgets
understand the following options:
</P>
<TABLE WIDTH="360">
<TR><TD>
-background color, -bg color
</TD><TD>
The background color of the widget. Valid
values
are of the form #RRGGBB, #RRRGGGBBB, or one of
the
names defined in /usr/lib/X11/rgb.txt.
</TD></TR>
<TR><TD>
-foreground color, -fg color
</TD><TD>
The foreground color of the widget. Valid
values
are of the form #RRGGBB, #RRRGGGBBB, or one of
the
names defined in /usr/lib/X11/rgb.txt.
</TD></TR>
<TR><TD>
-height pixels
</TD><TD>
The widget's height in pixels.
</TD></TR>
<TR><TD>
-width pixels
</TD><TD>
The widget's width in pixels.
</TD></TR>
<TR><TD>
-borderwidth pixels, -db pixels
</TD><TD>
The width of the widget's border in pixels.
</TD></TR>
<TR><TD>
-padx pixels
</TD><TD>
Extra space required by the widget in the
x
direction.
</TD></TR>
<TR><TD>
-pady pixels
</TD><TD>
Extra space required by the widget in the
y
direction.
</TD></TR>
<TR><TD>
-relief type
</TD><TD>
The 3D effect of the widget, where
type is one of
these strings: flat, raised, grove,
ridge, sunken.
</TD></TR>
<TR><TD>
-text string
</TD><TD>
The string to display in the widget.
</TD></TR>
<TR><TD>
-font font
</TD><TD>
The font to be used for the text displayed in
a
widget; valid font definitions are given by the
command
xlsfonts.
</TD></TR>
<TR><TD>
-command command
</TD><TD>
The tcl command to execute when the widget
is
used; usually this is the name of a procedure or
an
exec statement.
</TD></TR>
</TABLE>
<P>In addition to these options, the pack command understands the following options of its own:
</P>
<TABLE WIDTH="360">
<TR><TD>
-side type
</TD><TD>
Controls the order in which widgets are
placed.
Valid types are left, right, top, or
bottom. For
example, left indicates that new widgets
should
placed to the left of existing widgets.
</TD></TR>
<TR><TD>
-fill type
</TD><TD>
Controls whether or not widgets are stretched
to
fill up open space in the window. Valid values
are
none, x, y, or both. For example, both
indicates
that widgets should fill up all open space.
</TD></TR>
<TR><TD>
-expand value
</TD><TD>
Controls whether or not widgets expand if
the
window's size increases. value is either 0 or
1, with
1 indicating true.
</TD></TR>
</TABLE>
<A NAME="PAGENUM-520"><P>Page 520</P></A>
<H3><A NAME="ch25_ 22">
A tcl/tk Widget Programming Example
</A></H3>
<P>Now that you know about the options for widgets and for
pack, you can start using them. One of the interesting features of widgets is their
reliefs, the widgets' 3D look. To get an idea of how each
relief looks, make some labels, using the following:
</P>
<!-- CODE SNIP //-->
<PRE>foreach i {raised sunken flat groove ridge} {
label .$i -relief $i -text $i;
pack .$i
}
</PRE>
<!-- END CODE SNIP //-->
<P>This example iterates through the set of relief types, creating one
label for each type, along with setting each label's
text to be the relief type. The layout will look similar to Figure 25.2.
</P>
<P>
Figure 25.2.<BR>
Labels of varying <BR>
reliefs.<BR>
</P>
<a href="27rhu02.html"><img src="images/tn_27rhu02_jpg.jpg"></a><BR>
<P>There are two things to notice here. First, the labels are not all the same size. Second, the
labels are stacked one on top of the other. This is an example of the
pack command's default behavior; it determines the size of each widget automatically and then places each widget below
the preceding widget that was placed.
</P>
<P>Let's make all the labels the same size and pack them next to each other, instead of one on
top of the other. There are two ways to do this. The first is to rewrite the loop:
</P>
<!-- CODE SNIP //-->
<PRE>foreach i {raised sunken flat groove ridge} {
label .$i -relief $i -text $i -height 10 -width 10;
pack .$i -side left
}
</PRE>
<!-- END CODE SNIP //-->
<P>The second way is to reconfigure the labels using the
configure option, which has the following syntax:
</P>
<PRE>widget configure option
</PRE>
<A NAME="PAGENUM-521"><P>Page 521</P></A>
<P>In this case, you could use the following loop (after the labels are created):
</P>
<!-- CODE SNIP //-->
<PRE>foreach i {raised sunken flat groove ridge} {
.$i configure -height 10 -width 10;
pack .$i -side left;
}
</PRE>
<!-- END CODE SNIP //-->
<P>So why use configure?
</P>
<P>If wish is run interactively, and one version of the loop was given, modifying it and running
it again will produce the following error:
</P>
<!-- CODE SNIP //-->
<PRE>window name "raised" already exists in parent
</PRE>
<!-- END CODE SNIP //-->
<P>This is the method in which wish tells the programmer that the program has attempted to
re-create an existing widget (in this case with the label raised). So, you need to use
configure. In fact, configure is required any time an existing widget needs to be changed.
</P>
<P>In this case, the only way to use the new version of the loop is to destroy the existing
labels, using the destroy command:
</P>
<!-- CODE SNIP //-->
<PRE>foreach i {raised sunken flat groove ridge} { destroy .$i }
</PRE>
<!-- END CODE SNIP //-->
<P>The new result will be similar to Figure 25.3.
</P>
<BR>
<P>
Figure 25.3.<BR>
Labels of varying <BR>
relief, packed next to <BR>
each other.<BR>
</P>
<a href="27rhu03.html"><img src="images/tn_27rhu03_jpg.jpg"></a><BR>
<P>Now back to the example. There are two things that look like they should be fixed in
Figure 25.3. First, it is difficult to tell the labels apart. Second, most of the window is blank.
</P>
<P>You can make the labels easier to distinguish by padding them when they are packed and
by increasing their borderwidths. To make the labels take up all of the available space, give
pack the fill option for both x and y and set the
expand option to true:
</P>
<P><CENTER>
<a href="0515-0517.html">Previous</A> | <a href="../ewtoc.html">Table of Contents</A> | <a href="0522-0525.html">Next</A>
</CENTER></P>
</td>
</tr>
</table>
<!-- begin footer information -->
</body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -