Introduction to Tcl and Tk - PowerPoint PPT Presentation

1 / 23
About This Presentation
Title:

Introduction to Tcl and Tk

Description:

Tk: X11/Windows/Mac toolkit and widgets based on Tcl. Principle ... Create widget with command named after class. button .dlg.quit -text Quit ... – PowerPoint PPT presentation

Number of Views:424
Avg rating:3.0/5.0
Slides: 24
Provided by: dmhwa
Category:

less

Transcript and Presenter's Notes

Title: Introduction to Tcl and Tk


1
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

2
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

3
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

4
Language Philosophy
  • Language classes
  • large application implementation (structure,
    performance important)
  • scripting, extensions
  • interactive commands (structure bad, performance
    not critical)
  • One language cant meet all three needs?
  • Tcl goals
  • simple syntax (for humans)
  • programmable
  • easy to interpret
  • simple interface to C/C procedures

C
Tcl
5
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 b2
  • complex arguments
  • if alt0
  • puts stdout a is negative

6
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

7
Example Factorial
  • proc fac x
  • if xlt1
  • return 1
  • expr xfac expr x-1
  • fac 4
  • Returns 24

8
Embedding Tcl in Applications
Tcl
Application
Init
Parser
Command Loop
Built-In Commands
Application Commands
  • 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

9
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

10
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

11
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

12
Whats 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
13
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

14
Browser Wish Script
  • listbox .list -yscroll .scroll set \
  • -relief raised -width 20 -height 20
  • pack .list -side left
  • scrollbar .scroll -command .list yview
  • pack .scroll -side right -fill y
  • if argc gt 0
  • set dir lindex argv 0
  • else
  • set dir .
  • foreach i exec ls -a dir
  • .list insert end i
  • bind .list ltDouble-Button-1gt
  • browse dir selection get
  • bind .list ltControl-cgt destroy .
  • focus .list

15
Browse Script Cont.
  • proc browse dir file
  • global env
  • if dir ! . set file dir/file
  • if file isdirectory file
  • exec browse file
  • else
  • if file isfile file
  • exec env(EDITOR) file
  • else
  • puts stdout cant browse file
  • Browse is also name of wish script

16
TkSol - Tk-Based Solitaire Program
17
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

18
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

19
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

20
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.

21
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

22
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

23
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
Write a Comment
User Comments (0)
About PowerShow.com