Tcl/Tk 8.5 Programming Cookbook
By Bert Wheeler
5/5
()
About this ebook
Related to Tcl/Tk 8.5 Programming Cookbook
Related ebooks
Clojure Programming Cookbook Rating: 0 out of 5 stars0 ratingsJulia Cookbook Rating: 0 out of 5 stars0 ratingsLua Game Development Cookbook Rating: 0 out of 5 stars0 ratingsD Cookbook Rating: 0 out of 5 stars0 ratingsWindows Presentation Foundation 4.5 Cookbook Rating: 0 out of 5 stars0 ratingsLinux Shell Scripting Cookbook, Second Edition Rating: 0 out of 5 stars0 ratingsTcl/Tk: A Developer's Guide Rating: 5 out of 5 stars5/5Programming Concepts in Java Rating: 0 out of 5 stars0 ratingsReal-Time Embedded Systems Rating: 0 out of 5 stars0 ratingsJulia: High Performance Programming Rating: 0 out of 5 stars0 ratingsLearning ClojureScript Rating: 0 out of 5 stars0 ratingsThe Designer's Guide to VHDL Rating: 4 out of 5 stars4/5Introduction to Parallel Programming Rating: 0 out of 5 stars0 ratingsLearn Multithreading with Modern C++ Rating: 0 out of 5 stars0 ratingsCommon LISP: The Language Rating: 4 out of 5 stars4/5Get Programming with Haskell Rating: 0 out of 5 stars0 ratingsQt 5 Blueprints Rating: 4 out of 5 stars4/5The Art of Multiprocessor Programming, Revised Reprint Rating: 4 out of 5 stars4/5GPU Computing Gems Jade Edition Rating: 5 out of 5 stars5/5Visual Studio Tips and Tricks: I Rating: 0 out of 5 stars0 ratingsVLSI Design Rating: 5 out of 5 stars5/5Computer Practices Using C++ Rating: 0 out of 5 stars0 ratingsGNU Octave Beginner's Guide Rating: 3 out of 5 stars3/5Game Programming Using Qt: Beginner's Guide Rating: 0 out of 5 stars0 ratingsImplementing a Cpu using Fpga Rating: 0 out of 5 stars0 ratingsCommon Lisp A Complete Guide Rating: 1 out of 5 stars1/5Advances in GPU Research and Practice Rating: 0 out of 5 stars0 ratingsGetting Started with LLVM Core Libraries Rating: 0 out of 5 stars0 ratingsDigital Arithmetic Rating: 5 out of 5 stars5/5
Computers For You
Standard Deviations: Flawed Assumptions, Tortured Data, and Other Ways to Lie with Statistics Rating: 4 out of 5 stars4/5Slenderman: Online Obsession, Mental Illness, and the Violent Crime of Two Midwestern Girls Rating: 4 out of 5 stars4/5The Invisible Rainbow: A History of Electricity and Life Rating: 4 out of 5 stars4/5Elon Musk Rating: 4 out of 5 stars4/5CompTIA IT Fundamentals (ITF+) Study Guide: Exam FC0-U61 Rating: 0 out of 5 stars0 ratingsMaster Builder Roblox: The Essential Guide Rating: 4 out of 5 stars4/5The ChatGPT Millionaire Handbook: Make Money Online With the Power of AI Technology Rating: 0 out of 5 stars0 ratingsSQL QuickStart Guide: The Simplified Beginner's Guide to Managing, Analyzing, and Manipulating Data With SQL Rating: 4 out of 5 stars4/5101 Awesome Builds: Minecraft® Secrets from the World's Greatest Crafters Rating: 4 out of 5 stars4/5Mastering ChatGPT: 21 Prompts Templates for Effortless Writing Rating: 5 out of 5 stars5/5Procreate for Beginners: Introduction to Procreate for Drawing and Illustrating on the iPad Rating: 0 out of 5 stars0 ratingsUltimate Guide to Mastering Command Blocks!: Minecraft Keys to Unlocking Secret Commands Rating: 5 out of 5 stars5/5Everybody Lies: Big Data, New Data, and What the Internet Can Tell Us About Who We Really Are Rating: 4 out of 5 stars4/5CompTIA Security+ Get Certified Get Ahead: SY0-701 Study Guide Rating: 5 out of 5 stars5/5Learning the Chess Openings Rating: 5 out of 5 stars5/5The Hacker Crackdown: Law and Disorder on the Electronic Frontier Rating: 4 out of 5 stars4/5Dark Aeon: Transhumanism and the War Against Humanity Rating: 5 out of 5 stars5/5The Mega Box: The Ultimate Guide to the Best Free Resources on the Internet Rating: 4 out of 5 stars4/5User Friendly: How the Hidden Rules of Design Are Changing the Way We Live, Work, and Play Rating: 4 out of 5 stars4/5Deep Search: How to Explore the Internet More Effectively Rating: 5 out of 5 stars5/5Alan Turing: The Enigma: The Book That Inspired the Film The Imitation Game - Updated Edition Rating: 4 out of 5 stars4/5CompTIA Security+ Practice Questions Rating: 2 out of 5 stars2/5AP® Computer Science Principles Crash Course Rating: 0 out of 5 stars0 ratingsStorytelling with Data: Let's Practice! Rating: 4 out of 5 stars4/5
Reviews for Tcl/Tk 8.5 Programming Cookbook
1 rating0 reviews
Book preview
Tcl/Tk 8.5 Programming Cookbook - Bert Wheeler
Table of Contents
Copyright
Credits
About the Author
Acknowledgment
About the Reviewers
www.PacktPub.com
Support files, eBooks, discount offers and more
Why Subscribe?
Free Access for Packt account holders
Preface
What this book covers
What you need for this book
Who this book is for
Conventions
Reader feedback
Customer support
Errata
Piracy
Questions
1. The Tcl Shell
Introduction
The Tcl shell
Writing to the Tcl console
Getting ready
How to do it…
How it works…
Mathematical expressions
Tcl expr operands
Mathematical functions
Computing mathematical expressions
Referencing files in Tcl
UNIX (UNIX, Linux and Mac OS X)
Windows
Variables
Basic variable commands
Getting Ready
How to do it…
How it works…
There's more…
Unsetting a variable
Command line arguments
Launching a Tcl script
Getting Ready
How to do it…
How it works…
There's more…
2. Using the Building Blocks Control Constructs
Introduction
Controlling flow with the if statement
How to do it…
How it works…
There's more…
Looping with for
How to do it…
How it works…
Looping with foreach
How to do it…
How it works…
Looping with while
How to do it…
How it works…
Continuing a procedure
How to do it…
How it works…
Breaking out of a procedure
How to do it…
How it works…
Nested looping
How to do it…
How it works…
There's more…
3. Error Handling
Introduction
Using the catch command
Getting ready
How to do it…
How it works…
Using the eval command
Getting ready
How to do it…
How it works…
There's more…
Using the error command
Getting ready
How to do it…
How it works…
Error handling procedure
Getting ready
How to do it…
How it works…
4. Handling String Expressions
Introduction
Appending to a string
How to do it…
How it works…
Formatting a string
How to do it…
How it works…
Matching a regular expression within a string
Getting ready
How to do it…
How it works…
Performing character substitution on a string
How to do it…
How it works…
Parsing a string using conversion specifiers
How to do it…
How it works…
Determining the length of a string
How to do it…
How it works…
Comparing strings
Getting ready
How to do it…
How it works…
Comparing a string of characters
How to do it…
How it works…
Locating the first instance of a character
How to do it…
How it works…
Locating the index of a character
How to do it…
How it works…
Determining the class of a string
How to do it…
How it works…
Locating the last instance of a string
How to do it…
How it works…
Determining the size of a string
How to do it…
How it works…
Replacing values within a string
How to do it…
How it works…
Locating a pattern within a string
How to do it…
How it works…
Returning a range of characters from a string
How to do it…
How it works…
Creating a string of repeated characters
How to do it…
How it works…
Replacing ranges of characters contained within a string
How to do it…
How it works…
Creating a reverse string
Getting ready
How to do it…
How it works…
Converting a string to lowercase
How to do it…
How it works…
Converting a string to title
How to do it…
How it works…
Converting a string to uppercase
How to do it…
How it works…
Trimming a string
How to do it…
How it works…
Trimming leading whitespace
How to do it…
How it works…
Trimming trailing whitespace
How to do it…
How it works…
Locating the word end
How to do it…
How it works…
Locating the word start
How to do it…
How it works…
Performing variable substitution
How to do it…
How it works…
5. Expanding String Functionality Using Lists
Introduction
Creating a list
How to do it…
How it works…
There's more…
Joining two lists
How to do it…
How it works…
There's more…
Joining list elements
How to do it…
How it works…
There's more…
Appending list elements
How to do it…
How it works…
Assigning list elements to variables
How to do it…
How it works…
Retrieving an element from a list
How to do it…
How it works…
Inserting elements into a list
How to do it…
How it works…
Determining the number of elements
How to do it…
How it works…
Getting a list element
How to do it…
How it works…
Repeating elements
How to do it…
How it works…
There's more…
Replacing elements
How to do it…
How it works…
Reversing elements
How to do it…
How it works…
Searching a list
How to do it…
How it works…
Editing a list
How to do it…
How it works…
Sorting a list
How to do it…
How it works…
Splitting a string into a list
How to do it…
How it works…
6. The Tcl Dictionary
Introduction
Creating a dictionary
How to do it…
How it works…
There's more…
Using set with the dict create command
Appending to a dictionary
How to do it…
How it works…
Determining if a key exists
How to do it…
How it works…
Filtering a dictionary
How to do it…
How it works…
There's more…
Searching a dictionary
How to do it…
How it works…
Getting a record
How to do it…
How it works…
Incrementing a value
How to do it…
How it works…
Getting the dictionary structure
How to do it…
How it works…
Getting a list of keys
How to do it…
How it works…
Appending to an existing record
How to do it…
How it works…
Merging two dictionaries
How to do it…
How it works…
Creating a blank dictionary structure
How to do it…
How it works…
Updating variables from a dictionary
How to do it…
How it works…
Determining the size of a dictionary
How to do it…
How it works…
Getting all records
How to do it…
How it works…
Assigning values
How to do it…
7. File Operations
Introduction
Opening a file
How to do it…
How it works…
There's more…
Configuring a file
How to do it…
There's more…
Opening a command pipeline
How to do it…
How it works…
There's more…
Writing a file
How to do it…
How it works…
Reading a file
How to do it…
How it works…
Closing a file
How to do it…
How it works…
File handling
Getting ready
How to do it…
How it works…
8. Tk GUI Programming with Tcl/Tk
Introduction
Creating a widget
How to do it…
How it works…
There's more…
Writing to the console
How to do it…
How it works…
Setting the attributes of the window through window manager
How to do it…
How it works…
Creating an additional window
Standard options:
Window-specific options
Commands
How to do it…
How it works…
Destroying a window
How to do it…
How it works…
There's more…
Creating a custom dialog
How to do it…
How it works…
9. Configuring and Controlling Tk Widgets
Introduction
Creating a frame widget
How to do it…
How it works…
Creating a label widget
How to do it…
How it works…
Creating an entry widget
How to do it…
How it works…
Creating a button widget
How to do it…
How it works…
Creating a listbox widget
How to do it…
How it works…
Creating an image
Getting ready
How to do it…
How it works…
Creating a simple form
How to do it…
How it works…
10. Geometry Management
Introduction
Controlling layout with the pack command
How to do it...
How it works…
Controlling layout with the grid command
How to do it…
How it works…
Combining pack and grid
How to do it…
How it works…
Creating an address book interface
Getting ready
How to do it…
How it works…
11. Using Tcl Built-in Dialog Windows
Introduction
Displaying a message box
How to do it…
How it works…
Displaying a confirmation dialog
How to do it…
How it works…
Displaying the color picker
How to do it…
How it works…
Displaying the directory dialog
How to do it…
How it works…
Displaying the file selection dialog
How to do it…
How it works…
Selecting a directory and file
How to do it…
How it works…
12. Creating and Managing Menus
Introduction
Creating a menu
How to do it…
How it works…
Adding menu buttons
How to do it…
How it works…
Displaying a pop-up menu
How to do it…
How it works…
Data entry application
How to do it…
How it works…
13. Creating the Address Book Application
Introduction
Creating the Address Book application
How to do it…
How it works…
Adding a record
How to do it…
How it works…
Navigating records
How to do it…
How it works…
Deleting a record
How to do it…
How it works…
Finding a record
How to do it…
How it works…
Full listing
Index
Tcl/Tk 8.5 Programming Cookbook
Copyright
Copyright © 2011 Packt Publishing
Tcl/Tk 8.5 Programming Cookbook
All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews.
Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the author, nor Packt Publishing, and its dealers and distributors will be held liable for any damages caused or alleged to be caused directly or indirectly by this book.
Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information.
First published: February 2011
Production Reference: 1080211
Published by Packt Publishing Ltd. 32 Lincoln Road Olton Birmingham, B27 6PA, UK.
ISBN 978-1-849512-98-5
www.packtpub.com
Cover Image by Asher Wishkerman (<a.wishkerman@mpic.de>)
Credits
Author
Bert Wheeler
Reviewers
Clif Flynt
Thomas J. Krehbiel
Acquisition Editor
Steven Wilding
Development Editor
Susmita Panda
Technical Editor
Pooja Pande
Indexer
Hemangini Bari
Editorial Team Leader
Aanchal Kumar
Project Team Leader
Ashwin Shetty
Project Coordinator
Joel Goveya
Proofreader
Jacqueline McGhee
Production Coordinator
Adline Swetha Jesuthas
Cover Work
Adline Swetha Jesuthas
About the Author
Bert Wheeler was born and raised in Louisville, Kentucky and is one of five sons. After graduating from high school he entered the United States Navy and later retired in 1997, after 20 years as an Air Traffic Controller. Following his military career, Bert returned to college and graduated Magna Cum Laude with a degree in computer science.
After completion of his degree, Bert worked in all aspects of the IT and software industries in numerous positions including Software Design and Development, Project and Product Management, Director of Information Technologies, and Director of Engineering Services. He continues to work and his primary area of expertise is in the design and implementation of physical security solutions in the Access Control arena throughout the world.
Acknowledgment
This book would not have been completed without the help of numerous individuals. I would like to thank the staff at Packt Publishing for their meticulous efforts and editorial assistance throughout the completion of this book. Special thanks to Steven Wilding, Susmita Panda, and Joel Goveya, whose tireless efforts have guided me and kept me on track.
As with all writers, it is our families that provide the support and necessary encouragement. To Claudia, my wife, partner and best friend, and my sons Jeremy, Justin, Valentino, and Arrigo. Without your love and support throughout my lifetime, I would not have become who I am today. Thank you for everything you have done, the lessons you have taught me and the love that keeps me going. I am blessed.
About the Reviewers
Clif Flynt has been a professional programmer since 1978. He has used languages ranging from Am2900 microcode to high level languages such as APL, HTML, and Tcl/Tk. Clif has developed applications for many clients including the US Navy, General Mills, and McDonalds, and also for his own amusement.
Clif has taught computer science at Grinnell College and Eastern Michigan University. He's a member of the Washtenaw Community College Computer Science Advisory Committee. He's delivered training sessions in the US, Canada, Europe, and India, and has given talks at conferences in the US and Europe.
Clif is the president and founder of the Tcl Community Association, the organization that runs the annual Tcl/Tk conference in the US and coordinates the Tcl/Tk and Open ACS involvement with Google Summer of Code. Since 1999, Clif has owned his own company: Noumena Corporation. Noumena Corporation provides computer training and software development services, focusing on Tcl/Tk and other open source technologies.
Clif has written Tcl/Tk for Real Programmers, Tcl/Tk: A Developer's Guide; has edited Interactive Web Applications with Tcl/Tk (Academic Press, Schroeder/Doyle), Practical Programming in Tcl/Tk (Prentice Hall, Welch/Jones/Hobbs), Tcl and the Tk Toolkit (Addison Wesley, Ousterhout/Jones), and Tcl/Tk Programmer's Reference (Osborne, Nelson).
Thomas J. Krehbiel is a graduate electrical engineer with a Masters' in solid state semiconductor and device physics. During his career in the semiconductor industry, he did IC circuit design, IC device design, managed a parametric test area, managed a CIM group, and worked in a RET (Reticle Enhancement Technologies) software development group. Along the way, he created a parametric data analysis system, a wafer tracking system, and a RET processing system.
Thomas has many years of experience managing software development and the hardware and system associated with that development. He has programmed for over 30 years, starting with FORTRAN and ending with his current favorite language Tcl/Tk. As computers changed, Tom worked with mainframes (IBM360), minis (DEC), and desktops (HPUX, Solaris, Linux, Windows).
When he was younger, he enjoyed playing baseball, basketball, handball, tennis, hiking the Grand Canyon, and snow skiing.
www.PacktPub.com
Support files, eBooks, discount offers and more
You might want to visit www.PacktPub.com for support files and downloads related to your book.
Did you know that Packt offers eBook versions of every book published, with PDF and ePub files available? You can upgrade to the eBook version at www.PacktPub.com and as a print book customer, you are entitled to a discount on the eBook copy. Get in touch with us at
At www.PacktPub.com, you can also read a collection of free technical articles, sign up for a range of free newsletters and receive exclusive discounts and offers on Packt books and eBooks.
http://PacktLib.PacktPub.com
Do you need instant solutions to your IT questions? PacktLib is Packt's online digital book library. Here, you can access, read and search across Packt's entire library of books.
Why Subscribe?
Fully searchable across every book published by Packt
Copy & paste, print and bookmark content
On demand and accessible via web browser
Free Access for Packt account holders
If you have an account with Packt at www.PacktPub.com, you can use this to access PacktLib today and view nine entirely free books. Simply use your login credentials for immediate access.
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