Introduction

Tcl and Tk are enjoying a resurgence of popularity and interest in the computing community. There are some fine books dedicated to Tcl and Tk programming, but the book you hold in your hands addresses what I see as an underserved market, Tcl and Tk neophytes with little or no programming experience. Like other books in the Absolute Beginner series, I use simple games as a vehicle to demonstrate language-specific features and more general programming concepts.

Tcl is an uncomplicated language. With surprisingly few syntax rules and a limited yet comprehensive set of commands, technically competent readers can become competent Tcl programmers with a few weeks of practice. While Tcl, and its graphical extension, Tk, are simple to learn and use, they are remarkably powerful and can be used to create sophisticated, powerful, and full-featured applications in a short amount of time. In addition, the Tcl and Tk development community, by which I mean the people who develop the language and the (growing) number of people who use it on a day-to-day basis, is friendly, knowledgeable, and helpful. This is all to say that you can be productive without needing to be a Tcl or Tk guru, but when you need guru-level help, it is readily available.

Who This Book Is For

What I wanted to create was a book that reflects the way I learn new programming languages. I learn best when introduced to a command, get to see simple examples that illustrate its use, and then am provided with enough information to facilitate and encourage experimentation. Toward that end, almost all of the examples in this book are complete and stand on their own. They don’t rely on code from other chapters to work. I also reuse and rework earlier examples so you can see how functionality evolves. Finally, I provided a lot of examples. I firmly believe that the only way to learn a new programming language is to look at a lot of examples of other people’s code.

I wrote this book with my own experience learning Tcl and Tk in mind. I’d found that the available books were either dated and based on ancient versions of Tcl and Tk or advanced texts that assumed either prior experience with Tcl and Tk or significant experience with other programming languages. There was plenty of tutorial material available on the Web, and the demonstration programs that came with the Tcl and Tk distributions were also excellent resources, but I couldn’t find a single source that introduced the fundamental concepts that inform all Tcl programming, introduced Tcl commands with simple examples, or that covered most Tcl and Tk commands in a comprehensive way.

How This Book Is Organized

I organized this book into two parts. The first part, consisting of Chapters 18, introduces Tcl programming. The second part, Chapters 915, covers Tk programming. My approach is cumulative; you need to read and understand the material in one chapter before you proceed to the next. In some cases, I use commands in one chapter that I don’t discuss until later in the book. I apologize in advance for this, but there are certain commands you need to know how to use in order to have a complete, functioning Tcl script. I clearly note these situations, and there aren’t many of them.

I have not attempted to write a Tcl and Tk reference manual. In fact, I have deliberately avoided writing an exhaustive tome. I have not described every Tcl and Tk command that exists, have not covered every option and attribute of the commands I do discuss, and have avoided covering corner cases and elements of the language that I consider obscure or that would just serve to complicate the text or confuse newcomers to the language. In general, I wanted to cover the common case—the tasks that most people just beginning with Tcl and Tk want to perform. You could say I have tried to write a book that shows you how to use the 80 percent of Tcl and Tk’s functionality; the other 20 percent is useful, but you won’t need it for most of your programming with Tcl.

