Programming Using Tcl/Tk

Slides:



Advertisements
Similar presentations
What is a Computer Program? For a computer to be able to do anything (multiply, play a song, run a word processor), it must be given the instructions.
Advertisements

PHP I.
Introducing JavaScript
JavaScript FaaDoOEngineers.com FaaDoOEngineers.com.
1 CSC 551: Web Programming Spring 2004 client-side programming with JavaScript  scripts vs. programs  JavaScript vs. JScript vs. VBScript  common tasks.
Introduction To Java Objectives For Today â Introduction To Java â The Java Platform & The (JVM) Java Virtual Machine â Core Java (API) Application Programming.
The Web Warrior Guide to Web Design Technologies
Programming Using Tcl/Tk These slides are based upon u several Tcl/Tk text books u material byDr. Ernest J. Friedman-Hill.
Browsers and Servers CGI Processing Model ( Common Gateway Interface ) © Norman White, 2013.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 14 Web Database Programming Using PHP.
CS Lecture 03 Outline Sed and awk from previous lecture Writing simple bash script Assignment 1 discussion 1CS 311 Operating SystemsLecture 03.
Working with JavaScript. 2 Objectives Introducing JavaScript Inserting JavaScript into a Web Page File Writing Output to the Web Page Working with Variables.
©2004 Brooks/Cole Chapter 1: Getting Started Sections Covered: 1.1Introduction to Programming 1.2Constructing a Java Program 1.3The print() and println()
Introduction to Tcl and Tk Outline –The Tcl Language –The Tk Toolkit –Tk Applications –Composing Applications –Status and Conclusions Goal –Understand.
Programming for Image Processing/Analysis and Visualization using The Visualization Toolkit Week 2: An Introduction to Tcl Xenios Papademetris
XP 1 Working with JavaScript Creating a Programmable Web Page for North Pole Novelties Tutorial 10.
Writing Tcl Scripts Outline Goal Reading Syntax Data Types
About the Presentations The presentations cover the objectives found in the opening of each chapter. All chapter objectives are listed in the beginning.
XP Tutorial 1 New Perspectives on JavaScript, Comprehensive1 Introducing JavaScript Hiding Addresses from Spammers.
Slide 1 of 9 Presenting 24x7 Scheduler The art of computer automation Press PageDown key or click to advance.
8/17/2015CS346 PHP1 Module 1 Introduction to PHP.
An Overview of Tcl and Tk John Ousterhout Sun Microsystems Laboratories Tcl/Tk Tutorial, Part I.
INTRODUCTION TO WEB DATABASE PROGRAMMING
1 Programming Concepts Module Code : CMV6107 Class Contact Hours: 45 hours (Lecture 15 hours) (Laboratory/Tutorial 30 hours) Module Value: 1 Textbook:
FALL 2005CSI 4118 – UNIVERSITY OF OTTAWA1 Part 4 Web technologies: HTTP, CGI, PHP,Java applets)
CS110/CS119 Introduction to Computing (Java)
Basics of Web Databases With the advent of Web database technology, Web pages are no longer static, but dynamic with connection to a back-end database.
Topics Introduction Hardware and Software How Computers Store Data
INTERNET APPLICATION DEVELOPMENT For More visit:
Copyright © 2012 Pearson Education, Inc. Chapter 1: Introduction to Computers and Programming 1.
Introduction to NS2 -Network Simulator- -Prepared by Changyong Jung.
CHAPTER FOUR COMPUTER SOFTWARE.
10/5/2015CS346 PHP1 Module 1 Introduction to PHP.
Client Scripting1 Internet Systems Design. Client Scripting2 n “A scripting language is a programming language that is used to manipulate, customize,
FTP Server and FTP Commands By Nanda Ganesan, Ph.D. © Nanda Ganesan, All Rights Reserved.
BLU-ICE and the Distributed Control System Constraints for Software Development Strategies Timothy M. McPhillips Stanford Synchrotron Radiation Laboratory.
CMPS 211 JavaScript Topic 1 JavaScript Syntax. 2Outline Goals and Objectives Goals and Objectives Chapter Headlines Chapter Headlines Introduction Introduction.
1 JavaScript in Context. Server-Side Programming.
XP Tutorial 10New Perspectives on Creating Web Pages with HTML, XHTML, and XML 1 Working with JavaScript Creating a Programmable Web Page for North Pole.
Extending HTML CPSC 120 Principles of Computer Science April 9, 2012.
Active Server Pages  In this chapter, you will learn:  How browsers and servers interacted on the Internet when the Internet first became popular 
Guide to Programming with Python Chapter One Getting Started: The Game Over Program.
CS4710 Why Progam?. Why learn to program? Utility of programming skills: understand tools modify tools create your own automate repetitive tasks automate.
Chapter 0 Getting Started. Objectives Understand the basic structure of a C++ program including: – Comments – Preprocessor instructions – Main function.
XP Tutorial 10New Perspectives on HTML and XHTML, Comprehensive 1 Working with JavaScript Creating a Programmable Web Page for North Pole Novelties Tutorial.
Guide to Linux Installation and Administration, 2e1 Chapter 11 Using Advanced Administration Techniques.
Overview of Form and Javascript fundamentals. Brief matching exercise 1. This is the software that allows a user to access and view HTML documents 2.
1 A Balanced Introduction to Computer Science David Reed, Creighton University ©2005 Pearson Prentice Hall ISBN X Chapter 4 JavaScript and.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
David Lawrence 7/8/091Intro. to PHP -- David Lawrence.
9-Nov-97Tri-Ada '971 TASH An Alternative to the Windows API TRI-Ada ‘97 Terry J. Westley
TCL TK. Tcl/Tk C functions can become Tcl commands that are invoked interactively Tk = scriptable, portable user interface –Windows, X (Unix), MacOS,
Introduction to Tcl/Tk TraNese Christy U.S. Army Research Laboratory.
1 JavaScript in Context. Server-Side Programming.
Chapter 5 Introduction To Form Builder. Lesson A Objectives  Display Forms Builder forms in a Web browser  Use a data block form to view, insert, update,
NASRULLAH KHAN.  Lecturer : Nasrullah   Website :
An Overview of Tcl/Tk Programming. Tcl/Tk Tcl (Tool Command Language) is used by over half a million developers worldwide and has become a critical component.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 14 Web Database Programming Using PHP.
Scripting.
1 CSC160 Chapter 1: Introduction to JavaScript Chapter 2: Placing JavaScript in an HTML File.
XP Tutorial 10New Perspectives on HTML, XHTML, and DHTML, Comprehensive 1 Working with JavaScript Creating a Programmable Web Page for North Pole Novelties.
A S P. Outline  The introduction of ASP  Why we choose ASP  How ASP works  Basic syntax rule of ASP  ASP’S object model  Limitations of ASP  Summary.
An Introduction To Tcl Scripting John Ousterhout Sun Microsystems Laboratories Tcl/Tk Tutorial, Part II.
TCL/TK Tool Command Language/Tool Kit. What is TCL? u Tool Command Language u An interpreted programming language  Created by John Ousterhout.John Ousterhout.
Server-Side Application and Data Management IT IS 3105 (Spring 2010)
Intro to PHP & Variables
TCL/TK Tool Command Language/Tool Kit.
Chapter 1 Coding Introduction.
Presentation transcript:

