tcl and tk

25
Tcl and Tk Chen Wei, Gingko Studio

Upload: ardith

Post on 05-Feb-2016

138 views

Category:

Documents


5 download

DESCRIPTION

Tcl and Tk. Chen Wei, Gingko Studio. Content. history and purpose Important Features Tcl Syntax Tk Examples. Content. history and purpose Important Features Tcl Syntax Tk Examples. How it started. Codes must be modified when the environment is changed, so a lot of time is wasted. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Tcl  and  Tk

Tcl and Tk

Chen Wei, Gingko Studio

Page 2: Tcl  and  Tk

Content

history and purpose Important Features Tcl Syntax Tk Examples

Page 3: Tcl  and  Tk

Content

history and purpose Important Features Tcl Syntax Tk Examples

Page 4: Tcl  and  Tk

How it started

Codes must be modified when the environment is changed, so a lot of time is wasted.

Find a programming language which can be: 1. Reused 2. Extended 3. Learned quickly

Solution: Component Approach

Page 5: Tcl  and  Tk

What is Tcl?

developed by John Ousterhout

(OH-stir-howt) at UC Berkeley. Tcl stands for “tool command

language” interpreted scripting language; most commonly used to build

graphical interfaces in conjunction with Tk.

Page 6: Tcl  and  Tk

Cont… What is Tcl?

The Tcl interpreter has been ported from UNIX to DOS, Windows, OS/2, NT, and Macintosh environments.

The Tk toolkit has been ported from X Window to Windows and Macintosh.

Page 7: Tcl  and  Tk

What is Tk?

A graphical toolkit that gives one access to many of the graphics primitives of the underlying windowing system, as well as providing access to many types of widgets, such as buttons, scrollbars, menus, and text boxes.

a set of commands and procedures that make it relatively easy to program graphical user interfaces in Tcl.

Tk defines Tcl commands that let you create and manipulate user interfaces.

Page 8: Tcl  and  Tk

What is tclsh and wish?

tclsh is an interpreter which interprets tcl commands

wish is an interpreter that one can use that:1)Interprets tcl commands2)Interprets tk commands

Page 9: Tcl  and  Tk

Content

history and purpose Important Features Tcl Syntax Tk Examples

Page 10: Tcl  and  Tk

Features

Easy to learn Standard systax Graphical interfaces Cross-platform applications Rapid development Extensive and embeddable Flexible integration Free

Page 11: Tcl  and  Tk

Content

history and purpose Important Features Tcl Syntax Tk Examples

Page 12: Tcl  and  Tk

Mike Ang Tcl/Tk

Basics Tcl script =

– Sequence of commands.– Commands separated by newlines, semicolons.

Tcl command =– One or more words separated by white space.– First word is command name, others are arguments.– Returns string result.

Examples:set a 22puts "Hello, World!"

Page 13: Tcl  and  Tk

Mike Ang Tcl/Tk

Tcl: Tool Command Language Simple syntax (similar to sh):

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]

Page 14: Tcl  and  Tk

Mike Ang Tcl/Tk

More On The Tcl Language Rich set of built-in commands:

– Variables, associative arrays, lists.– C-like expressions.– Conditionals, looping:

if "$x < 3" { puts "x is too small"}

– Procedures.– Access to files, subprocesses, network sockets

Page 15: Tcl  and  Tk

Mike Ang Tcl/Tk

More On The Tcl Language Only data representation is zero-terminated

strings:– Easy access from C.– Programs and data interchangeable.

set cmd1 "exec notepad"...eval $cmd1(notepad.exe launches under Windows)

Page 16: Tcl  and  Tk

Example: Factorial

proc fac x {

if {$x<=1} {

return 1

}

expr {$x*[fac [expr $x-1]]}

}

fac 4

Returns: 24

Page 17: Tcl  and  Tk

Content

history and purpose Important Features Tcl Syntax Tk Examples

Page 18: Tcl  and  Tk

This shows a Hello button on the Main window. When you click the button the console window will

display the string "Hello World!" on the console.

button command creates an instance .b button called .hello.

-text string shows the button title. -command string is the command responded to the

button's action. pack command layouts the button on the window.

Hello Worldpack [button .b -text "Hello" -command {puts "Hello World!"}]

Page 19: Tcl  and  Tk
Page 20: Tcl  and  Tk
Page 21: Tcl  and  Tk

Browser Wish Script

scrollbar .h -orient horizontal -command ".list xview"

scrollbar .v -command ".list yview"

listbox .list -selectmode single -width 20 -height 10 \

-setgrid 1 -xscroll ".h set" -yscroll ".v set"

label .label -text "File Selected:" -justify left

entry .e -textvariable fileSelected

Page 22: Tcl  and  Tk

Cont… Browser Wish Script

grid .list -row 0 -column 0 -columnspan 2 -sticky "news" grid .v -row 0 -column 2 -sticky "ns" grid .h -row 1 -column 0 -columnspan 2 -sticky "we" grid .label -row 2 -column 0 grid .e -row 3 -column 0 -columnspan 3 -sticky "we" grid columnconfigure . 0 -weight 1 grid rowconfigure . 0 -weight 1

Page 23: Tcl  and  Tk

Cont… Browser Wish Script

foreach file [glob *] {

.list insert end $file

}

bind .list <ButtonRelease-1> \

{global fileSelected;set fileSelected \[%W get [%W curselection]]}

Page 24: Tcl  and  Tk
Page 25: Tcl  and  Tk

The End