PacktLib: Tcl/Tk 8.5 Programming Cookbook

Tcl/Tk 8.5 Programming Cookbook


About the Author


About the Reviewers


The Tcl Shell


The Tcl shell

Writing to the Tcl console

Mathematical expressions

Tcl expr operands

Mathematical functions

Computing mathematical expressions

Referencing files in Tcl


Command line arguments

Using the Building Blocks Control Constructs


Controlling flow with the if statement

Looping with for

Looping with foreach

Looping with while

Continuing a procedure

Breaking out of a procedure

Nested looping

Error Handling


Using the catch command

Using the eval command

Using the error command

Error handling procedure

Handling String Expressions


Appending to a string

Formatting a string

Matching a regular expression within a string

Performing character substitution on a string

Parsing a string using conversion specifiers

Determining the length of a string

Comparing strings

Comparing a string of characters

Locating the first instance of a character

Locating the index of a character

Determining the class of a string

Locating the last instance of a string

Determining the size of a string

Replacing values within a string

Locating a pattern within a string

Returning a range of characters from a string

Creating a string of repeated characters

Replacing ranges of characters contained within a string

Creating a reverse string

Converting a string to lowercase

Converting a string to title

Converting a string to uppercase

Trimming a string

Trimming leading whitespace

Trimming trailing whitespace

Locating the word end

Locating the word start

Performing variable substitution

Expanding String Functionality Using Lists


Creating a list

Joining two lists

Joining list elements

Appending list elements

Assigning list elements to variables

Retrieving an element from a list

Inserting elements into a list

Determining the number of elements

Getting a list element

Repeating elements

Replacing elements

Reversing elements

Searching a list

Editing a list

Sorting a list

Splitting a string into a list

The Tcl Dictionary


Creating a dictionary

Appending to a dictionary

Determining if a key exists

Filtering a dictionary

Searching a dictionary

Getting a record

Incrementing a value

Getting the dictionary structure

Getting a list of keys

Appending to an existing record

Merging two dictionaries

Creating a blank dictionary structure

Updating variables from a dictionary

Determining the size of a dictionary

Getting all records

Assigning values

File Operations


Opening a file

Configuring a file

Opening a command pipeline

Writing a file

Reading a file

Closing a file

File handling

Tk GUI Programming with Tcl/Tk


Creating a widget

Writing to the console

Setting the attributes of the window through window manager

Creating an additional window

Configuring and Controlling Tk Widgets


Creating a frame widget

Creating a label widget

Creating an entry widget

Creating a button widget

Creating a listbox widget

Creating an image

Creating a simple form

Geometry Management


Controlling layout with the pack command

Controlling layout with the grid command

Combining pack and grid

Creating an address book interface

Using Tcl Built-in Dialog Windows


Displaying a message box

Displaying a confirmation dialog

Displaying the color picker

Displaying the directory dialog

Displaying the file selection dialog

Selecting a directory and file

Creating and Managing Menus


Creating a menu

Adding menu buttons

Displaying a pop-up menu

Data entry application

Creating the Address Book Application


Creating the Address Book application

Adding a record

Navigating records

Deleting a record

Finding a record

Full listing