Programming Using Tcl/Tk Seree Chinodom seree@buu.ac.th http://lecture.compsci.buu.ac.th/TclTk

“Programming productivity may be increased as much as five times when a suitable high-level language is used.” F.P.Brooks, “The Mythical Man-Month” “ [representation] is important, because how information is represented can greatly affect how easy it is do do different things with it.” David Marr, “Vision”

Your Instructor SMTS in Scientific Computing at Sandia National Labs Writing Tcl code since Fall 1994 Ph.D. in Chemistry, MIT 1992 Using Tcl/Tk for systems integration, automated test, distributed computing Wrote XCELL Design Environment Friedman-Hill, E.J. and J.M. O’Connor, The XCELL Integrated Product Realization Environment: A Toolbox for Agile Construction of Custom Parametric Design Software, Proceedings of the 1996 Agility Forum National Meeting, Boston, MA.

What you’ll need A computer with WWW & FTP access (UNIX preferred; Win95, Win3.1 PC or Mac OK) Netscape Navigator (if available) for ‘Tclets’ A Tcl/Tk installation http://www.sunlabs.com/research/tcl/install.html Binaries and source Textbook Ousterhout’s Tcl and the Tk Toolkit (Addison-Wesley, ISBN 0-201-63337-X) Other reading material Up-to-date man pages; Tk4.0 Porting document

