introduction to tcl and tk outline –the tcl language –the tk toolkit –tk applications...
Post on 20-Dec-2015
251 views
TRANSCRIPT
Introduction to Tcl and Tk
Outline– The Tcl Language
– The Tk Toolkit
– Tk Applications
– Composing Applications
– Status and Conclusions
Goal– Understand basics of Tcl and Tk
– Understand Tk/Tcl philosophy
Reading– Ch. 5-9, Practical Programming in Tcl and Tk
Overview
• What– Tcl: embeddable scripting language
– Tk: X11/Windows/Mac toolkit and widgets based on Tcl
• Principle– single interpretive language controls all aspects of all
interactive applications
» function
» interface
» composition of pieces
» communication between applications
• Results– higher- level graphics programming - simpler, 10X faster
– greater power - more programmable, programs work together
Tcl: Tool Command Language
• Problem– interactive programs need command languages
– traditionally redone for each application
– result: weak, quirky
– Emacs and csh nice, but cannot reuse
• Solution: Tcl– command language = embeddable C library
– powerful features: procedures, variables, lists, expressions, loops, etc.
– extensible by applications
Language Philosophy
• Language classes– large application implementation (structure,
performance important)
– scripting, extensions
– interactive commands (structure bad, performance not critical)
• One language can’t meet all three needs?
• Tcl goals– simple syntax (for humans)
– programmable
– easy to interpret
– simple interface to C/C++ procedures
C
Tcl
Tcl Syntax
• Basic syntax like shells– words separated by spaces:
cmd arg arg arg . . .
– commands separated by newlines, semicolons
– commands return string results
• Simple substitution rules– variables:
set a $b
– command results:
set a [expr $b+2]
– complex arguments:
if $a<0 {
puts stdout “a is negative”
}
More on the Tcl Language
• Rich set of built-in commands– variables, associative arrays, lists
– arithmetic expressions
– conditionals, looping
– procedures
– access to files, system commands
• Only data type is string– easy access from C/C++
– programs and data are interchangeable
Example: Factorial
proc fac x {
if {$x<=1} {
return 1
}
expr {$x*[fac [expr $x-1]]}
}
fac 4
Returns: 24
Embedding Tcl in Applications
Parser
Init
Command Loop
• Application generates Tcl scripts
• Tcl parses scripts, calls command procedures with argc, argv
• Application extends built-in command set– define new object types in C
– implement primitive operations on objects as new Tcl commands
– build complex features with Tcl scripts
Tcl Application
Built-In Commands Application Commands
The Tk Toolkit
• Problem– too hard to build applications with nice user interfaces
• Wrong Solution– C++, object-oriented toolkits
– only 10-20% improvement, must still program at low level
• Right Solution– raise the level of GUI programming
– create interfaces by writing Tcl scripts
Creating Interfaces with Tk
• Widgets/windows have path names
.dlg.quit
• Create widget with command named after class
button .dlg.quit -text Quit \
-foreground red -command exit
• Tell geometry manager where to display widget
place .dlg.quit -x 0 -y 0
pack .dlg.quit -side bottom
Other Tk Features
• Manipulate widgets with widget commands
.dlg.quit flash
.dlg.quit configure -relief sunken
• Use Tcl for interconnection– buttons, menu entries invoke Tcl commands
– scrollbars and listboxes communicate with Tcl
– can define new event bindings in Tcl
– selection, focus accessible via Tcl
• Tk also provides C interfaces– create new widget classes
– create new geometry managers
What’s a Tk-Based Application?
1. The Tcl interpreter
2. The Tk toolkit
3. Application-specific C code (primitives!)– new object types
– new widgets
4. Tcl scripts (compose primitives)– build GUI
– respond to events
Tcl commands
Wish: Simplest Tk Application
• Wish - windowing shell
• No C code except command-line reader
• Can build many apps as wish scripts– Hello, world:
label .hello \
-text “Hello, world”
.pack .hello
– simple directory browser
» 30 lines of Tk/Tcl
Browser Wish Script
listbox .list -yscroll “.scroll set” \-relief raised -width 20 -height 20
pack .list -side leftscrollbar .scroll -command “.list yview”pack .scroll -side right -fill y
if {$argc > 0} {set dir [lindex $argv 0]
} else {set dir “.”
}foreach i [exec ls -a $dir] {
.list insert end $i}bind .list <Double-Button-1> {
browse $dir [selection get]}bind .list <Control-c> {destroy .}focus .list
Browse Script Cont.
proc browse {dir file} {global envif {$dir != “.”} {set file $dir/$file}if [file isdirectory $file] {
exec browse $file &} else {
if [file isfile $file] {exec $env(EDITOR) $file &
} else {puts stdout “can’t browse $file”
}}
}
• Browse is also name of wish script
TkSol - Tk-Based Solitaire Program
TkSol Statistics
• Code– 1635 lines of Tk/Tcl code
– 4 card down bitmaps
– 52 card up bitmaps (4 suits of 13 cards each)
– almost all time in bitmap creation
• Comparison to MS Solitaire– simpler end game
– has auto-play mode
– slower startup
– slower deal
– similar interactive speed
TkMines Statistics
• Code– 1178 lines of Tk/Tcl code
– uses a few signal and time commands from TclX extension, are not really needed
– several small bitmaps for squares
• Comparison to MS Mines– simpler score file
– same look-and-feel
– slower startup
– similar interactive performance
Composing Applications
• Problem– only communication between applications is via selection
» OLE on MS Windows provides object selection
– result: monolithic programs
• Solution - send command– send appName command
– any Tk application can invoke anything in any other Tk application: interface or actions
– result: powerful communication
– result: big security hole
Composing Applications, cont.
• Examples– debugger - sends command to editor to highlight line of
execution
– UI editor - sends commands to modify interface of live application
– multimedia - send record, play commands to audio and video applications
– spreadsheets - cell sends commands to database to fetch current value
• Revolutionary results– build complex systems as collections of specialized but
reusable hypertools
– easy to create active objects: embeddable Tcl commands. Hypertext, hypermedia is easy.
Status
• Source and documentation freely available– developed at UC Berkeley
– now maintained and developed at Sun Labs
– code redistributable in commercial applications
– runs on UNIX, Windows, Mac
• Large user/developer community– 50,000+ worldwide
– comp.lang.tcl newsgroup
– many commercial products
– several books
• Many extensions available– Tk is just one extension
Drawbacks
• Must learn new language– substitution can be confusing
• Interpreted language has performance limits– want a compiler for some applications
– want to avoid C programming
– dynamic compiler will provide at least 10x speedup
• Competition– Visual Basic
– AppleScript
– Java/JavaScript
Conclusions
• High-level programming– less to learn
– easy to learn
– build applications very quickly
• One interpretive language for everything– extend and modify applications at run-time
– make many things work together
• Web programming/agent language?– platform independent
– interpreted
– easily embedded
– secure