Book Image

Tcl/Tk 8.5 Programming Cookbook

Book Image

Tcl/Tk 8.5 Programming Cookbook

Overview of this book

With Tcl/Tk, you can create full-featured cross-platform applications in a simple and easy-to-understand way without any expensive development package; the only tools required are a simple text editor and your imagination. This practical cookbook will help you to efficiently interact with editors, debuggers, and shell type interactive programs using Tcl/Tk 8. This cookbook will comprehensively guide you through practical implementation of Tcl/Tk 8.5 commands and tools. This book will take you through all the steps needed to become a productive programmer in Tcl/Tk 8. Right from guiding you through the basics to creating a stand-alone application, it provides complete explanation of all the steps along with handy tips and tricks. The book begins with an introduction to the Tcl shell, syntax, variables, and programming best practices in the language. It then explores procedures and the flow of events with control constructs followed by advanced error trapping and recovery. From Chapter 4, a detailed study of string expressions and handling enables you to handle various string functions and use lists to expand the string functionality. The book then discusses in-depth the Tcl Dictionary and how to utilize it to store and retrieve data. File operations and Tk GUI handling are covered extensively along with a developing a real-world address book application to practice the concepts learned.
Table of Contents (20 chapters)
Tcl/Tk 8.5 Programming Cookbook
Credits
About the Author
Acknowledgment
About the Reviewers
www.PacktPub.com
Preface

Preface

Created in 1988 by John Ousterhoult, while working at the University of Califormia, Berkeley, Tcl (Tool Command Language) is a scripting language originally designed for embedded system platforms. Since its creation, Tcl has grown far beyond its original design with numerous expansions and additions (such as the graphical Took Kit or Tk) to become a full-featured scripted programming language capable of creating elegant, cross-platform solutions.

This book is written for both the beginning developer looking for a instructions on how to get their application up and running quickly to the experienced Tcl/Tk programmer looking to sharpen their skills. You will find everything from utilization of the console commands through to the creation of a stand-alone application.

What this book covers

Chapter 1, The Tcl Shell, gives an introduction to the Tcl shell.

Chapter 2, Using the Building Blocks Control Constructs, talks about using control constructs (if statements, for statements, and so on) to perform control program flow.

Chapter 3, Error Handling, talks about using the built-in commands and the Tcl shell to perform error handling.

Chapter 4, Handling String Expressions, explains how to create, manipulate, and manage string variables.

Chapter 5, Expanding String Functionality Using List, shows how to create, manipulate, and manage data in Tcl lists.

Chapter 6, The Tcl Dictionary, explains how to create, manipulate, and manage data in Tcl dictionaries.

Chapter 7, File Operations, tells how to open, read, write, and configure access to files stored on the system.

Chapter 8, Tk GUI Programming with Tcl/Tk, gives an introduction to the Tk shell, creating and managing a widget or window.

Chapter 9, Configuring and Controlling Tk Widgets, explains about creating and managing the most commonly used Tk widgets.

Chapter 10, Geometry Management, talks about managing the layout and design of the window.

Chapter 11, Using Tcl Built-In Dialog Windows, is about the creation and use of the Tcl built-in dialog windows available in Tk.

Chapter 12, Creating and Managing Menus, explains creating and managing menus, menu buttons, and pop-up menus.

Chapter 13, Creating the Address Book Application, gives full code listing and description of an Address Book application that makes use of the information covered in the previous sections.

What you need for this book

To complete the recipes covered in this book you will need the following:

  • A computer running any supported operating system (Window, Linux, Mac OSX, and so on)

  • A standard installation of Tcl/Tk

  • A non-formatting text editor such as Notepad

Who this book is for

If you are a beginner interested in adding Tcl/Tk 8.5 to your list of languages or an experienced Tcl/Tk programmer looking to sharpen your knowledge, be assured you will find your perfect guide in this book. Whether you are developing for your personal use or commercial applications, this book will provide you with a ready reference to the building blocks of Tcl/Tk 8.5.

Conventions

In this book, you will find a number of styles of text that distinguish between different kinds of information. Here are some examples of these styles, and an explanation of their meaning.

Code words in text are shown as follows: "The catch construct is used to prevent errors from aborting a script."

A block of code is set as follows:

If {[catch {set doubled [expr $value * 2]} errmsg]} {
puts "Script Failed - $errmsg"
} else {
puts "$value doubled is: $doubled"
}

Any command-line input or output is written as follows:

% unset x
%

New terms and important words are shown in bold. Words that you see on the screen, in menus or dialog boxes for example, appear in the text like this: "If the user clicks on the Cancel button, an empty string is returned."

Note

Warnings or important notes appear in a box like this.

Note

Tips and tricks appear like this.

Reader feedback

Feedback from our readers is always welcome. Let us know what you think about this book—what you liked or may have disliked. Reader feedback is important for us to develop titles that you really get the most out of.

To send us general feedback, simply send an e-mail to, and mention the book title via the subject of your message.

If there is a book that you need and would like to see us publish, please send us a note in the SUGGEST A TITLE form on www.packtpub.com or e-mail.

If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide on www.packtpub.com/authors.

Customer support

Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.

Note

Downloading the example code for this book

You can download the example code files for all Packt books you have purchased from your account at http://www.PacktPub.com. If you purchased this book elsewhere, you can visit http://www.PacktPub.com/support and register to have the files e-mailed directly to you.

Errata

Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books—maybe a mistake in the text or the code—we would be grateful if you would report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting http://www.packtpub.com/support, selecting your book, clicking on the errata submission form link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded on our website, or added to any list of existing errata, under the Errata section of that title. Any existing errata can be viewed by selecting your title from http://www.packtpub.com/support.

Piracy

Piracy of copyright material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works, in any form, on the Internet, please provide us with the location address or website name immediately so that we can pursue a remedy.

Please contact us at with a link to the suspected pirated material.

We appreciate your help in protecting our authors, and our ability to bring you valuable content.

Questions

You can contact us at if you are having a problem with any aspect of the book, and we will do our best to address it.