TCL/TK - Tutorialspoint

Transcription

Tcl/TkAbout the TutorialTcl is a general purpose multi-paradigm system programming language. It is a scriptinglanguage that aims at providing the ability for applications to communicate with eachother.On the other hand, Tk is a cross platform widget toolkit used for building GUI in manylanguages.This tutorial covers various topics ranging from the basics of the Tcl/ Tk to its scope invarious applications.AudienceThis tutorial is designed for all those individuals who are looking for a starting point oflearning Tcl/ Tk. Therefore, we cover all those topics that are required for a beginner andan advanced user.PrerequisitesBefore proceeding with this tutorial, it is advisable for you to understand the basic conceptsof computer programming. This tutorial is self-contained and you will be able to learnvarious concepts of Tcl/Tk even if you are a beginner. You just need to have a basicunderstanding of working with a simple text editor and command line.Disclaimer & Copyright Copyright 2015 by Tutorials Point (I) Pvt. Ltd.All the content and graphics published in this e-book are the property of Tutorials Point (I)Pvt. Ltd. The user of this e-book is prohibited to reuse, retain, copy, distribute, or republishany contents or a part of contents of this e-book in any manner without written consentof the publisher.We strive to update the contents of our website and tutorials as timely and as precisely aspossible, however, the contents may contain inaccuracies or errors. Tutorials Point (I) Pvt.Ltd. provides no guarantee regarding the accuracy, timeliness, or completeness of ourwebsite or its contents including this tutorial. If you discover any errors on our website orin this tutorial, please notify us at contact@tutorialspoint.comi

Tcl/TkTable of ContentsAbout the Tutorial . iAudience . iPrerequisites . iDisclaimer & Copyright . iTable of Contents . ii1.TCL – OVERVIEW . 1Features of Tcl . 1Applications. 12.TCL – ENVIRONMENT SETUP . 2Local Environment Setup . 2Text Editor . 2The Tcl Interpreter . 3Installation on Windows . 3Installation on Linux . 3Installation on Debian based Systems . 4Installation on Mac OS X. 4Installation from Source Files . 43.TCL – SPECIAL VARIABLES. 6Examples for using Tcl Special Variables . 74.TCL – BASIC SYNTAX . 9First Tcl Program . 9Comments . 9Identifiers . 10Reserved Words . 10ii

Tcl/TkWhitespace in Tcl . 115.TCL – COMMANDS . 12Command Substitution . 13Variable Substitution . 13Backslash Substitution . 136.TCL – DATA TYPES . 14Simple Tcl Objects . 14String Representations . 15List . 15Associative Array . 16Handles . 167.TCL – VARIABLES . 17Variable Naming . 17Dynamic Typing . 17Mathematical Expressions . 188.TCL – OPERATORS . 20Arithmetic Operators. 20Relational Operators . 22Logical Operators . 23Bitwise Operators . 25Ternary Operator. 27Operators Precedence in Tcl . 279.TCL – DECISIONS . 30Tcl - If Statement . 31Tcl – If else Statement . 32iii

Tcl/TkThe if.else if.else Statement . 34Tcl – Nested If Statement . 35Tcl – Switch Statement . 36Tcl – Nested Switch Statement . 39The? : Operator . 4010.TCL – LOOPS. 42Tcl – While Loop . 43Tcl – For Loops . 44Tcl – Nested Loops . 46Loop Control Statements . 48Tcl – Break Statement. 48Tcl – Continue Statement . 50The Infinite Loop . 5111.TCL – ARRAYS . 52Size of Array . 52Array Iteration . 52Associative Arrays . 53Indices of Array . 53Iteration of Associative Array . 5412.TCL – STRINGS. 55String Representations . 55String Escape Sequence . 55String Command . 5613.TCL – LISTS . 63Creating a List . 63iv

Tcl/TkAppending Item to a List . 64Length of List . 64List Item at Index . 64Insert Item at Index . 65Replace Items at Indices . 65Set Item at Index . 66Transform List to Variables . 66Sorting a List . 6614.TCL – DICTIONARY. 68Size of Dict . 68Dictionary Iteration . 69Value for Key in Dict . 69All Keys in Dict . 70All Values in Dict . 70Key Exists in Dict . 7015.TCL – PROCEDURES . 72Procedures with Multiple Arguments . 72Procedures with Variable Arguments . 73Procedures with Default Arguments . 73Recursive Procedures . 7416.TCL – PACKAGES . 75Creating Package . 7517.TCL – NAMESPACES . 77Creating Namespace . 77Nested Namespaces . 77v

