Scripting-GUI-TclTk(1).pdf
(
21 KB
)
Pobierz
Scripting Graphical Commands with Tcl/Tk Mini-HOWTO
Scripting Graphical Commands with Tcl/Tk
Mini−HOWTO
Salvador J. Peralta
<
speralta [at] willamette [dot] edu
>
Revision History
Revision 1.0 release 2003−08−19 Revised by: sjp
Subtitle: Putting a graphical face on commandline utilities with Tcl and Tk.
One of the richest aspects of Linux is its abundance of commandline utilities. The ability to rapidly provide a
graphical frontend for those utilities to make them available to non−technical users can be a handy skill for a
developer or administrator to possess. This article provides a cookbook−style tutorial introduction to Tcl and
Tk, a scripting language and graphical toolkit that were designed to accomplish that very task.
Scripting Graphical Commands with Tcl/Tk Mini−HOWTO
Table of Contents
1. Copyright.
........................................................................................................................................................1
2. Introduction to Tcl and Tk..
...........................................................................................................................2
3. Tcl and Tk Basics.
...........................................................................................................................................3
4. Adding Features.
.............................................................................................................................................6
5. Conclusions.
.....................................................................................................................................................9
6. Further Reading.
...........................................................................................................................................10
7. About the Author.
.........................................................................................................................................11
i
1. Copyright
Copyright (c) 2003 Salvador Peralta
Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free
Documentation License, Version 1.2 or any later version published by the Free Software Foundation; with no
Invariant Sections, no Front−Cover Texts, and no Back−Cover Texts.
1. Copyright
1
2. Introduction to Tcl and Tk
The tool command language Tcl (pronounced tickle) is an interpreted, action−oriented, string−based,
command language. It was created by John Ousterhaut in the late 1980's along with the Tk graphical toolkit.
Tcl and the Tk toolkit comprise one of the earliest scripted programming environments for the X Window
System. Though it is venerable by today's standards, Tcl/Tk remains a handy tool for developers and
administrators who want to rapidly build graphical frontends for command line utilities.
Tcl and Tk come bundled with most major Linux distributions and source−based releases are available from
tcl.sourceforge.net. If Tcl and Tk are not installed on your system, the source releases are available from the
SourceForge Tcl project: http://tcl.sourceforge.net/. Binary builds for most Linux distributions are available
from rpmfind.net. A binary release is also available for Linux and other platforms from Active State at
http://aspn.activestate.com/ASPN/Tcl.
2. Introduction to Tcl and Tk
2
3. Tcl and Tk Basics
Tcl is built up from commands which act on data, and which accept a number of options which specify how
each command is executed. Each command consists of the name of the command followed by one or more
words separated by whitespace. Because Tcl is interpreted, it can be run interactively through its shell
command,
tclsh
, or non−interactively as a script. When Tcl is run interactively, the system responds to each
command that is entered as illustrated in the following example. You can experiment with tclsh by simply
opening a terminal and entering the command
tclsh
.
$ tclsh
% set a 35
35
% expr 35 * $a
1225
% puts "35 * a is: [ expr 35 * $a ]"
35 * a is: 1225
% exit
$
The previous example illustrates several aspects of the Tcl language. The first line,
set a 35
assigns 35 to the
variable
a
using the
set
command. The second line evaluates the result of 35 times the value of
a
using the
expr
command. Note that Tcl, like Perl and Bash requires the use of the dollar operator to get the value of a
variable. The open brackets around the expression
[ expr 35 * $a ]
instruct the interpreter to perform a
command substitution on the expression, adds it to the rest of the string and uses the puts command to print
the string to Tcl's default output channel, standard output.
Tcl's windowing shell,
Wish
, is an interpreter that reads commands from standard input or from file, and
interprets them using the Tcl language, and builds graphical components from the Tk toolkit. Like the
tclsh
, it
can be run interactively.
To invoke Wish interactively, start X on your system, open a terminal, and type
wish
at the command prompt.
If your environment is set up properly, this will launch an empty root window and start the windowing shell in
your terminal. The following example is a two−line script that is one of the simplest programs that can be
created with
wish
:
$ wish
% button .submit −text "Click Me" −command { puts "\nHello World" }
.submit
% pack .submit
Let's break down these two lines of code:
button .submit −text "Click Me" −command { puts "\nHello World" }
:
The
button
command enables you to create and manipulate the Tk button widget. As with all Tk widgets, the
syntax is
button .name [−option value] [−option value] ...
. The curly braces surrounding the
puts
command
allow you to nest the text string, "Hello World", inside of the command without performing any variable
substitutions. Other basic widgets include the following: label, checkbutton, radiobutton, command, separator,
entry, and frame. Click the button a few times to verify that it works.
pack .submit
3. Tcl and Tk Basics
3
Plik z chomika:
megaskrypty
Inne pliki z tego folderu:
3-Button-Mouse(1).pdf
(58 KB)
XWindow-Overview-HOWTO(1).pdf
(30 KB)
Xterm-Title(1).pdf
(39 KB)
Xterminals(1).pdf
(30 KB)
Xinerama-HOWTO(1).pdf
(33 KB)
Inne foldery tego chomika:
Programowanie
Zgłoś jeśli
naruszono regulamin