Other References Exploring Expect, Don Libes (O'Reilly, ISBN 1-56592-090-2) Recommended reading. Practical Programming in Tcl and Tk, Brent Welch (Prentice Hall, ISBN 0-13-182007-9) Lots of examples. Comes with a source disk. http://www.sunlabs.com/research/tcl/ Tcl/Tk home page. Source, docs, info. http://www.sunlabs.com/research/tcl/plugin/ Home page for the new Navigator plug-in

Who you are Write on a piece of paper: Your name What you do What you want to do with Tcl/Tk What other computer languages you know Anything you’d especially like to see covered

What I’ll do Today: give an overview of the Tcl language Next four weeks: Using the Tcl language GUI development with Tk Important Tcl extensions like expect Integrating Tcl/Tk with C and C++ applications Writing ‘Tclets’ for the WWW Put course notes on the Web: http://herzberg.ca.sandia.gov/TclCourse/ PowerPoint format only (sorry)

What you’ll do Complete several small problem sets Coding problems and questions Complete a term programming project You should choose a small application that fills a real need or solves a real problem Examples: GUI for a command-line utility; task automation; a game or puzzle Briefly present your project at the last meeting Ask questions Participation in class or via email is expected

Grading System Grade: 40% Homework 40% Project 20% Participation Scores: (None), OK, Good, Excellent Grading on a Curve. Mode is B+.

Overview of Tcl/Tk Component technologies: Tcl: embeddable scripting language Tk: GUI toolkit and widgets based on Tcl. The principle: universal scripting language controls everything: functions, interfaces, communication. Results: Raise the level of GUI programming: simpler, 5-10x faster application development than X, raw Win32 Greater power: programmable applications work together; cross-platform delivery Active objects: replace data with scripts.

Outline Tcl scripting language. Tk toolkit. Tk applications. Survey of applications and extensions. Conclusions.

Tcl: Tool Command Language Interactive programs need command languages: Typically redone for each application. Result: weak, quirky. emacs and csh powerful, but can't reuse. Solution: reusable scripting language. Interpreter is a C library. Provides basic features: variables, procedures, etc. Applications extend with additional features.

Scripting Language Philosophy Program size, complexity, reuse 1 Large, complex applications: Performance important. Need structure. Goal: prevent bad things. Interactive commands, scripting: Performance less important. Minimum structure: less overhead, easy interchange. Goal: enable good things. One language can't meet all needs?

Two-Language Approach ฅ Program size, complexity, reuse 1 C Tcl Use Tcl for scripting, C or C++ for large things. Goals for Tcl: Minimal syntax: easy to learn and type. Minimal structure: make things play together. Simple interfaces to C: extensibility.

Tcl: Tool Command Language Simple syntax (similar to sh, C, Lisp): 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]

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

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) Unfortunately, no 8-bit data

Factorial Procedure Defining and using procedures is easy! proc fac x { if $x<=1 {return 1} expr $x*[fac [expr $x-1]] } fac 4 ํ 24