Tcl/TkImporting and Exporting Namespace. 78Forget Namespace . 7918.TCL – FILE I/O . 80Opening Files . 80Closing a File . 80Writing a File . 81Reading a File . 8119.TCL – ERROR HANDLING . 83Error Syntax . 83Catch Syntax . 8320.TCL – BUILT-IN FUNCTIONS . 85Math Functions . 85System Functions. 8721.TCL – REGULAR EXPRESSIONS . 90Syntax . 90Multiple Patterns . 91Switches for Regex Command . 9222.TK – OVERVIEW. 94Features of Tk . 94Applications Built in Tk . 9423.TK – ENVIRONMENT. 95The Tk Interpreter . 95Installation on Windows . 95Installation on Linux . 96vi

Tcl/TkInstallation on Debian Based Systems . 96Installation on Mac OS X. 97Installation from Source Files . 97Examples for Using Tcl Special Variables . 9924.TK – WIDGETS OVERVIEW . 101Creating a Widget . 101Widget Naming Convention . 101Color Naming Convention . 101Dimension Convention . 101Common Options . 10225.TK – BASIC WIDGETS . 105Tk - Label Widget . 105Tk – Button Widget . 106Tk – Entry Widgets . 109Tk – Message Widget . 110Tk – Text Widget . 111Tk – Top Level Widgets . 11326.TK – LAYOUT WIDGETS. 115Tk – Frame Widget. 115Tk – Place Widget . 116Tk – Pack Widget . 117Tk – Grid Widget . 11927.TK – SELECTION WIDGETS . 122Tk – Radio Button Widget . 122Tk – Check Button Widget . 123vii

Tcl/TkTk – Menu Widget . 125Tk – Listbox Widget . 12728.TK – CANVAS WIDGETS . 131Options. 131Widgets for Drawing in Canvas . 132Tk – Canvas Line Widget . 132Tk - Canvas Arc Widget . 134Tk – Canvas Rectangle Widget . 135Tk – Canvas Oval Widget . 136Tk – Canvas Polygon Widget . 137Tk - Canvas Text Widget . 138Tk – Canvas Bitmap Widget . 139Tk – Canvas Image Widget . 14029.TK – MEGA WIDGETS . 143Tk – Dialog Widget. 143Tk – Spinbox Widget . 144Tk – Combobox Widget . 145Tk – Notebook Widget . 146Tk – Progressbar Widget . 147Tk – Treeview Widget . 148Tk – Scrollbar Widget . 150Tk – Scale Widget . 15130.TK – FONTS . 154Options. 15431.TK – IMAGES . 156viii

Tcl/TkOptions. 15632.TK – EVENTS . 159Event Binding . 16133.TK – WINDOWS MANAGER . 162Creating Window . 164Destroying Window . 16434.TK – GEOMETRY MANAGER . 165Positioning and Sizing . 165Grid Geometry. 165ix

Tcl – OverviewTcl is shortened form of Tool Command Language. John Ousterhout of the University ofCalifornia, Berkeley, designed it. It is a combination of a scripting language and its owninterpreter that gets embedded to the application, we develop with it.Tcl was developed initially for Unix. It was then ported to Windows, DOS, OS/2, and MacOSX. Tcl is much similar to other unix shell languages like Bourne Shell (Sh), the C Shell(csh), the Korn Shell (sh), and Perl.It aims at providing ability for programs to interact with other programs and also for actingas an embeddable interpreter. Even though, the original aim was to enable programs tointeract, you can find full-fledged applications written in Tcl/Tk.Features of TclThe features of Tcl are as follows: Reduced development time. Powerful and simple user interface kit with integration of TK. Write once, run anywhere. It runs on Windows, Mac OS X, and almost on everyUnix platform. Quite easy to get started for experienced programmers; since, the language is sosimple that they can learn Tcl in a few hours or days. You can easily extend existing applications with Tcl. Also, it is possible to includeTcl in C, C , or Java to Tcl or vice versa. Have a powerful set of networking functions. Finally, it’s an open source, free, and can be used for commercial applicationswithout any limit.ApplicationsTcl is a general-purpose language and you can find Tcl everywhere. It includes, Scalable websites that are often backed by databases. High performance web servers build with TclHttpd. Tcl with CGI based websites. Desktop GUI applications. Embedded applications.

Tcl – Environment SetupTcl/TkTry it OptionYou really do not need to set up your own environment to start learning Tclprogramming. Reason is very simple, we already have set up Tcl Programmingenvironment online, so that you can execute all the Tcl examples online at the sametime when you are

Tcl was developed initially for Unix. It was then ported to Windows, DOS, OS/2, and Mac OSX. Tcl is much similar to other unix shell languages like Bourne Shell (Sh), the C Shell (csh), the Korn Shell (sh), and Perl. It aims at providing ability for programs to interact with other programs and also for acting as an embeddable interpreter.