1 / 23

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. Introduction to Tcl and Tk. What

una
Download Presentation

Introduction to Tcl and Tk

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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 Introduction to Tcl and Tk

  2. 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 Overview

  3. 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 Tcl: Tool Command Language

  4. 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 Language Philosophy C Tcl

  5. 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” } Tcl Syntax

  6. 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 More on the Tcl Language

  7. proc fac x { if {$x<=1} { return 1 } expr {$x*[fac [expr $x-1]]} } fac 4 Returns: 24 Example: Factorial

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

  9. 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 The Tk Toolkit

  10. 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 Creating Interfaces with Tk

  11. 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 Other Tk Features

  12. 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 What’s a Tk-Based Application? Tcl commands

  13. 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 Wish: Simplest Tk Application

  14. 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 > 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 Browser Wish Script

  15. 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 “can’t browse $file” } } } Browse is also name of wish script Browse Script Cont.

  16. TkSol - Tk-Based Solitaire Program

  17. 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 TkSol Statistics

  18. 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 TkMines Statistics

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

  20. 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. Composing Applications, cont.

  21. 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 Status

  22. 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 Drawbacks

  23. 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 Conclusions

More Related