Embedding Tcl In Applications Application generates scripts. Tcl parses scripts, passes words to command procedures. Application extends built-in command set: Define new object types in C. Implement primitive operations as new Tcl commands. Build complex features with Tcl scripts. Tcl Application Init Command Loop Parser Built-In Commands Application Commands Note that the command loop can also be in the Tcl/Tk box.

Extensions Extensions can be developed independently: Tcl Application Init Command Loop Parser Extension Commands Built-In Commands Application Commands Extensions can be developed independently: Network communication, database access, security, ... Applications can include combinations of extensions.

The Tk Toolkit The problem: Too hard to build applications with nice user interfaces. Even harder to do so cross-platform The wrong solution: C++, object-oriented toolkits, Java’s AWT Only small improvement (10-20%?): must still program at a low level. The right solution: Raise the level of programming. Create interfaces by writing Tcl scripts.

Creating User Interfaces With Tk Additional Tcl commands: Create Motif-like widgets (on all platforms) Arrange widgets. Bind events to Tcl commands. Manipulate selection, focus, window manager, etc. Library of C procedures: Create new widget classes. Create new geometry managers.

What's A Tk-Based Application? The Tcl interpreter. The Tk toolkit. Optionally, application-specific C code (primitives): New commands. New widgets. Tcl scripts (compose primitives into actions): Build user interface. Respond to events. Tcl commands

Wish: Windowing Shell Create user interfaces by writing Tcl scripts. Hello, world: button .hello -text "Hello, world" -command exit pack .hello Simple directory browser: 30 lines Web browser: 2000 lines 10x less code for simple things.

Browser Wish Script #!/usr/local/bin/wish4.0 listbox .list -yscroll ".scroll set" \ -width 20 -height 20 pack .list -side left scrollbar .scroll -command \ ".list yview" pack .scroll -side right -fill y wm title . "File Browser" if {$argc > 0} { set dir [lindex $argv 0] } else { set dir . } foreach i [lsort [glob * .*]] { .list insert end $i bind .list <Double-ButtonPress-1> { browse $dir [selection get] } bind all <Control-c> {destroy .} proc browse {dir file} { if {$dir != "."} { set file $dir/$file if {file isdirectory $file] { exec browse $file & } else { if [file isfile $file] { exec emacs $file & } else{ error "can't browse $file"

Other Uses For Tcl Data representation: Store data on disk as Tcl scripts. To load information, evaluate script Good for app configuration Active objects: Store scripts in objects. Evaluate scripts to give objects behavior. Communication: send Tcl scripts between applications. Executable content: Active e-mail messages (Safe-Tcl) Web pages (Tclets)

Status Runs on all UNIX/X platforms. Runs on Win32/Win16/Mac as of version 7.4/4.1. Source and documentation freely available. New Netscape Plug-In may increase audience. 100,000 developers world-wide? Hundreds of commercial products, free extensions. Newsgroup: comp.lang.tcl. 2 introductory books (Ousterhout, Welch).

Representative Applications Multimedia, groupware. Active e-mail messages. System administration. Testing. Scientific applications: instrument control, simulation, visualization, CAD. Real-time control system for offshore platform. British teletext system. Feature animation at Walt Disney Studios. On-air broadcast control system for NBC.

Popular Extensions Expect: remote control for interactive UNIX programs such as ftp, telnet, crypt, and fsck: #!/usr/local/bin/expect spawn rlogin [lindex $argv 0] expect -re "($|#|%) " send "cd [pwd]\r" send "setenv DISPLAY $env(DISPLAY)\r" interact Expect is available via anonymous FTP ftp://ftp.cme.nist.gov/pub/expect/

Popular Extensions, cont'd Archives via anonymous FTP: ftp://ftp.neosoft.com/pub/tcl TclX: general-purpose extensions: POSIX system calls. Keyed lists. File scanning (similar to awk). Date/time manipulation. Debugging, help, profiling. Oratcl and Sybtcl: access to commercial databases. Incr tcl: object-oriented programming in Tcl.

