Version 0 of An Overview of Tcl and Tk

Updated 2001-10-05 13:50:08

Purpose: The following is a series of notes used by John Ousterhout in the early 1990s as handouts for a series of Usenix "Intro to Tcl and Tk" tutorials. My hope is that the community will build upon these notes to bring them up to date and turn this into a free tutorial on Tcl and Tk.

An Overview of Tcl and Tk John Ousterhout

Tcl/Tk Tutorial, Part I


Introduction

  • Component technologies:
  1. Tcl: embeddable scripting language
  2. Tk: GUI toolkit and widgets based on Tcl.
  • The principle: universal scripting language controls everything: functions, interfaces, communication.
  • Results:
  1. Raise the level of X programming: simpler, 5-10x faster application development.
  2. Greater power: more things programmable, applications work together.
  3. Active objects: replace data with scripts.

Outline

  • Tcl scripting language.
  • Tk toolkit.
  • Tk applications.
  • Survey of applications and extensions.
  • Conclusions.

Tcl: Tool Command Language

  • Interactive programs need command languages:
  1. Typically redone for each application.
  2. Result: weak, quirky.
  3. emacs and csh powerful, but can't reuse.
  • Solution: reusable scripting language.
  1. Interpreter is a C library.
  2. Provides basic features: variables, procedures, etc.
  3. Applications extend with additional features.

Scripting Language Philosophy

  • Large, complex applications:
  1. Performance important.
  2. Need structure.
  3. Goal: prevent bad things.
  • Interactive commands, scripting:
  1. Performance less important.
  2. Minimum structure: less overhead, easy interchange.
  3. Goal: enable good things.

Two-Language Approach

  • Use Tcl for scripting, C or C++ for large things.
  • Goals for Tcl:
  1. Minimal syntax: easy to learn and type.
  2. Minimal structure: make things play together.
  3. Simple interfaces to C: extensibility.

Tcl: Tool Command Language

  • Simple syntax (similar to sh, C, Lisp):
  set a 47      ?       47
  • Substitutions:
  set b $a      ?       47
  set b [expr $a+10]    ?       57
  • Quoting:
  set b "a is $a"       ?       a is 47
  set b {[expr $a+10]} ?        [expr $a+10]

More On The Tcl Language

  • Rich set of built-in commands:
  1. Variables, associative arrays, lists.
  2. C-like expressions.
  3. Conditionals, looping:
 if "$x < 3" {
    puts "x is too small"
 }
  1. Procedures.
  2. Access to UNIX files, subprocesses.
  • Only representation is strings:
  1. Easy access from C.
  2. Programs and data interchangeable.

Factorial Procedure

 proc fac x {
    if $x<=1 {return 1}
    expr $x*[fac [expr $x-1]]
 }

 fac 4  ?       24

Embedding Tcl In Applications

  • Application generates scripts.
  • Tcl parses scripts, passes words to command procedures.
  • Application extends built-in command set:
  1. Define new object types in C.
  2. Implement primitive operations as new Tcl commands.
  3. Build complex features with Tcl scripts.

Extensions

  • Extensions can be developed independently:
  1. Network communication, database access, security, ...
  • Applications can include combinations of extensions.

The Tk Toolkit

  • The problem:
  1. Too hard to build applications with nice user interfaces.
  • The wrong solution:
  1. C++, object-oriented toolkits.
  2. Only small improvement (10-20%?): must still program at a low level.
  • The right solution:
  1. Raise the level of programming.
  2. Create interfaces by writing Tcl scripts.

Creating User Interfaces With Tk

  • Additional Tcl commands:
  1. Create Motif-like widgets.
  2. Arrange widgets.
  3. Bind events to Tcl commands.
  4. Manipulate X selection, focus, window manager, etc.
  • Library of C procedures:
  1. Create new widget classes.
  2. Create new geometry managers.

What's A Tk-Based Application?

  • The Tcl interpreter.
  • The Tk toolkit.
  • Application-specific C code (primitives!):
  1. New object types.
  2. New widgets.
  • Tcl scripts (compose primitives):
  1. Build user interface.
  2. Respond to events.

Wish: Windowing Shell

  • Create user interfaces by writing Tcl scripts.
  • Hello, world:
 button .hello -text "Hello, world" -command exit
 pack .hello
  • Simple directory browser: 30 lines
  • Web browser: 2000 lines
  • 10x less code for simple things.

Browser Wish Script

 #!/usr/local/bin/wish4.0

 listbox .list -yscroll ".scroll set" \
    -width 20 -height 20
 pack .list -side left
 scrollbar .scroll -command \
    ".list yview"
 pack .scroll -side right -fill y

 if {$argc > 0} {
    set dir [lindex $argv 0]
 } else {
    set dir .
 }
 foreach i [lsort [glob * .*]] {
    .list insert end $i
 }

 bind .list <Double-ButtonPress-1> {
    browse $dir [selection get]
 }
 bind all <Control-c> {destroy .}

 proc browse {dir file} {
    if {$dir != "."} {
        set file $dir/$file
    }
    if {file isdirectory $file] {
        exec browse $file &
    } else {
        if [file isfile $file] {
            exec xedit $file &
        } else{
            error "can't browse $file"
        }
    }
 }

Other Uses For Tcl

  • Data representation:
  1. Store data on disk as Tcl scripts.
  2. To load information, evaluate script.
  • Active objects:
  1. Store scripts in objects.
  2. Evaluate scripts at interesting times to give objects behavior.
  • Communication: send Tcl scripts between applications.
  • Executable content:
  1. Active e-mail messages, Web pages.
  2. Agents.

Status

  • Runs on all UNIX/X platforms.
  • PC/Mac alpha releases: September 1995.
  • Source and documentation freely available.
  • 100,000 developers world-wide?
  • Hundreds of commercial products, free extensions.
  • Newsgroup: comp.lang.tcl.
  • 2 introductory books (Ousterhout, Welch).

Representative Applications

  • Multimedia, groupware.
  • Active e-mail messages.
  • System administration.
  • Testing.
  • Scientific applications: instrument control, simulation, visualization, CAD.
  • Real-time control system for offshore platform.
  • British teletext system.
  • Feature animation at Walt Disney Studios.
  • On-air broadcast control system for NBC.

Popular Extensions

 #!/usr/local/bin/expect
 spawn rlogin [lindex $argv 0]
 expect -re "($|#|%) "
 send "cd [pwd]\r"
 expect -re "($|#|%) "
 send "setenv DISPLAY $env(DISPLAY)\r"
 interact

Popular Extensions, cont'd

  • TclX [L1 ]: general-purpose extensions:
  1. POSIX system calls.
  2. Keyed lists.
  3. File scanning (similar to awk).
  4. Date/time manipulation.
  5. Debugging, help, profiling.
  • Oratcl [L2 ] and Sybtcl: access to commercial databases.
  • Incr tcl [L3 ]: object-oriented programming in Tcl.

Popular Extensions, cont'd

  • Tcl-DP: socket-based remote procedure calls, distributed objects.
  1. Sample server:
 set myId 0
 proc GetId {} {
    global myId
    incr myId
    return $myId
 }
 dp_MakeRPCServer 4545
  1. Sample client:
 set server [dp_MakeRPCClient foo.bar.com 4545]
 dp_rpc $server GetId

Where You Might Use Tcl and Tk

  • Creating graphical user interfaces.
  • Testing.
  • Applications that need scripting or extension facilities.
  • Platform-independent applications.

Drawbacks

  • Must learn new language (substitution rules confusing to some people).
  • Interpreted language has performance limits (but surprisingly high).
  • C interfaces incompatible with Xt, Motif library.

Plans For The Future - see the Tcl Improvement Proposal page for details of the way that changes occur in Tcl today. All future plans for Tcl and Tk improvement occur based solely on the ability of members of the community to design and implement these changes. The changes will appear in the core of Tcl and Tk when community members and the Tcl Core Team come to a mutual agreement on implementation details.


Conclusions

  • High-level programming:
  1. Less to learn.
  2. Build applications more quickly.
  • Universal scripting language:
  1. Extend and modify applications at run-time.
  2. Make many things work together.
  • Use scripts instead of data:
  1. Active objects, executable content.

Tcl + Tk = shell of the 1990's?


An Introduction to Tcl Scripting - Building User Interfaces with Tcl and Tk - Writing Tcl-Based Applications in C