So you’ll know what you’re getting into, the chapter-by- chapter description follows.

  • Chapter 1, “Introducing Tcl and Tk,” consists of a short history of Tcl and Tk, a section highlighting their salient features, and a description of their distinguishing characteristics. If your computer doesn’t already have Tcl and Tk installed, then don’t skip “Getting Tcl and Tk,” or you won’t be able to work through the example programs, which would certainly defeat the purpose of buying this book.

  • Chapter 2, “Running TCL Programs, describes how to use the Tcl interpreter interactively and in batch mode. Tcl is an interpreted language, so Tcl commands must be executed by an interpreter, tclsh, instead of being compiled and then executed directly. You can use tclsh interactively, allowing you to enter commands and see their results immediately. You can also save your Tcl commands to a file, called a command file (imaginative name, yes?) or script, and have the Tcl interpreter execute the script. After you become familiar with Tcl’s simple syntax, I think you will find it much more efficient to save your scripts in a file and execute them by passing the command file to the interpreter.

  • Chapter 3, “Doing Mathematics,” introduces the fundamental elements of the Tcl language, such as comments, variables, expressions, and commands. You also get your first exposure to the two most difficult elements of Tcl: command substitution and grouping. Command substitution describes the manner in which programming statements are built from Tcl’s built-in commands and the results of those commands. Grouping refers to the way in which the operators "" and {} affect how command substitution works. This chapter also teaches you how to perform mathematical operations using Tcl’s expr command.

  • After learning how to perform basic math using Tcl, Chapter 4, “Strings, Strings, Everywhere Strings!,” shows you how to perform string operations. Tcl has a rich set of commands and functionality for manipulating strings, an unsurprising fact when you consider that Tcl is a string-based programming language. The final section continues the discussion of Tcl control structures I started in the previous chapter by introducing two looping commands: while and for.

  • Lists are one of Tcl’s two native or built-in data structures. In Chapter 5, “Working with Lists,” you spend some quality time with lists. Tcl has a broad set of commands for dealing with lists, and this chapter will get you up to speed with them. I also finish up the discussion of control structures by introducing the switch command, another command used for conditional execution, and the foreach command, a looping control structure that specializes in iterating over list items. The chapter ends with the two commands you can use to interrupt loop execution: break and continue.

  • Chapter 6, “Creating and Using Arrays,” is devoted mostly to arrays. Tcl arrays, like Perl’s hashes, are associative, meaning that they are indexed by strings, rather than integers or other numeric types. In addition to learning how to create and use arrays, this chapter also shows you commands and techniques for handling errors. Error handling combines well with material on arrays because common mistakes that occur when using arrays (such as accessing out-of-bounds or non-existent array indices) raise errors that need to be handled gracefully.

  • Procedures are covered in depth in Chapter 7, “Writing Tcl Procedures.” Procedures enable you to replace a commonly used sequence of commands with a single new command. Known as subroutines or functions in other programming languages, Tcl procedures can be called with or without arguments. You will also learn about variable and procedure scope, which determines when and where variables and procedures are visible. Together, procedures and an understanding of variable and procedure scope give you the tools you need to start implementing your Tcl scripts in a more modular and easy-to-maintain manner.

  • Most non-trivial programs involve interacting with the host filesystem. In Chapter 8, “Accessing Files and Directories,” you’ll learn how to open, close, delete, and rename files. The chapter also shows you how to perform file I/O using the puts (output) and gets (input) commands and how to use the format command to “pretty print” output. Finally, you’ll learn how to navigate the filesystem programmatically and work with file and directory names in a platform-neutral manner.

  • Chapter 9, “Understanding Tk Programming,” starts the discussion of graphical programming, introducing programming in Tk. As an introductory chapter, this chapter is light on code and long on text, as it discusses topics including event-driven programming and widget attributes and operations. Covering this information here simplifies my job in the rest of the chapters because most Tk programming assumes familiarity with material presented in this chapter. The chapter closes with a description of each of the widgets available to Tk programming.

  • Unless you’ve been living in an unelectrified cave for the last decade, you are accustomed to clicking buttons. Chapter 10, “Button Widgets,” describes how to program Tk buttons. After providing more information about the first of Tk’s three geometry managers, pack, this chapter looks at Tk’s button widgets. In addition to learning how to use buttons, I’ll show you how to use color in a Tk application and how to bind buttons to commands and events

  • Chapter 11, “Windows, Frames, and Messages,” shows you how to use the grid geometry manager; I think you’ll like grid better than pack. I’ll also introduce you to three more Tk widgets: frames, toplevels (that is, top-level windows), and messages.

  • Chapter 12, “Entry and Spinbox Widgets,” introduces the entry and spinbox widgets. Tk’s entry widget is a specialized type of text-entry field best suited to high-speed, head-down data entry, but applicable for many types of data entry in which you want to control or validate the data that is input. The spinbox widget, often referred to as a spinner in other GUI toolkits, is based on the entry widget.

  • In Chapter 13, “Listbox Widgets,” you learn how to use Tk’s listbox widget. A listbox displays a series of read-only text lines. The list is vertically scrollable and can be scrolled horizontally as necessary. You can select zero, one, or more items in a list, so the listbox widget has methods for determining which items are selected (and for selecting items programmatically). You can add and delete items from a listbox, but items themselves cannot be edited. As usual, you can also control the colors, relief, and other visual attributes of listbox widgets.

  • Chapter 14, “Scrollbar, Scale, and Text Widgets,” discusses three Tk widgets: scrollbars, scales, and text boxes. Scrollbars allow you or your users to scroll the viewable area of a window. A scale widget is a slider whose value changes as the slider is moved. Text widgets provide areas for displaying and editing text. As you will see later in the chapter, Tk’s text widget is a full-featured text display and manipulation tool. The price of this feature set is that the text widget is complex.

  • Chapter 15, “The Canvas Widget,” concludes the book with a tour of the canvas widget. The canvas is a general purpose widget you can use to display drawing primitives, such as arcs, lines, polygons, and other shapes; images in a variety of formats; text; and even other embedded widgets. I will show you how to use many of the canvas widget’s other features in this chapter.

Conventions Used in This Book

A note on textual conventions used in the text is in order. Code listings are shown in a monospaced font. Similarly, code that appears in text is also shown in a monospaced font. Commands or text that you type will appear in bold, monospaced font. Placeholders, such as variable names in syntax diagrams, are shown in italicized monospaced font. Finally, new terms and phrases are shown in regular-faced italicized text.

Where’s the Code?

The source code for the example code can be downloaded from the book’s companion Web site at http://www.courseptr.com/downloads (search by author, ISBN, or title) or from my personal Web site at http://www.kurtwerks.org/bookwerks/tcl/. In addition, suggested solutions for the end-of-chapter exercises can also be downloaded from the companion Web site or from my personal Web site.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.1.51