📄 0522-0525.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="0518-0521.html">Previous</A> | <a href="../ewtoc.html">Table of Contents</A> | <a href="0526-0528.html">Next</A>
</CENTER></P>
<A NAME="PAGENUM-522"><P>Page 522</P></A>
<!-- CODE //-->
<PRE>foreach i {raised sunken flat groove ridge} {
label .$i -relief $i -text $i;
.$i configure -height 5 -width 5 -borderwidth 5;
pack .$i -side left -padx 5 -pady 5 -fill both -expand 1;
}
</PRE>
<!-- END CODE //-->
<P>The result will be similar to Figure 25.4.
</P>
<BR>
Figure 25.4.<BR>
Labels of varying <BR>
relief, version 3.<BR>
<a href="27rhu04.html"><img src="images/tn_27rhu04_jpg.jpg"></a><BR>
<P>This example can be easily changed to use any of the widget types by replacing
label with a different type of widget.
</P>
<H3><A NAME="ch25_ 23">
A tcl/tk Interface to xsetroot
</A></H3>
<P>This section introduces some of the other abilities of the
tk toolkit by applying them to the development of a GUI front end for the X Window program
xsetroot.
</P>
<P>Most X Window users will be familiar with the X Window program
xsetroot that can be used to set the background color of the root window, under X11. The actual command is
</P>
<!-- CODE SNIP //-->
<PRE>xsetroot -solid color
</PRE>
<!-- END CODE SNIP //-->
<P>where color can be given in the form #RRGGBB. The front end will allow for colors to be
previewed and then applied.
</P>
<P>Let's get started. The first thing that you need is a variable that holds the color. Then you
need to create two basic frames, one for the main area of the application and another for
putting messages in. You will also need a third frame for the controls.
</P>
<P>Frames are a handy thing to use because they can be used to pack items of a particular
type/function together. Also, they are useful in partitioning up a window into sections that
don't change.
</P>
<A NAME="PAGENUM-523"><P>Page 523</P></A>
<P>Create the frames and the color globally:
</P>
<!-- CODE SNIP //-->
<PRE>set color "#000000";
frame .main_frame;
frame .message_frame;
frame .control_frame;
</PRE>
<!-- END CODE SNIP //-->
<P>Now pack the frames:
</P>
<!-- CODE SNIP //-->
<PRE>pack .control_frame -in .main_frame -expand 1 -fill both;
pack .main_frame -anchor c -expand 1;
pack .message_frame -anchor s -padx 2 -pady 2 \
-fill x -expand 1;
</PRE>
<!-- END CODE SNIP //-->
<P>You also need to create a label to handle messages. I'll do this as a procedure, so it will be
easy to modify and execute:
</P>
<!-- CODE SNIP //-->
<PRE>proc make_message_label {} {
label .message_label -relief sunken;
pack .message_label -anchor c \
-in .message_frame -padx 2 \
-pady 2 -fill both -expand 1;
}
</PRE>
<!-- END CODE SNIP //-->
<P>Pack the message label into .message_frame so that it is at the bottom of the window at
all times.
</P>
<P>Now make the scales. You need three scales, one for red, blue, and green, with each one
going from 0 to 255. You also need to pack the scales and their corresponding labels in their
own frame:
</P>
<!-- CODE //-->
<PRE>proc make_scales {} {
frame .scale_frame;
foreach i {red green blue} {
frame .scale_frame_$i -bg $i;
label .label_$i -text $i -bg $i \
-fg white;
scale .scale_$i -from 0 -to 255 \
-command setColor;
pack .label_$i .scale_$i \
-in .scale_frame_$i;
pack .scale_frame_$i -in .scale_frame \
-side left -padx 2 -pady 2;
}
pack .scale_frame -in .control_frame \
-side left -expand 1;
}
</PRE>
<!-- END CODE //-->
<P>This procedure is a good example of using frames. In all, this example creates four frames,
one for each slider and label pair and overall frame. Adding the label and the slider to their
own frame simplifies the overall layout strategy.
</P>
<P>Another example in this procedure is the use of the
-command option for a widget. Each time the scales change, the command specified by the
-command option is executed. In this case, the
setColor command, which sets the global variable
color, is executed:
</P>
<A NAME="PAGENUM-524"><P>Page 524</P></A>
<!-- CODE //-->
<PRE>proc setColor {value} {
global color;
foreach i {red green blue} {
set $i [format %02x [.scale_$i get]];
}
set color "#$red$green$blue";
.preview_label configure -bg $color;
.message_label configure -text "$color";
}
</PRE>
<!-- END CODE //-->
<P>You can preview the color change by setting the background color of the widget,
.preview_label. To create .preview label, use the following procedure:
</P>
<!-- CODE //-->
<PRE>proc make_preview {} {
global color;
frame .preview_frame;
label .preview_label -bg $color \
-height 5 -width 5;
pack .preview_label -in .preview_frame \
-padx 2 -pady 2 -fill both \
-anchor c -expand 1;
pack .preview_frame -in .control_frame \
-side bottom -fill both -expand 1 \
-padx 2 -pady 2;
}
</PRE>
<!-- END CODE //-->
<P>Now you need to add a few buttons—one to apply the changes, another to
quit the program. Use the following procedure:
</P>
<!-- CODE //-->
<PRE>proc make_buttons {} {
frame .button_frame;
button .apply -text "apply" \
-command setRootColor;
button .quit -text "quit" -command exit;
pack .apply .quit -in .button_frame \
-fill both -expand 1 -padx 2 \
-pady 2 -side left;
pack .button_frame -in .main_frame \
-fill both;
}
</PRE>
<!-- END CODE //-->
<P>You also need the following procedure, which sets the root color:
</P>
<!-- CODE //-->
<PRE>proc setRootColor {} {
global color;
catch {
exec xsetroot -solid $color;
} msg;
if {$msg != {}} {
set msg "An error occurred";
} else {
set msg "$color";
}
.message_label configure -text $msg;
}
</PRE>
<!-- END CODE //-->
<A NAME="PAGENUM-525"><P>Page 525</P></A>
<P>Now that you are done with the procedures, invoke them:
</P>
<!-- CODE SNIP //-->
<PRE>make_message_label;
make_scales;
make_preview;
make_buttons;
</PRE>
<!-- END CODE SNIP //-->
<P>You are now ready to test your little tcl application. When the application is run, the
resulting window should look like Figure 25.5.
</P>
<BR>
Figure 25.5.<BR>
The tksetroot <BR>
application.<BR>
<a href="27rhu05.html"><img src="images/tn_27rhu05_jpg.jpg"></a><BR>
<P>Listing 25.1 contains the complete source code of the
tksetroot application.
</P>
<P>Listing 25.1. tksetroot.
</P>
<!-- CODE //-->
<PRE>set color "#000000";
frame .main_frame;
frame .message_frame;
frame .control_frame;
pack .control_frame -in .main_frame -expand 1 -fill both;
pack .main_frame -anchor c -expand 1;
pack .message_frame -anchor s -padx 2 -pady 2 \
-fill x -expand 1;
proc make_message_label {} {
label .message_label -relief sunken;
pack .message_label -anchor c -in .message_frame \
-padx 2 -pady 2 -fill both -expand 1;
}
proc make_scales {} {
frame .scale_frame;
foreach i {red green blue} {
frame .scale_frame_$i -bg $i;
label .label_$i -text $i -bg $i -fg white;
scale .scale_$i -from 0 -to 255 \
-command setColor;
pack .label_$i .scale_$i -in .scale_frame_$i;
pack .scale_frame_$i -in .scale_frame -side left \
-padx 2 -pady 2;
}
pack .scale_frame -in .control_frame -side left -expand 1;
}
</PRE>
<!-- END CODE //-->
<PRE>
continues
</PRE>
<P><CENTER>
<a href="0518-0521.html">Previous</A> | <a href="../ewtoc.html">Table of Contents</A> | <a href="0526-0528.html">Next</A>
</CENTER></P>
</td>
</tr>
</table>
<!-- begin footer information -->
</body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -