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
- Tcl: embeddable scripting language - Tk: GUI toolkit and widgets based on Tcl.
- Raise the level of X programming: simpler, 5-10x faster application development. - Greater power: more things programmable, applications work together. - Active objects: replace data with scripts.
Outline
Tcl: Tool Command Language
- Typically redone for each application. - Result: weak, quirky. - '''emacs''' and '''csh''' powerful, but can't reuse.
- Interpreter is a C library. - Provides basic features: variables, procedures, etc. - Applications extend with additional features.
Scripting Language Philosophy
- Performance important. - Need structure. - ''Goal: prevent bad things.''
- Performance less important. - Minimum structure: less overhead, easy interchange. - ''Goal: enable good things.''
One language can't meet all needs?
Two-Language Approach
- Minimal syntax: easy to learn and type. - Minimal structure: make things play together. - Simple interfaces to C: extensibility.
Tcl: Tool Command Language
set a 47 -> 47
set b $a -> 47 set b [expr $a+10] -> 57
set b "a is $a" -> a is 47 set b {[expr $a+10]} -> [expr $a+10]
More On The Tcl Language
- Variables, associative arrays, lists. - C-like expressions. - Conditionals, looping: if "$x < 3" { puts "x is too small" } - Procedures. - Access to UNIX files, subprocesses.
- Easy access from C. - 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
- Define new object types in C. - Implement primitive operations as new Tcl commands. - Build complex features with Tcl scripts.
[Nifty graphic missing here showing interaction between a C app and the Tcl interpreter - can anyone help me figure out how to get it in here?]
Extensions
- Network communication, database access, security, ...
[Nifty graphic missing here showing interaction between a C app and the Tcl interpreter and a C extension - can anyone help me figure out how to get it in here?]
The Tk Toolkit
- Too hard to build applications with nice user interfaces.
- C++, object-oriented toolkits. - Only small improvement (10-20%?): must still program at a low level.
- ''Raise the level of programming.'' - Create interfaces by writing Tcl scripts.
Creating User Interfaces With Tk
- Create professional looking widgets. - Arrange widgets. - Bind events to Tcl commands. - Manipulate X selection, focus, window manager, etc.
- Create new widget classes. - Create new geometry managers.
What's A Tk-Based Application?
- New object types. - New widgets.
- Build user interface. - Respond to events.
Wish: Windowing Shell
button .hello -text "Hello, world" -command exit pack .hello
[the two images here won't even show up in my powerpoint window! I really need help with figuring out what went here...]
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
- Store data on disk as Tcl scripts. - To load information, evaluate script.
- Store scripts in objects. - Evaluate scripts at interesting times to give objects behavior.
- Active e-mail messages, Web pages. - Agents.
Status
Representative Applications
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
- POSIX system calls. - Keyed lists. - File scanning (similar to awk). - Date/time manipulation. - Debugging, help, profiling.
- Sample server: set myId 0 proc GetId {} { global myId incr myId return $myId } dp_MakeRPCServer 4545 - Sample client: set server [dp_MakeRPCClient foo.bar.com 4545] dp_rpc $server GetId
Where You Might Use Tcl and Tk
Drawbacks
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
- Less to learn. - Build applications more quickly.
- Extend and modify applications at run-time. - Make many things work together.
- Active objects, executable content.
Tcl + Tk = shell of the future?
An Introduction to Tcl Scripting - Building User Interfaces with Tcl and Tk - Writing Tcl-Based Applications in C