Tcl/Tk: An introduction
D. Kim, K. Kundu, and M. Siegel
November 26, 2002CMSC 631
November 26, 2002
Tcl/Tk: An Introduction 2
Tcl (Tool Control Language) history
Developed in late 1980s by John Ousterhout at UC Berkeley
Created as a single language used to control IC tools, rather than use a different language for each one.
Provides for extensions such as Tk (GUI), [incr Tcl] (OOP), etc.
November 26, 2002
Tcl/Tk: An Introduction 3
8.0 Aug8.0 Aug
Timeline of Tcl
1988 1989 1990 1991 1992 1993 1995 1997 1998
2. Open source distributionsfrom U.C. Berkeley:
• Easy GUIs under Unix• Extensible applications
2. Open source distributionsfrom U.C. Berkeley:
• Easy GUIs under Unix• Extensible applications
3. Tcl enhanced at Sun Microsystems:• Windows, Macintosh ports• Web/Internet support• Java support
3. Tcl enhanced at Sun Microsystems:• Windows, Macintosh ports• Web/Internet support• Java support
4. Scriptics formed:• Evolve and extend Tcl platform• Create development tools
4. Scriptics formed:• Evolve and extend Tcl platform• Create development tools
1. Tcl created as general-purpose command/scripting language by John Ousterhout
1. Tcl created as general-purpose command/scripting language by John Ousterhout
1994 19991996 2000 2001
7. ActiveState introduces Tcl support and services
7. ActiveState introduces Tcl support and services
6.0 Sept6.0 Sept 7.0 Sept7.0 Sept 7.4 July7.4 July 7.6 Oct7.6 Oct 8.1 Apr8.1 Apr
8.2 Aug8.2 Aug
8.3 Feb8.3 Feb
……
Slide courtesy of ActiveState
November 26, 2002
Tcl/Tk: An Introduction 4
Installing Tcl/Tk
Windows/Mac: Latest distribution maintained by ActiveState (ActiveTcl 8.4.1.0). Download at http://www.tcl.tk/
Unix/Linux: Tcl/Tk is included with most Unix/Linux distributions
November 26, 2002
Tcl/Tk: An Introduction 5
3 Ways to Use Tcl/Tk1. tclsh for interactive use
$ tclsh% puts “I am using tclsh”I am using tclsh
2. wish for programs using the Tk package
3. Embed in C program with <tcl.h>
November 26, 2002
Tcl/Tk: An Introduction 6
Basics Tcl script =
Sequence of commands. Tcl command =
One or more words separated by white space.
First word is command name, others are arguments.
Returns string result. Example:
set a 22set b 33
November 26, 2002
Tcl/Tk: An Introduction 7
[ ] and $ substitution
Substitutions: variable substitution: set id 631puts “This class is CMSC $id”
command substitution, evaluated as separate script:set b [expr $id*4]
November 26, 2002
Tcl/Tk: An Introduction 8
Math Evaluation expr command evaluates expressions.
Sample command Resultset b 5 5expr ($b*4)-3 17expr $b <= 2 0
Many other math functions included, such as sin, cos, sqrt, and log.
November 26, 2002
Tcl/Tk: An Introduction 9
Conditional/Looping Statements Like most languages, Tcl supports an if
statement, though the keywords then and else are optional.
For loop: for {set a 0} {$a < 100} {incr a} {
#more code here
}
While loop is also supported
November 26, 2002
Tcl/Tk: An Introduction 10
Tcl lists list are ordered collections of elements any proper list can also be a Tcl
command (eval) concat list list – concatenate lists
concat {a b c} {d e f} a b c d e f join list sep – convert to string with
separatorjoin {a b c} ", " a, b, c
Some list functions: lappend lindex, linsert, llength, lrange
November 26, 2002
Tcl/Tk: An Introduction 11
Tcl Arrays Tcl arrays are 'associative arrays': index
is any string set nicholas(1) 331 set nicholas(2) [expr $nicholas(1) + 300] array names nicholas
Matricies can be “faked” with index notation:
set A(1,1) 10 set A(1,2) 11 array names A
=> 1,1 1,2 (commas included in names!)
November 26, 2002
Tcl/Tk: An Introduction 12
Regular Expressions Tcl has full support for regular
expression pattern matching and substitution
regexp command for matching, places matched chars into variable specified
regsub for substitution
November 26, 2002
Tcl/Tk: An Introduction 13
Tk : An Introduction Tk is a Toolkit for programmable
user interfaces. Tk provides a set of Tcl commands
that create and manipulate widgets. John Ousterhout began work on Tk
in late 1988; finished in 1990. Tk's GUI facilities were both very
simple and very powerful.
November 26, 2002
Tcl/Tk: An Introduction 14
Tk : Widgets
A widget is window in a GUI with particular appearance and behavior.
Widget types include buttons, scrollbars, menus, and text windows.
Tk also has a general purpose drawing widget called a canvas that lets you create lightweight items such as lines, boxes and bitmaps.
November 26, 2002
Tcl/Tk: An Introduction 15
Tk : Widgets (Con’t)
Tk widgets organized in a hierarchy. - children windows inside a parent window
Parent widgets use frame widgets to lay the children windows out
Can create complex windowing schemes using Tk widget hierarchy.
November 26, 2002
Tcl/Tk: An Introduction 16
Tk : Geometry Manager Widgets are under the control of
geometry manager that controls their size and location on the screen
Until the GM learns about a widget, it is not mapped onto a screen
Types of Geometry Managers: The Pack GM The Grid GM The Place GM
November 26, 2002
Tcl/Tk: An Introduction 17
Tk : A Small Example
#!/usr/local/bin/wish
button .hello -text Hello -command {puts stdout "Hello World!"}
pack .hello -padx 20 -pady 10
(code courtesy of Practical Programming in Tcl and Tk by Brent B. Welch)
November 26, 2002
Tcl/Tk: An Introduction 18
Tk : Events
Tk-based application has event-driven control flow.
Usually Tk widgets handle most events automatically.
For specialized behavior, bind command is used.
November 26, 2002
Tcl/Tk: An Introduction 19
Tk : Events (Con’t) Examples of events include mouse motion,
mouse clicks, keystrokes, window resizing, window destruction
Virtual events like cut and paste are also possible.
Event bindings grouped into classes called bindtags which are associated with a class.
Focusing on windows helps switch bindtags.
November 26, 2002
Tcl/Tk: An Introduction 20
Example – diff command
#!/usr/bin/wish
# Description :# this program will give the user a graphical interface to the Unix# command "diff". The window will allow the user to specify a pair# of files to check for differences and a few options, as well as# colorizing the output appropriately
# title
wm title . tkdiff
November 26, 2002
Tcl/Tk: An Introduction 21
Example: (Con’t)
# frame for the first file
frame .first -borderwidth 1label .first.name1 -text "Filename 1:" -foreground redentry .first.ent1 -width 68 -relief sunken \ -textvariable name1pack .first.name1 -side leftpack .first.ent1 -side left -fill x -expand true
November 26, 2002
Tcl/Tk: An Introduction 22
Example: (Con’t)
# frame for the second file
frame .second -borderwidth 0label .second.name2 -text "Filename 2:" -foreground blueentry .second.ent2 -width 68 -relief sunken \ -textvariable name2pack .second.name2 -side leftpack .second.ent2 –side left -fill x -expand truepack .first .second -fill both
November 26, 2002
Tcl/Tk: An Introduction 23
Example: (Con’t)
# frame for check boxes
set third [frame .third -borderwidth 2]checkbutton $third.1 -text "Ignore Case Changes" \ -variable cFlagcheckbutton $third.2 -text "Ignore Whitespace Diffs" \ -variable wFlagpack $third.1 $third.2 -side leftpack $third -fill x
November 26, 2002
Tcl/Tk: An Introduction 24
Example: (Con’t)
# frame for command buttons
set fourth [frame .four -borderwidth 2]button $fourth.1 -text Quit -command Exit$fourth.1 config -activebackground redbutton $fourth.2 -text Go -command Run$fourth.2 config -activebackground greenbutton $fourth.3 -text Clear -command Clear$fourth.3 config -activebackground bluepack $fourth.1 $fourth.2 $fourth.3 -side leftpack $fourth
November 26, 2002
Tcl/Tk: An Introduction 25
Example: (Con’t)
# frame for display area
frame .msg -borderwidth 2set box [text .msg.box -width 60 -height 10 \ -borderwidth 1 -relief raised -setgrid true \ -yscrollcommand {.msg.yscroll set} \ -xscrollcommand {.msg.xscroll set}]scrollbar .msg.yscroll -command {.msg.box yview} \ -orient verticalscrollbar .msg.xscroll -command {.msg.box xview} \ -orient horizontalpack .msg.yscroll -side right -fill ypack .msg.xscroll -side bottom -fill xpack .msg.box -side left -fill both -expand truepack .msg -side top -fill both -expand true
November 26, 2002
Tcl/Tk: An Introduction 26
Example: (Con’t)
# for change colars
.msg.box tag configure TagA -foreground red
.msg.box tag configure TagB -foreground blue
# when user click exit buttonproc Exit {} { set picked [tk_messageBox -type yesno \ -message "Really Quit?" \ -default no \ -icon question] if {$picked == "yes"} { exit }}
November 26, 2002
Tcl/Tk: An Introduction 27
Example: (Con’t)
# when user click go buttonproc Run {} { global box name1 name2 cFlag wFlag input if {$wFlag == 1 && $cFlag == 1} { set cmd "diff $name1 $name2 -c -w" } elseif {$wFlag == 1 && $cFlag == 0} { set cmd "diff $name1 $name2 -w" } elseif {$wFlag == 0 && $cFlag == 1} { set cmd "diff $name1 $name2 -c" } else { set cmd "diff $name1 $name2“ } if [catch {open "|$cmd |& cat"} input] { $box insert end $input\n } else { fileevent $input readable Log $box insert end $cmd\n }}
November 26, 2002
Tcl/Tk: An Introduction 28
Example: (Con’t)
# function for write a log to display areaproc Log {} { global input box if [eof $input] { Stop } else { gets $input line if [regexp "^<" $line matches] { $box insert end $line\n TagA } elseif [regexp "^>" $line matches] { $box insert end $line\n TagB } else { $box insert end $line\n } $box see end }}
November 26, 2002
Tcl/Tk: An Introduction 29
Example: (Con’t)
# when input file is end of fileproc Stop {} { global input box catch {close $input}}
# clear the display areaproc Clear {} { global box $box delete 1.0 end}
November 26, 2002
Tcl/Tk: An Introduction 30
Example: Run
November 26, 2002
Tcl/Tk: An Introduction 31
Example: Output