Beruflich Dokumente
Kultur Dokumente
Lee Bernhard
Scriptics Corporation
2593 Coast Ave., 2nd Floor
Mountain View, CA 94043
lfb@scriptics.com
Other Courses
Effective Tcl/Tk Programming
2-day Advanced Topic
Object-Oriented Programming with [incr Tcl]
2-day Advanced Topic
Automated Testing and System Administration with Expect
2-day Advanced Topic
Schedule
Part 1:
Part 2:
What is Tcl/Tk?
Getting Started
Canvas Widget
Extensions
What is Tcl/Tk?
What is Tcl/Tk?
Tk
(GUI toolkit)
Tcl
(scripting language)
What is Tcl/Tk?
Production-quality
graphical user interfaces
Web-based applications:
Servlets (active server pages)
Applets (client-side applications)
CGI scripts
What is Tcl/Tk?
Cross-Platform Support
UNIX
Windows 95/98/NT
Macintosh
What is Tcl/Tk?
Add-On Packages
Tcl was designed to be extensible
[incr Tcl]
BLT
Tk
Tcl
...
wish
Popular Extensions:
BLT ........................................ plotting, tabnotebook, hierarchical list, and other widgets
[incr Tcl]................................. adds object-oriented support to the Tcl language
Expect ................................... controls command-based tools (legacy programs)
TclX ....................................... lots of extra commands for POSIX functions, looping, etc.
Togl ........................................ OpenGL widget for Tk (3D graphics)
OraTcl / SybTcl...................... interface with Oracle/Sybase databases
Scotty .................................... support for network management applications
Contributed Sources Archive: http://www.neosoft.com/tcl
7
What is Tcl/Tk?
Source-Navigator
from Cygnus Solutions
http://www.cygnus.com/sn
What is Tcl/Tk?
Commercial Support
Scriptics Corp. (www.scriptics.com)
Support Contracts
Consulting
Training
NeoSoft (www.neosoft.com)
Consulting
Training
Many others listed at: www.scriptics.com/resource/commercial
What is Tcl/Tk?
Installing Tcl/Tk
TclPro from Scriptics Corp.
Platforms:
Windows 95/NT, Solaris, HPUX, Linux
SGI IRIX
Platforms:
www.scriptics.com
www.linuxcentral.com
10
What is Tcl/Tk?
Other Resources
Scriptics Corp., www.scriptics.com
Start-up company commercializing Tcl/Tk
Web site has:
www.neosoft.com/tcl
Newsgroups
comp.lang.tcl
comp.lang.tcl.announce
Books
www.scriptics.com/resource/doc/books
11
Schedule
Part 1:
Part 2:
What is Tcl/Tk?
Getting Started
Canvas Widget
Extensions
12
Getting Started
Programs:
tclsh .... Tcl language shell
wish....... Tcl language shell with Tk widgets
Leave tclsh
Leave wish
13
$ tclsh
% info commands
tell open eof pwd glob list pid exec time
unknown eval lrange lsearch gets case lappend
proc break llength auto_execok return linsert
error catch info split array if auto_mkindex
...
% exit
$ wish
% info commands
tell open eof pwd glob list listbox pid exec
pack time unknown eval lrange checkbutton
lsearch gets canvas case lappend proc place
bind break llength return linsert error ...
% exit
Getting Started
Hello World
Main window named "."
created automatically by wish
button .b
Start up wish
$ wish
Make a button
Notes:
All widget names start with ".", e.g.: .b
.b2
.helloWorld
Getting Started
Tcl Syntax
# comment
command arg arg...
command arg \
arg arg...
command arg arg... ; command arg arg...
command arg arg... ;# inline comment
Examples:
Quotes keep words together
as one argument
arg
arg
arg
arg
15
Getting Started
Command Scripts
file: examples/hello.tcl
# --------------------------------------------# EXAMPLE: simple "Hello World!" application
# --------------------------------------------# COURSE: Building Applications with Tcl/Tk
# AUTHOR: Michael J. McLennan
# =============================================
button .b -text "Hello World!" -command exit
pack .b -padx 4 -pady 4
$ wish
% source hello.tcl
% exit
EDIT hello.tcl to say
"Hello Universe!"
$ wish
% source hello.tcl
% button .q -text "Quit"
.q
% pack .q
%
16
Getting Started
17
Getting Started
Configuration Options
% button .b -text "Hello World!"
.b
% pack .b
% .b configure
{-activebackground activeBackground Foreground #ececec #ececec} {-activeforeground
activeForeground Background Black Black} {-anchor anchor Anchor center center} {-background
background Background #d9d9d9 #d9d9d9} {-bd borderWidth} {-bg background} {-bitmap bitmap Bitmap
{} {}} {-borderwidth borderWidth BorderWidth 2 2} {-command command Command {} {}} {-cursor
cursor Cursor {} {}} {-disabledforeground disabledForeground DisabledForeground #a3a3a3
#a3a3a3} {-fg foreground} {-font font Font -Adobe-Helvetica-Bold-R-Normal--*-120-*-*-*-*-*-* Adobe-Helvetica-Bold-R-Normal--*-120-*-*-*-*-*-*} {-foreground foreground Foreground Black
Black} {-height height Height 0 0} {-highlightbackground highlightBackground
HighlightBackground #d9d9d9 #d9d9d9} {-highlightcolor highlightColor HighlightColor Black
Black} {-highlightthickness highlightThickness HighlightThickness 2 2} ...
% .b configure -background
-background background Background #d9d9d9 #d9d9d9
% .b cget -background
#d9d9d9
% .b configure -background green
% .b configure -background blue -foreground white
18
Getting Started
Anatomy of a Tk Widget
file: examples/label.tcl
# ----------------------------------------------------------# EXAMPLE: explore common configuration options
# ----------------------------------------------------------# COURSE: Building Applications with Tcl/Tk
# ===========================================================
label .x -text "Red Alert" -width 12 -height 2 -anchor c \
-borderwidth 2 -relief groove -padx 2 -pady 2
pack .x -padx 10 -pady 10
19
Getting Started
Colors
HINT:
Use these to select
colors:
xcolors
xco
xcoloredit
showrgb
-foreground color
-background color
Color values:
color name........red, tomato, salmon, etc.
RGB value ........#RRGGBB
Try this:
.x configure -background red -foreground white
.x configure -background #00ff00
.x configure -bg tomato -fg black
20
Getting Started
Text
-text string
-justify left/center/right
-wraplength size
Screen Distances:
3 .............3 pixels
5p...........5 points (5 1/72")
4.2i ......4.2 inches
10m.........10 mm
3.1c ......3.1 cm
Try this:
.x
.x
.x
.x
21
configure
configure
configure
configure
Getting Started
Font
HINT:
Use these to select
fonts:
xfontsel
xlsfonts
-font xFontName
foundry
family
weight slant
set width
-adobe-helvetica-bold-o-normal--*-140-*-*-*-*-*-*
Try this:
.x configure -font fixed
.x configure -font -*-courier-bold-o-normal--*-140-*
.x configure -font {Helvetica 16 bold}
22
Schedule
Part 1:
Part 2:
What is Tcl/Tk?
Getting Started
Canvas Widget
Extensions
23
Variables
set x "some value"
Assign this value
Variable name
set x
unset x
puts "$new_val0!"
puts "$$x"
"$some value"
24
puts "123$middle789"
puts "123${middle}789"
x
some value
25
Handling Math
Pythagorean Theorem:
$
%
3
%
4
%
tclsh
set a 3
set b 4
c =
a +b
26
x = x + 1;
set x [expr $x + 1]
x += 2;
incr x 2
set x [expr $x + 2]
y = sin(2*x/(x-1))
while {condition} {
body
}
if {condition} {
body
} elseif {condition} {
body
} else {
body
}
switch value {
val1 { body }
val2 { body }
27
default { body }
If present, must
be last switch value
break
continue
Try this:
$ cd /usr/local/tcl-tk/course/examples
$ tclsh math1.tcl
0 0.0 1.0
1 0.841471 0.540302
2 0.909297 -0.416147
3 0.14112 -0.989992
4 -0.756802 -0.653644
5 -0.958924 0.283662
6 -0.279415 0.96017
7 0.656987 0.753902
8 0.989358 -0.1455
9 0.412118 -0.91113
28
Formatted Output
file: examples/math2.tcl
puts "
x
puts "----------
sin(x)
----------
cos(x)"
----------"
fixed-point
number
Try this:
$ tclsh math2.tcl
x
sin(x)
---------- ---------0.000
0.000
1.000
0.841
2.000
0.909
3.000
0.141
4.000
-0.757
...
...
29
Format specifier:
%10.3f
3 digits after
decimal point
cos(x)
---------1.000
0.540
-0.416
-0.990
-0.654
...
10 spaces wide
RIGHT:
body
Missing space
if {$x > 0}{
puts "positive"
}
if {$x > 0} {
puts "positive"
}
Use braces!
if {$x > 0} {
puts "positive"
}
else {
puts "negative"
}
if {$x > 0} {
puts "positive"
} else {
puts "negative"
}
30
Two commands
Schedule
Part 1:
Part 2:
What is Tcl/Tk?
Getting Started
Canvas Widget
Extensions
31
set count 0
label .show -textvariable count
pack .show
button .inc -text "Increment" -command "incr count"
pack .inc
button .dec -text "Decrement" -command "incr count -1"
pack .dec
32
Quoting
Tcl also supports {} quote characters...
puts "Hello World!"
puts {Hello World!}
puts "Hello
World!"
puts {Hello
World!}
33
i
i
i
i
34
=
=
=
=
1
2
0
0
set i 0
button .braces -text "incr with {}" \
-command {incr i; puts "i = $i"}
pack .braces
button .quotes -text "incr with \"\"" \
-command "incr i; puts {i = $i}"
pack .quotes
Click!
incr i; puts {i = 0}
Rule of Thumb:
Use " " for text strings
Use { } for code fragments
Example:
$ wish
% set count 0
{ } around code fragment
0
% button .b -text "Increase" -command {incr count; puts "count = $count"}
.b
" " for text strings
% pack .b
" " for text strings
36
image1
set imh image1
37
[...] execute
this string as a
command, and
put the result in
its place
Try it out:
$ cd /usr/local/tcl-tk/course/examples
$ wish hello2.tcl
image1
label .im -image image1
38
Checkbuttons
file: examples/checkbutton.tcl
set im1 [image create photo -file loki2.gif]
set im2 [image create photo -file loki.gif]
label .im
pack .im
checkbutton .cb -text "Enlarge" \
-variable imageVal -onvalue $im1 -offvalue $im2 \
-command {.im configure -image $imageVal}
pack .cb -padx 4 -pady 4
.cb select
.cb invoke
39
"on" state:
"image1" imageVal
"off" state:
"image2" imageVal
file: checkbutton.tcl
"image1"
or
"image2"
40
Radiobuttons
file: examples/radiobutton.tcl
set im1 [image create photo -file earth.gif]
set im2 [image create photo -file saturn.gif]
set im3 [image create photo -file loki.gif]
label .im
pack .im
radiobutton .earth -text "Earth" \
-variable imageVal -value $im1 \
-command {.im configure -image $im1}
pack .earth -padx 4
radiobutton .saturn -text "Saturn" \
-variable imageVal -value $im2 \
-command {.im configure -image $im2}
pack .saturn -padx 4
radiobutton .loki -text "Loki" \
-variable imageVal -value $im3 \
-command {.im configure -image $im3}
pack .loki -padx 4
.earth invoke
41
Text Entries
file: examples/entry.tcl
label .im -image [image create photo -file earth.gif]
pack .im
entry .entry
pack .entry -padx 4 -pady 4
button .load -text "Load Image" -command {
set file [.entry get]
set im1 [image create photo -file $file]
.im configure -image $im1
}
pack .load -padx 4 -pady 4
Try it out:
type in one of: earth.gif, saturn.gif or loki.gif
click on "Load Image"
42
file: entry.tcl
label .im -image [image create photo -file earth.gif]
pack .im
entry .entry
pack .entry -padx 4 -pady 4
button .load -text "Load Image" -command {
set file [.entry get]
set im1 [image create photo -file $file]
.im configure -image $im1
}
pack .load -padx 4 -pady 4
43
Procedures
Instead of writing code in-line like this:
button .load -text "Load Image" -command {
set file [.entry get]
set im1 [image create photo -file $file]
.im configure -image $im1
}
You can define a procedure like this...
Procedure name
Argument list
proc load_image {fileName} {
set im1 [image create photo -file $fileName]
.im configure -image $im1
}
And use it like this...
load_image earth.gif
or
button .earth -command {load_image earth.gif}
44
45
"global" command
provides access to
variables at the global
scope
Global
Scope
Local
Scope
Listboxes
file: examples/listbox.tcl
proc load_image {file} {
set imh [.im cget -image]
image delete $imh
set imh [image create photo -file $file]
.im configure -image $imh
}
label .im -image [image create photo -file earth.gif]
pack .im
listbox .lbox -width 15 -height 5
pack .lbox -padx 4 -pady 4
.lbox insert 0 earth.gif saturn.gif loki.gif
.lbox selection set 0
button .load -text "Load Image" -command {
set index [.lbox curselection]
load_image [.lbox get $index]
}
pack .load -padx 4 -pady 4
47
Schedule
Part 1:
Part 2:
What is Tcl/Tk?
Getting Started
Canvas Widget
Extensions
48
X Server
Network
...
X server
...
Window
Manager
Tcl/Tk
Application
button .b -text "Hello World!" \
-command exit
pack .b -padx 4 -pady 4
X server:
Window Manager:
Tcl/Tk Application:
draws widgets
handles iconify/deiconify
responds to events
handles move/resize
49
...click!
Modfiiers:
Double
Triple
Control
Shift
Lock
Alt
Meta
B1
B2
B3
B4
B5
Type of event:
M1
M2
M3
M4
M5
ButtonPress
KeyPress
Enter
Motion
etc.
Examples:
bind .b <Double-ButtonPress-1> {puts "double-click!"}
bind .b <Control-KeyPress-d> {puts "key: ^D"}
bind .b <B1-Motion> {puts "holding down B1 and moving!"}
51
Example: Listbox
file: examples/listbox2.tcl
...
label .im -image [image create photo -file earth.gif]
pack .im
listbox .lbox -width 15 -height 5
pack .lbox -padx 4 -pady 4
bind .lbox <Double-ButtonPress-1> {
.load flash
.load invoke
}
.lbox insert 0 earth.gif saturn.gif loki.gif
.lbox selection set 0
button .load -text "Load Image" -command {
set index [.lbox curselection]
load_image [.lbox get $index]
}
pack .load -padx 4 -pady 4
52
Examples:
bind .b <ButtonPress> {puts "button: %b"}
bind .b <B1-Motion> {puts "drag: %x %y"}
53
file: examples/bind.tcl
label .value -width 30 -height 3
pack .value
bind .value <Motion> {.value configure -text "Position = %x %y"}
bind .value <KeyPress> {.value configure -text "Key = %K"}
focus .value
focus .value
Try this:
% wish bind.tcl
...move mouse around and press various keys...
54
55
Schedule
Part 1:
Part 2:
What is Tcl/Tk?
Getting Started
Canvas Widget
Extensions
56
Canvas Widget
Interactive Displays
You can use the canvas widget to build interactive drawings and maps
57
Canvas Widget
Canvas Items
Add "items" to the canvas to create drawings
continued...
58
Canvas Widget
Canvas Items
continued...
59
Canvas Widget
Drawn Last
canvas .c
pack .c
Create Items:
set id [.c create oval \
5 5 10 20 -outline black \
-fill yellow]
Manipulate Items:
.c itemconfigure $id -fill red
.c raise $id
.c lower $id
.c move $id 5 -5
.c scale $id 0 0 2 2
.c delete $id
Drawn First
60
Canvas Widget
Drawn Last
canvas .c
pack .c
...
foreach id [.c find enclosed 20 20 80 110] {
.c itemconfigure $id -fill red
}
Drawn First
61
Canvas Widget
file: examples/canvas1.tcl
canvas .pad
pack .pad
bind .pad <ButtonPress-1> {add_point %x %y}
bind .pad <B1-Motion> {add_point %x %y}
proc add_point {x y} {
.pad create rectangle [expr $x-4] [expr $y-4] \
[expr $x+4] [expr $y+4] -fill blue -outline ""
}
62
Canvas Widget
Rubberbanding
file: examples/canvas2.tcl
bind .pad <ButtonPress-3> {select_start %x %y}
bind .pad <B3-Motion> {select_move %x %y}
bind .pad <ButtonRelease-3> {select_end %x %y}
proc select_start {x y} {
global x0 y0
set x0 $x
set y0 $y
}
proc select_move {x y} {
global x0 y0
.pad delete rubberbox
.pad create rectangle $x0 $y0 $x $y -outline black -tags rubberbox
}
proc select_end {x y} {
global x0 y0
"rubberbox" is the
.pad delete rubberbox
symbolic name for the
foreach item [.pad find overlapping $x0 $y0 $x $y] {
selection rectangle
.pad itemconfigure $item -fill red
}
}
63
Schedule
Part 1:
Part 2:
What is Tcl/Tk?
Getting Started
Canvas Widget
Extensions
64
Extensions
Extensions
Each extension adds new commands
$ tclsh
% info commands
tell subst open eof pwd glob list pid exec time
unknown eval lrange lsearch gets ...
Tcl
vanilla tclsh
$ wish
% info commands
tell subst open eof pwd glob list pid exec time
unknown eval lrange lsearch gets ...
button checkbutton radiobutton pack place ...
Tk
Tcl
vanilla wish
Tk
$ itkwish
% info commands
tell subst open eof pwd glob list pid exec time
unknown eval lrange lsearch gets ...
button checkbutton radiobutton pack place ...
class namespace import delete ...
[incr Tcl]
Tcl
itkwish
Tk
[incr Tcl]
Tcl
bigwish
65
BLT
$ bigwish
% info commands
tell subst open eof pwd glob list pid exec time
unknown eval lrange lsearch gets ...
button checkbutton radiobutton pack place ...
class namespace import delete ...
graph barchart table bgexec ...
Extensions
set
set
set
set
set
class Pile {
private variable
private variable
private variable
private variable
private variable
PileInfo($name-canvas) ""
PileInfo($name-x) 0
PileInfo($name-y) 0
PileInfo($name-drawcmd) $drawCmd
PileInfo($name-cards) $cardlist
canvas ""
x 0
y 0
drawcmd ""
cardlist {}
66
Extensions
graph
barchart
hierbox
67
table
Extensions
ftp
expect
68
ftp
Extensions
Tk
Tcl
mywish
Command handler
(C procedure)
Tcl_CreateCommand()
Tcl
strings
69
strings
More Information
John K. Ousterhout,
Tcl and the Tk Toolkit,
Addison-Wesley, 1994
(ISBN 0-201-63337-X)
Brent B. Welch,
Practical Programming in
Tcl and Tk, 2d ed.,
Prentice-Hall, 1997
(ISBN 0-13-616830-2)
Don Libes,
Exploring Expect,
OReilly, 1994
(ISBN 1-56592-090-2)
Mark Harrison,
Michael McLennan,
Effective Tcl/Tk
Programming,
Addison-Wesley, 1997
Eric Johnson,
Graphical Applications
with Tcl and Tk
M&T Books
Training: http://www.scriptics.com/training
Resource Center: http://www.scriptics.com/resource
70