You are previewing Tcl/Tk Programming for the Absolute Beginner.
O'Reilly logo
Tcl/Tk Programming for the Absolute Beginner

Book Description

Tcl/Tk is enjoying a resurgence of popularity and interest in the computing community due to the fact that it is relatively easy to learn, powerful, fast, permits rapid development, and runs on all computing platforms. Until now, there has not been a guide book available that teaches Tcl/Tk to those with little or no programming experience. Covering the newest versions of Tcl and Tk, Tcl and Tk Programming for the Absolute Beginner teaches beginning and novice programmers concepts such as loops, conditional execution, input and output, and events, and shows readers how to implement these concepts using the grammar and syntax of the Tcl language. As part of the for the absolute beginner™ series, the book teaches all the concepts through the creation of simple computer games. Not only will this "learn by doing" approach provide you with an instant sense of accomplishment, but it's also a fun way to learn. Plus, in addition to learning Tcl and Tk, you'll also learn the basics of computer programming, so you'll have a solid foundation from which you can confidently jump to other programming languages.

Table of Contents

  1. Copyright
    1. Dedication
  2. Acknowledgments
  3. About the Author
  4. Introduction
    1. Who This Book Is For
    2. How This Book Is Organized
      1. Conventions Used in This Book
      2. Where’s the Code?
  5. 1. Introducing Tcl and Tk
    1. What Is Tcl?
    2. What Is Tk?
    3. What Makes Tcl and Tk Different?
    4. Why Use Tcl and Tk?
    5. Getting Tcl and Tk
      1. Installing Tcl and Tk on Linux
      2. Installing Tcl and Tk on Windows
      3. Installing Tcl and Tk on OS X
      4. Installing Tcl and Tk from Source
  6. 2. Running Tcl Programs
    1. Invoking the Interpreter
    2. Executing Tcl Commands Interactively
    3. Creating Tcl Command Files
  7. 3. Doing Mathematics
    1. Guessing Numbers
    2. Language Fundamentals
      1. Comments
      2. Commands
      3. Command Substitution
      4. Grouping
      5. Grouping with Double Quotes
      6. Grouping with Braces
      7. Variables
      8. Procedures
    3. Getting User Input
    4. Basic Mathematical Operators
    5. Conditional Execution: The if Command
    6. Analyzing the Guessing Numbers Program
      1. Looking at the Code
      2. Understanding the Code
      3. Modifying the Code
  8. 4. Strings, Strings, Everywhere Strings!
    1. Mad Libs
    2. The string Command
    3. Comparing Strings
      1. The compare Option
      2. The equal Option
      3. The match Option
    4. Inspecting Strings
      1. The length and bytelength Options
      2. The index Option
      3. The first and last Options
      4. The range Option
      5. The replace Option
      6. The is Option
    5. Modifying Strings
      1. Repeating Strings
      2. Switching Case
      3. Trimming Strings
      4. Appending Strings
    6. Looping Commands
      1. Looping with the while Command
      2. Iterative Loops: The for Command
    7. Analyzing Mad Libs
      1. Looking at the Code
      2. Understanding the Code
      3. Modifying the Code
  9. 5. Working with Lists
    1. Playing Blackjack
    2. What Is a Tcl List?
    3. Creating Lists
    4. Appending Lists
    5. Merging Lists
    6. Accessing List Elements
      1. Accessing Specific List Elements
    7. Modifying Lists
      1. Inserting New Elements
      2. Replacing Elements
    8. Searching and Sorting Lists
      1. Searching 101
      2. Sorting
    9. Additional List Operations
      1. Strings to Lists
      2. Lists to Strings
    10. Looping with the foreach Command
    11. Conditional Execution: The switch Command
    12. Interrupting Loop Execution
    13. Analyzing Playing Blackjack
      1. Looking at the Code
      2. Understanding the Code
      3. Modifying the Code
  10. 6. Creating and Using Arrays
    1. What’s the Word?
    2. The Differences Between Arrays and Lists
    3. Working with Arrays
      1. Getting Information About Arrays
      2. Converting Lists to Arrays
      3. Converting Arrays to Lists
      4. Retrieving Array Elements
      5. Searching Arrays
    4. Grace Under Pressure
      1. Dealing with Exceptions: The catch Command
      2. Raising Errors: The error Command
    5. Examining Variables
    6. Analyzing What’s the Word?
      1. Looking at the Code
      2. Understanding the Code
      3. Modifying the Code
  11. 7. Writing Tcl Procedures
    1. Fortune Teller
    2. What Is a Procedure?
    3. Defining Procedures
      1. Defining Procedures with Default Values
      2. Defining Procedures with Variable Arguments
    4. Understanding Variable and Procedure Scope
    5. Analyzing Fortune Teller
      1. Looking at the Code
      2. Understanding the Code
      3. Modifying the Code
  12. 8. Accessing Files and Directories
    1. Word Search
    2. Opening and Closing Files
    3. Reading Files
      1. Using gets for File Input
      2. Using read for File Input
    4. Writing Files
      1. Using puts for Output
      2. Formatting Output with format
    5. Moving the File Pointer: Random Access I/O
    6. Working with Directories
    7. Analyzing Word Search
      1. Looking at the Code
      2. Understanding the Code
      3. Modifying the Code
  13. 9. Understanding Tk Programming
    1. Hello, Tk World!
    2. Components of a Tk Application
    3. Naming Tk Widgets
    4. Understanding Event-Driven Programming
    5. Widget Options
    6. Surveying Tk’s Widgets
  14. 10. Button Widgets
    1. Memory Test
    2. Packed and Ready: The pack Geometry Manager
    3. Button, Button, Who’s Got the Button?
      1. Plain Vanilla Buttons
      2. Check Buttons
      3. Radio Buttons
    4. A Smörgåsbord of Menus
      1. Creating a Basic Menu Bar
    5. Binding Commands to Events
    6. Coloring Your World
    7. Analyzing Memory Test
      1. Looking at the Code
      2. Understanding the Code
      3. Modifying the Code
  15. 11. Windows, Frames, and Messages
    1. Tic-Tac-Toe
    2. On the Grid: The Grid Geometry Manager
      1. Relative Positioning
      2. Absolute Positioning
      3. Positioning and Padding
      4. Spanning Rows and Columns
      5. Operating on Rows and Columns
        1. Setting a Minimum Size
        2. Padding Rows and Columns
        3. Resizing Widgets
    3. I’ve Been Framed!
      1. Frames
      2. Labelframes
    4. Labeling the Contents
    5. Creating New Windows
    6. Did You Get the Message?
    7. Analyzing Tic-Tac-Toe
      1. Looking at the Code
      2. Understanding the Code
      3. Modifying the Code
  16. 12. Entry and Spinbox Widgets
    1. Mad Libs Revista
    2. Features of the Entry and Spinbox Widgets
    3. Entry and Spinbox Attributes
    4. Validating User Input
    5. Building a Better Message Box
    6. Analyzing Mad Libs Revista
      1. Looking at the Code
      2. Understanding the Code
      3. Modifying the Code
  17. 13. Listbox Widgets
    1. Matching Lists
    2. Creating a Listbox
    3. Selecting Listbox Content
      1. Setting the Selection Mode
      2. Determining the Selected Items
      3. Selecting Items Programmatically
    4. Analyzing Matching Lists
      1. Looking at the Code
      2. Understanding the Code
      3. Modifying the Code
  18. 14. Scrollbar, Scale, and Text Widgets
    1. Word Search
    2. Using the Scrollbars to Move the Viewport
      1. Simple Scrolling
      2. Probing the scrollbar Protocol
    3. Using the scale Widget
    4. Using the Text Widget
    5. Getting Started
      1. Adding a Scrollbar
      2. Adding and Populating a Menu
    6. Using Marks and Tags
      1. Text Indices
      2. Hitting the Mark
      3. Tag, You’re It!
    7. Analyzing Word Search
      1. Looking at the Code
      2. Understanding the Code
      3. Modifying the Code
  19. 15. The Canvas Widget
    1. Got the Picture?
    2. The Canvas Widget
      1. The Coordinate System
      2. Using Canvas Objects
        1. Drawing Arcs
        2. Drawing Bitmaps
        3. Drawing Images
          1. Using the image Command
          2. Using the image Canvas Object
        4. Drawing Lines
        5. Drawing Ovals
        6. Drawing Polygons
        7. Drawing Text
    3. Analyzing Got the Picture?
      1. Looking at the Code
      2. Understanding the Code
      3. Modifying the Code
  20. A. Tcl Command Summary
  21. B. Tk Command Summary