A Developer's Guide


  • Clif Flynt, CEO, Noumena Corporation, Dexter, Michigan

Newly updated with over 150 pages of material on the latest Tcl extensions, Tcl/Tk: A Developer’s Guide is a unique practical tutorial for professional programmers and beginners alike. Starting with a clear picture of the basics, Tcl/Tk covers the variety of tools in this "Swiss army knife" of programming languages, giving you the ability to enhance your programs, extend your application's capabilities, and become a more effective programmer.

This updated edition covers all of the new features of version 8.6, including object-oriented programming and the creation of megawidgets, existing data structure implementations, themed widgets and virtual events. Extensive code snippets and online tutorials in various languages will give you a firm grasp on how to use the Tcl/Tk libraries and interpreters and, most importantly, on what constitutes an effective strategy for using Tcl/Tk.

View full description


Applications developers, programmers, software testers, systems software engineers, software architects, and design engineers


Book information

  • Published: February 2012
  • ISBN: 978-0-12-384717-1


"The best computer programmers are astonishingly more productive than average. One reason is that they use good tools, and they use them well. Clif Flynt's Tcl/Tk: A Developer's Guide, Second Edition helps you improve your effectiveness as a developer in both of these ways. Tcl is a software language with a great return on investment—it achieves portable, powerful, economical results with great economy. Just a few lines of Tcl are enough to create applications that are both interesting and useful. Clif draws on his deep experience as a front-line developer to help you get the most out of Tcl. He writes clearly, and has organized his book with instructive examples that teach the essentials of Tcl. He covers the right material too: Tcl/Tk packs in not just such long-standing Tcl strengths such as its easy networking and graphical user interface, but also the latest breakthroughs with internationalization, widget upgrades, and StarPacks. Want to take your programming to the next level? Get Tcl/Tk: A Developer's Guide, Second Edition."

