📄 467-470.html
字号:
<HTML>
<HEAD>
<TITLE>Linux Configuration and Installation:Programming in Linux</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=1558285660//-->
<!--TITLE=Linux Configuration and Installation//-->
<!--AUTHOR=Patrick Volkerding//-->
<!--AUTHOR=Kevin Reichard//-->
<!--AUTHOR=Eric Foster//-->
<!--PUBLISHER=IDG Books Worldwide, Inc.//-->
<!--IMPRINT=M & T Books//-->
<!--CHAPTER=10//-->
<!--PAGES=467-470//-->
<!--UNASSIGNED1//-->
<!--UNASSIGNED2//-->
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="464-467.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="470-473.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
<P><BR></P>
<P><FONT SIZE="+1"><B>Working with Tcl</B></FONT></P>
<P>To try out some Tcl programs, you should run the Tcl interpreter, called <B>wish</B>, which allows you to enter Tcl commands as if you were in a Tcl-based shell, which is what <B>wish</B> is.</P>
<P>The most interesting use of Tcl is to create graphical programs including widgets such as push buttons. To create a push button in Tcl, use the <B>button</B> command:</P>
<!-- CODE SNIP //-->
<PRE>
button .b1 \
-text "My first button" \
-command { exit }
</PRE>
<!-- END CODE SNIP //-->
<BLOCKQUOTE>
<P><FONT SIZE="-1"><HR><B>NOTE: </B>Because Tcl is a scripting language, you can use the backslash character, <B>\</B>, to extend a command on one line over many lines. This makes your programs easier to read.<HR></FONT>
</BLOCKQUOTE>
<P>The preceding command creates a button widget named <B>.b1</B> (the leading period is important). Just like Linux uses the <B>/</B> character to mark the root directory, Tcl uses the period (<B>.</B>) to mark the root widget (your application’s main window). We’re then creating a button widget that sits like a subdirectory beneath the root widget.</P>
<P>The <I>-command</I> sets the Tcl command that will run when the button gets pushed. In our case, the <B>exit</B> command exits <B>wish</B> and our Tcl script.</P>
<BLOCKQUOTE>
<P><FONT SIZE="-1"><HR><B>NOTE: </B>It’s important to note that the code for the <I>-command</I> gets evaluated only when the button is pushed, usually sometime after the button is created and usually when the Tcl program is in another procedure. Because of this, local variables no longer have their values at execution time.
<P>This can be very difficult to debug. We will show some workarounds in the sample code.<HR></FONT>
</BLOCKQUOTE>
</P>
<P>To get a widget to appear, we must pack it. The <B>pack</B> command takes a lot of parameters, including the name of the widget or widgets to pack:</P>
<!-- CODE SNIP //-->
<PRE>
pack .b1
</PRE>
<!-- END CODE SNIP //-->
<BLOCKQUOTE>
<P><FONT SIZE="-1"><HR><B>WARNING: </B>Tcl widgets don’t appear until you pack them.<HR></FONT>
</BLOCKQUOTE>
<P><FONT SIZE="+1"><B>Making Script Files for Tcl</B></FONT></P>
<P>You can put together a set of Tcl commands into a script file, just like C and Bourne shell scripts. The program to execute the script is again <B>wish</B>. The following script assumes that <B>wish</B> is located in <B>/usr/bin</B> (as it is for Linux).</P>
<P>To turn our first example into a working script, we do the following:</P>
<!-- CODE //-->
<PRE>
#!/usr/bin/wish
#
# example1.tcl
#
# Create a button.
button .b1 \
-text "My first button" \
-command { exit }
pack .b1
# example1.tcl
</PRE>
<!-- END CODE //-->
<P>To show you more of a flavor of Tcl scripting, we put together the following file. In it, Tcl commands create a set of buttons that allow you to launch useful Linux programs like <B>xman</B> and <B>xterm</B>. The toolbar appears at the bottom of the screen and uses the override-redirect mode that prevents a window manager from placing a title bar around the window. Also, in honor of Windows 95, we place the current time at the end of the toolbar.</P>
<P>In our script, we create a number of procedures, called <I>procs</I> in Tcl. The <I>exec_cmd</I> procedure executes a text string as a UNIX command. We use the <I>eval</I> statement to deal with text-string issues and evaluate any Tcl variables within the command. Try this Tcl script without the <I>eval</I> in the <I>exec_cmd</I> procedure and you’ll see why we need it. (It has to do with evaluating the arguments as one string or as a command line; this is one area where Tcl is not intuitive.)</P>
<P>The <I>update_time</I> procedure gets the current time, using the UNIX <B>date</B> command, and then changes the text displayed in a widget (you pass <I>update_time</I> the widget name) and uses the <B>after</B> command to set up a callback, the <I>update_time</I> procedure, to get called after a particular amount of time. With Tcl 7.5, you can use the built-in <B>clock</B> command instead of calling the Linux program <B>date</B>.</P>
<P>The main part of the Tcl script creates a frame widget to hold all the buttons and then creates a set of buttons. The <B>logo</B> button quits the script when it’s pressed. We use the words <I>Linux</I> and your machine’s hostname for the text in the <B>logo</B> button.</P>
<P>The whole point of this Tcl script is to launch commonly used applications, particularly graphical ones. The buttons set up are listed in Table 10.6.</P>
<TABLE WIDTH="100%"><CAPTION><B>Table 10.6</B> Applications Launched from the toolbar.tcl Script
<TR>
<TH COLSPAN="2"><HR>
<TR>
<TH WIDTH="30%" ALIGN="LEFT">Button
<TH WIDTH="70"% ALIGN="LEFT">Launches
<TR>
<TD>Manuals
<TD>xman to view Linux online manuals
<TR>
<TD>Mail
<TD>elm (inside xterm window) to read mail
<TR>
<TD>Shell
<TD>xterm for a command-line shell
<TR>
<TD>File Manager
<TD>xfm, a Linux file manager
<TR>
<TD>Images
<TD>xv, an image-viewing and file-browsing program
<TR>
<TD>Mahjongg
<TD>Our favorite game on X
<TR>
<TD COLSPAN="2"><HR>
</TABLE>
<P><BR></P>
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="464-467.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="470-473.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
</td>
</tr>
</table>
<!-- begin footer information -->
</body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -