User Interface Development Tool: Tcl/Tk
[
lecture notes | CSC 397 | Pete Sanderson | Computer Science | SMSU ]
Table of Contents
Origins
Tcl characteristics
Tk characteristics
Procedure for preparing demo
Hello world in Tcl/Tk with variations
Packing using geometry manager
The ExecLog example
Resources
Practical Programming in Tcl and Tk 2nd Edition, by Brent Welch
Scriptics ("The Tcl Platform Company") provides definitive Tcl web resource
Sun Microsystems also supports Tcl/Tk
MI/X is a free X server for Mac or Windows from MicroImages..
Origins
TCL = Tool Command Language
TK = Tool Kit
- Developed by Dr. John Ousterhout at UC Berkeley (later a Sun Distinguished Engineer; in 1998 formed Scriptics Corporation to market Tcl to industry)
- Developed as a tool for development of Sprite network OS at UCB.
- Developed for Unix/X environment
- Ported to Mac/Win95NT/OS2, and more??
Return to the Top
TCL characteristics
Scripting language (like Perl or a shell language)
Interpreted (like other scripting languages)
syntax resembles Unix shell languages (and a bit of C)
Interpreter (and thus language) can be extended to implement new commands
Useful as 'glue' to combine existing programs in powerful ways
Return to the Top
TK characteristics
Extension to TCL
Provides set of commands to create and manipulate widgets
Gives application an event-driven control flow
Widgets are associated with events (and thus can respond to them)
GUI widgets are related in tree hierarchy with '.' as root (primary window) [ looks like file pathname except use '.' instead of '/']
A sampling of Tk widgets:
frame |
Container widget used with geometry managers |
button |
Command button |
radiobutton |
One of a set of radio buttons linked to variable |
menu |
Creates a menu |
canvas |
Supports lines, boxes, arcs, polygons, widgets, etc |
label |
Read-only one-line label |
entry |
One-line text entry widget |
message |
Read-only multi-line text |
scrollbar |
Scrollbar linked to another widget |
scale |
Scale widget to adjust value of a variable |
Return to the Top
Procedure for preparing demo
MI/X is a free X server for Mac or Windows from MicroImages. As of October 1998, the Windows version is X11R5, the Mac version is X11R6. Thanks to Jeff Peck for providing this URL.
In the X world, server is local and client is remote. In the demo configuration, server runs on the Gateway (WinNT), client runs on csc.smsu.edu.
Procedure to run the demo is:
- On the Gateway, launch MI/X (assuming it has been installed).
- On the Gateway, telnet to CSC.
- On CSC, set the DISPLAY environment variable to point back to the Gateway, e.g. setenv DISPLAY joe.smsu.edu:0.0
- On CSC, run the xterm program in background, e.g. xterm &
- On Gateway MI/X window, CSC command shell window will appear.
- Subsequent interaction is through MI/X and its windows. MI/X default window manager is twm.
- Run Tcl/Tk programs through wish interpreter. e.g. wish hello.tcl
Return to the Top
Hello World in Tcl/Tk with variations
Basic:
button .hello -text Hello \
-command {puts stdout "Hello, World!"}
pack .hello -padx 20 -pady 10
'.' is the parent frame
'.hello' identifies button hello as child of '.' Button label is "Hello"
the backslash ('\') indicates that specification continues on next line.
'-command' specifies response when button pressed
curly brackets are used to group a set of words.
'pack' places button into parent frame and displays
Variation on code (does not affect display):
set theButton .hello
button $theButton -text Hello \
-command {puts stdout "Hello, World!"}
pack $theButton -padx 20 -pady 10
widget names can be put into variables to facilitate single point control
Variation to show dynamic attributes:
button .hello -text Hello \
-command {.hello config -text Goodbye! \
-command exit}
pack .hello -padx 20 -pady 10
Widget attributes can be modified at runtime using config
When Hello button is selected first time, its label changes to Goodbye and action changes to exit
When button is selected second time, program exits.
Return to the Top
Packing using geometry manager
(chap 12 in Welch first edition, chap 20 in second edition)
- pack
is a Tk "geometry manager" for arranging widgets
- The packer is constraint-based.
- manager uses one widget as parent (typically a frame) and arranges children (slaves) inside the parent
- widget must be managed (packed) to appear
- one constraint is "pack toward" (-side) direction: top, bottom, right, left
- Welch example shows two frames inside main frame, packed toward top.
# Example 20-1
# Two frames packed inside the main frame.
#
# Make the main window black
. config -bg black
# Create and pack two frames
frame .one -width 40 -height 40 -bg white
frame .two -width 100 -height 50 -bg grey50
pack .one .two -side top
- Next Welch example shows hierarchy: pack buttons "toward left" inside frame, pack frames "toward top" inside parent frame. Pleasing arrangement.
# Example 20-3
# A horizontal stack inside a vertical stack.
#
frame .one -bg white
frame .two -width 100 -height 50 -bg grey50
# Create a row of buttons
foreach b {alpha beta gamma} {
button .one.$b -text $b
pack .one.$b -side left
}
pack .one .two -side top
Note visual constraints:
- first frame size not specified so is just large enough to hold buttons.
- Second frame size fixed.
- Parent frame size not specified; just large enough for both children
- place
is another Tk geometry manager that expects numerical specification of widget sizes and locations (relative or absolute).
Return to the Top
The ExecLog Example
An interface for entering Unix commands into text entry field with output displayed in separate scrolling text window. Demonstrates a number of the techniques described above, and then some. Primary frame has two child frames: one contains the text entry field and two buttons ("Run it" and "Quit"); the other contains the text output window along with its scrollbar. This is example 19-1 from Welch second edition (example 11-1 from first edition).
Return to the Top
[
lecture notes | CSC 397 | Pete Sanderson | Computer Science | SMSU ]
Last reviewed: 15 October 1998
Peter Sanderson ( pete@csc.smsu.edu )