Popular Extensions, cont'd Tcl-DP: socket-based remote procedure calls, distributed objects Sample ‘unique ID’ server: set myId 0 proc GetId {} { global myId incr myId return $myId } dp_MakeRPCServer 4545 Sample client: set server [dp_MakeRPCClient foo.bar.com 4545] dp_rpc $server GetId Not yet updated for Tcl 7.4/4.1 socket command

Where You Might Use Tcl and Tk Creating graphical user interfaces. Testing. Applications that need scripting or extension facilities. Platform-independent applications. Creating interactive content for the WWW.

Drawbacks Must learn new language Substitution rules confusing to some people. Competitors: JavaScript, Visual Basic… Interpreted language has performance limits Surprisingly high; not much worse than Java C interfaces incompatible with Xt, Motif library. Motif look-and-feel on PC, Mac for now.

What’s up at Sun Labs? Create exciting Internet applications: Netscape Plug-In available now! Increase accessibility: More work on PC/Mac ports (native look and feel). SpecTcl: interactive GUI builder (available now) Better development tools (debugger, etc.) Improving the language/toolkit: Incremental on-the-fly compiler (some progress) Better support for modules, data structures. Dynamically loadable extensions now possible

Summing up... High-level programming: Less to learn. Build applications more quickly. Universal scripting language: Extend and modify applications at run-time. Make many things work together. Use scripts instead of data: Active objects, executable content. Tcl + Tk = shell of the 1990's?

Tcl Language Programming There are two parts to learning Tcl: 1. Syntax and substitution rules: Substitutions simple, but may be confusing at first. 2. Built-in commands: Can learn individually as needed. Control structures are commands, not language syntax. TCL HAS NO FIXED GRAMMAR!

Basics Tcl script = Sequence of commands. Commands separated by newlines, semi-colons. Tcl command = One or more words separated by white space. First word is command name, others are arguments. Returns string result. Examples: set a 22 set the variable ‘a’ to 22 puts "Hello, World!" world’s shortest program

Division Of Responsibility Command Chops commands into words. Makes substitutions. Does not interpret values of words. Single pass operation! Tcl Parser Words Interprets words. Can invoke parser recursively. Produces string result. Command Procedure Result

Arguments Parser assigns no meaning to arguments (quoting by default, evaluation is special): C: x = 4; y = x+10 y is 14 Tcl: set x 4; set y x+10 y is "x+10" Different commands assign different meanings to their arguments. “Type-checking” must be done by commands themselves. set a 122 expr 24/3.2 eval "set a 122" button .b -text Hello -fg red string length Abracadabra

Variable Substitution Syntax: $varName Variable name is letters, digits, underscores. This is a little white lie, actually. May occur anywhere in a word. Sample command Result set b 66 66 set a b b set a $b 66 set a $b+$b+$b 66+66+66 set a $b.3 66.3 set a $b4 no such variable

Command Substitution Syntax: [script] Evaluate script, substitute result. May occur anywhere within a word. Sample command Result set b 8 8 set a [expr $b+2] 10 set a "b-3 is [expr $b-3]" b-3 is 5

Controlling Word Structure Words break at white space and semi-colons, except: Double-quotes prevent breaks: set a "x is $x; y is $y" Curly braces prevent breaks and substitutions: set a {[expr $b*$c]} Backslashes quote special characters: set a word\ with\ \$\ and\ space Backslashes can escape newline (continuation) Substitutions don't change word structure: set a "two words" set b $a

Notes on Substitution and Parsing Tcl substitution rules are simple and absolute Example: comments set a 22; set b 33 <- OK # this is a comment <- OK set a 22 # same thing? <- Wrong! set a 22 ;# same thing <- OK Parser looks at a command just once! It’s OK to experiment Expressions exist that can’t be written in one command Sometimes things get hairy “{[“cmd”]}”

For Next Week... Get your Tcl/Tk environment set up Try the demos, especially Tk’s ‘widget’ Type in ‘Hello, World’ and get it to work Read Chapters 1 through 5 of Ousterhout