—Cameron Laird is vice president of Phaseit, Inc. (, where he specializes in Rapid Enterprise Integration with Tcl and related tools. His frequent publications on information technology include the popular "Regular Expressions" column (

Table of Contents

Tcl/Tk Features.1.1 Tcl Overview.1.1.1 The Standard Tcl Distribution1.2 Tcl as a Glue Language1.2.2 Tcl Scripts Compared with Unix Shell Scripts1.2.3 Tcl Scripts Compared with MS-DOS .bat Files1.3 Tcl as a General-Purpose Interpreter1.3.1 Tcl/Tk Compared with Visual Basic1.3.2 Tcl/Tk Compared with Perl1.3.3 Tcl/Tk Compared with Python1.3.4 Tcl/Tk Compared with Java1.4 Tcl as an Extensible Interpreter1.5 Tcl as an Embeddable Interpreter1.6 Tcl as a Rapid Development Tool1.7 GUI-Based Programming1.8 Shipping Products1.9 Bottom Line1.10 ProblemsThe Mechanics of Using the Tcl and Tk Interpreters.2.1 The tclsh and wish Interpreters2.1.1 Starting the tclsh and wish Interpreters2.1.2 Starting tclsh or wish under Unix2.1.3 Starting tclsh or wish under Microsoft Windows2.1.4 Starting tclsh or wish on the Mac2.1.5 Exiting tclsh or wish2.2 Using tclsh/wish Interactively2.2.1 Tclsh as a Command Shell2.2.2 Tk Console (tkcon) -- An Alternative Interactive tclsh/wish Shell2.2.3 Evaluating Scripts Interactively2.3 Evaluating Tcl Script Files2.3.1 The Tcl Script File2.3.2 Evaluating Tcl Script Files2.3.3 Evaluating a Tcl Script File under Unix2.3.4 Evaluating a Tcl Script File under Microsoft Windows2.3.5 Evaluating a Tcl Script on the Mac2.4 Bottom Line2.5 ProblemsIntroduction to the Tcl Language.3.1 Overview of the Basics3.1.1 Syntax3.1.2 Grouping Words3.1.3 Comments3.1.4 Data Representation3.1.5 Command Results3.1.6 Errors3.2 Command Evaluation and Substitutions3.2.1 Substitution3.2.2 Controlling Substitutions with Quotes, Curly Braces, and Backslash3.2.3 Steps in Command Evaluation3.3 Data Types3.3.1 Assigning Values to Variables3.3.2 Strings3.3.3 String Processing Commands3.3.4 Lists3.3.5 List Processing Commands3.3.6 Associative Arrays3.3.7 Associative Array Commands3.3.8 Binary Data3.3.9 Handles3.4 Arithmetic and Boolean operations3.4.1 Math Operations3.4.2 Conditionals3.4.3 Looping3.5 Modularization3.5.1 Procedures3.6 Bottom Line3.7 ProblemsFile System, Disk I/O and Sockets.4.0.1 Navigating the File System4.0.2 Properties of file system items4.0.3 Removing files4.1.2 Input4.1.3 Creating a channel4.1.4 Closing Channels4.1 Input/Output in Tcl4.1.1 Output4.2 Sockets4.2.1 Using a client socket4.2.2 Controlling Data Flow4.2.3 Server Sockets4.3 Bottom Line4.4 ProblemsUsing Strings and Lists.5.1 Converting a String into a List5.2 Examining the List with a for Loop5.3 Using the foreach Command5.4 Using the string match instead of string first5.5 Using lsearch5.6 The regexp Command5.6.1 Regular Expression Matching Rules5.6.2 Advanced and Extended Regular Expression Rules5.7 Creating a Procedure5.8 Making a Script5.9 Speed Considerations5.10 Bottom Line5.11 ProblemsBuilding complex data structures with lists and arrays.6.1 Using the Tcl List6.2 Using the Associative Array6.3 Exception handling and Introspection6.4 Trees in Tcl6.5 Tree Library Implementation6.5.5 Generating unique names6.6 Using the Tree Library6.7 Speed Considerations6.8 Bottom Line6.9 ProblemsProcedure Techniques.7.1 Arguments to Procedures7.1.1 Variable Number of Arguments to a Procedure7.1.2 Default Values for Procedure Arguments7.2 Renaming or Deleting Commands7.3 Getting Information about Procedures7.4 Substitution and Evaluation of Strings7.4.1 Performing Variable Substitution on a String7.4.2 Evaluating a String as a Tcl Command7.5 Working with Global and Local Scopes7.5.1 Global and Local Scope7.6.1 An Object Example7.6.2 Creating a Tree Object7.6.3 Defining the Object's Method7.6 Making a Tcl Object7.7 Bottom Line7.8 ProblemsNamespaces and Pack.8.1 Namespaces and Scoping Rules8.1.1 Namespace Scope8.1.2 Namespace Naming Rules8.1.3 Accessing Namespace Entities8.1.4 Why Use Namespaces?8.1.5 The namespace and variable Commands8.1.6 Creating and Populating a Namespace8.1.7 Namespace Nesting8.2 Packages8.2.1 How Packages Work8.2.2 Internal Details: Files and Variables Used with Packages8.2.3 Package Commands8.2.4 Version Numbers8.2.5 Package Cookbook8.3 A Tree Object Package with Namespaces8.3.1 Adding Namespace and Package to tree.tcl8.3.2 The Tree Object in a Namespace8.3.3 Procedures and Namespace Scopes8.4 Bottom Line8.5 ProblemsIntroduction to Tk Graphics.9.1 Creating a Widget9.2 Conventions9.2.1 Widget Naming Conventions9.2.2 Color Naming Conventions9.2.3 Dimension Conventions9.3 Common9.4 Determining and Setting Options9.5 The Basic Widgets9.6 Introducing Widgets: label, button, and entry9.7 Widget Layout: frame, place, pack, and grid9.7.1 The frame Widget9.7.2 The place Layout Manager9.7.3 The pack Layout Manager9.7.4 The grid Layout Manager9.7.5 Working Together9.8 Selection Widgets: radiobutton, checkbutton, menu, and listbox9.8.1 radiobutton and checkbutton9.8.2 Pull-down Menus: menu, menubutton and menubars9.8.2.1 Menubars9.8.3 Selection widgets: listbox9.9 Scrollbar9.9.1 The Basic scrollbar9.9.2 scrollbar Details9.9.3 Intercepting scrollbar Commands9.13.1 Canceling the Future9.10 The scale Widget9.11 New Windows9.12 Interacting with the Event Loop9.13 Scheduling the Future: after9.14 Bottom Line9.15 ProblemsUsing the canvas widget.10.1 Overview of the canvas Widget10.1.1 Identifiers and Tags10.1.2 Coordinates10.1.3 Binding10.2 Creating a canvas10.3 Creating Displayable Canvas Items10.3.1 An Exam10.4 More Canvas Widget Subcommands10.4.1 Modifying an Item10.4.2 Changing the Display Coordinates of an Item10.4.3 Moving an Item10.4.4 Finding Items, Raising and Lowering Items10.4.5 Fonts and Text items10.4.6 Using a Canvas Larger than the View10.5 The bind and focus Commands10.5.1 The bind Command10.5.2 The Canvas Widget bind Subcommand10.5.3 Focus10.6 Creating a Widget10.7 A Help-Balloon: Interacting with the window manager10.8 The image Object10.8.1 The image Command10.8.2 Bitmap Images10.8.3 Photo Images10.8.4 Revisiting the delayButton Widget10.9 Bottom Line10.10 ProblemsThe text widget and htmllib.11.1 Overview of the text Widget11.1.1 Text Location in the text Widget11.1.2 Tag Overview11.1.3 Mark Overview11.1.4 Image Overview11.1.5 Window Overview11.2 Creating a text Widget11.3 Text Widget Subcommands11.3.1 Inserting and deleting text11.3.2 Searching Text11.3.3 The mark Subcommands11.3.4 Tags11.3.4.2 Finding Tags11.3.5 Inserting Images and Widgets into a text Widget11.4 HTML Display Package11.4.1 Displaying HTML Text11.4.2 Using html_library Callbacks: Loading Images and Hypertext Links11.4.3 Interactive help with the text widget and htmllib11.5 Bottom Line11.6 ProblemsTk Megawidgets.12.1 Standard Dialog Widgets12.2 Megawidget Building Philosophy12.2.1 Display in Application Window or Main Display?12.2.2 Modal versus Modeless Operation12.2.3 Widget Access Conventions12.2.4 Widget Frames12.2.5 Configuration12.2.6 Access to Subwidgets12.2.7 Following Tk Conventions12.3 Functionality that makes Megawidgets possible12.3.1 The rename command12.3.2 The option command12.3.3 The -class option12.4 Building a Megawidget12.5 A Scrolling Listbox Megawidget12.5.1 scrolledListBox Description12.5.2 Using the scrolledLB12.5.3 Implementing the Scrollable ListBox12.5.4 The scrolledLB Code12.6 Namespaces and Tk widgets12.7 Incorporating a Megawidget into a Larger Megawidget12.8 Making a Modal Megawidget: The grab and tkwait Commands12.8.1 The grab command12.8.2 The tkwait command12.8.3 The modal widget code12.9 Automating megawidget Construction12.9.1 Building megawidgets from a skeleton12.9.2 Building megawidgets from a configuration file12.9.3 Another technique for building megawidgets12.10 Bottom Line12.11 ProblemsWriting a Tcl Extension.13.1 Functional View of a Tcl Extension13.1.1 Overview13.1.2 Initialize any persistent data structures13.1.3 Register new commands with the Interpreter13.1.4 Accept data from Tcl Interpreter13.1.5 Returning Results13.1.6 Returning Status to the Script13.1.7 Dealing with Persistent Data13.2 Building an Extension13.2.1 Structural Overview of an Extension13.2.2 Naming Conventions13.3 An Example13.3.1 demoInt.h13.3.2 demoInit.c13.3.3 demoCmd.c13.3.4 demoDemo.c13.4 Complex Data13.5 Bottom Line13.6 ExercisesExtensions and Package.14.1 [incr Tcl]14.2 Expect14.3 TclX14.4 Sybtcl and Oratcl14.5 MySqlTcl14.6 VSdb Package14.7 BWidgets14.8 BLT14.9 Graphics Extensions: Img14.10 Bottom LineProgramming Tools.15.1 Code Formatter15.1.1 frink15.2 Code Checkers15.2.1 tclCheck15.2.2 ICEM ice_lint15.2.3 procheck15.3 Debuggers15.3.1 Debug15.3.2 Graphic Debuggers15.4 GUI Generators15.4.1 SpecTcl15.4.2 Visual GYPSY15.5 Tcl Compilers15.5.1 ICEM Tcl Compiler15.5.2 TclPro procomp15.6 Packaging Tools15.6.1 TclPro prowrap15.6.2 FreeWrap15.6.3 Starkit and Starpack15.7 Tcl Extension Generators15.7.1 SWIG15.7.2 CriTcl15.8 Integrated Development Environments15.8.1 ASED15.8.2 Komodo15.8.3 MyrmecoX15.9 Bottom LineTips and Techniques.16.1 Debugging Techniques16.2 Tcl as a Glue Language: The exec Command16.3 Common Mistakes16.4 Coding Tips and Techniques16.5 Bottom Line