Product Cover Image

Tcl and the Tk Toolkit,, 2nd Edition

By John Ousterhout, Ken Jones

Published by Addison-Wesley Professional

Published Date: Sep 2, 2009

Description

John K. Ousterhout’s Definitive Introduction to Tcl/Tk—Now Fully Updated for Tcl/Tk 8.5

 

Tcl and the Tk Toolkit, Second Edition, is the fastest way for newcomers to master Tcl/Tk and is the most authoritative resource for experienced programmers seeking to gain from Tcl/Tk 8.5’s powerful enhancements. Written by Tcl/Tk creator John K. Ousterhout and top Tcl/Tk trainer Ken Jones, this updated volume provides the same extraordinary clarity and careful organization that made the first edition the world’s number one Tcl/Tk tutorial.

 

Part I introduces Tcl/Tk through simple scripts that demonstrate its value and offer a flavor of the Tcl/Tk scripting experience. The authors then present detailed, practical guidance on every feature necessary to build effective, efficient production applications—including variables, expressions, strings, lists, dictionaries, control flow, procedures, namespaces, file and directory management, interprocess communication, error and exception handling, creating and using libraries, and more.

 

Part II turns to the Tk extension and Tk 8.5’s new themed widgets, showing how to organize sophisticated user interface elements into modern GUI applications for Tcl.

 

Part III presents incomparable coverage of Tcl’s C functions, which are used to create new commands and packages and to integrate Tcl with existing C software—thereby leveraging Tcl’s simplicity while accessing C libraries or executing performance-intensive tasks.

 

Throughout, the authors illuminate all of Tcl/Tk 8.5’s newest, most powerful improvements. You’ll learn how to use new Starkits and Starpacks to distribute run-time environments and applications through a single file; how to take full advantage of the new virtual file system support to treat entities such as zip archives and HTTP sites as mountable file systems; and more.

 

From basic syntax to simple Tcl commands, user interface development to C integration, this fully updated classic covers it all. Whether you’re using Tcl/Tk to automate system/network administration, streamline testing, control hardware, or even build desktop or Web applications, this is the one Tcl/Tk book you’ll always turn to for answers.

Table of Contents

Preface xxv

Preface to the First Edition xxvii

Introduction xxxi

 

PART I: The Tcl Language 1

 

Chapter 1: An Overview of Tcl and Tk 3

1.1: Getting Started 3

1.2: “Hello, World!” with Tk 6

1.3: Script Files 8

1.4: Variables and Substitutions 11

1.5: Control Structures 12

1.6: On the Tcl Language 14

1.7: Event Bindings 15

1.8: Additional Features of Tcl and Tk 19

 

Chapter 2: Tcl Language Syntax 21

2.1: Scripts, Commands, and Words 21

2.2: Evaluating a Command 22

2.3: Variable Substitution 24

2.4: Command Substitution 26

2.5: Backslash Substitution 26

2.6: Quoting with Double Quotes 28

2.7: Quoting with Braces 29

2.8: Argument Expansion 30

2.9: Comments 32

2.10: Normal and Exceptional Returns 35

2.11: More on Substitutions 36

 

Chapter 3: Variables 39

3.1: Commands Presented in This Chapter 39

3.2: Simple Variables and the set Command 41

3.3: Tcl’s Internal Storage of Data 41

3.4: Arrays 42

3.5: Variable Substitution 43

3.6: Multidimensional Arrays 45

3.7: Querying the Elements of an Array 46

3.8: The incr and append Commands 47

3.9: Removing Variables: unset and array unset 49

3.10: Predefined Variables 49

3.11: Preview of Other Variable Facilities 50

 

Chapter 4: Expressions 53

4.1: Commands Presented in This Chapter 53

4.2: Numeric Operands 54

4.3: Operators and Precedence 55

4.4: Math Functions 58

4.5: Substitutions 60

4.6: String Manipulation 62

4.7: List Manipulation 63

4.8: Types and Conversions 63

4.9: Precision 64

 

Chapter 5: String Manipulation 65

5.1: Commands Presented in This Chapter 65

5.2: Extracting Characters: string index and string range 69

5.3: Length, Case Conversion, Trimming, and Repeating 70

5.4: Simple Searching 70

5.5: String Comparisons 71

5.6: String Replacements 72

5.7: Determining String Types 73

5.8: Generating Strings with format 74

5.9: Parsing Strings with scan 76

5.10: Glob-Style Pattern Matching 78

5.11: Pattern Matching with Regular Expressions 79

5.12: Using Regular Expressions for Substitutions 87

5.13: Character Set Issues 89

5.14: Message Catalogs 91

5.15: Binary Strings 95

 

Chapter 6: Lists 101

6.1: Commands Presented in This Chapter 101

6.2: Basic List Structure and the lindex and llength Commands 103

6.3: Creating Lists: list, concat, and lrepeat 105

6.4: Modifying Lists: lrange, linsert, lreplace, lset, and lappend 106

6.5: Extracting List Elements: lassign 109

6.6: Searching Lists: lsearch 110

6.7: Sorting Lists: lsort 111

6.8: Converting between Strings and Lists: split and join 111

6.9: Creating Commands as Lists 113

 

Chapter 7: Dictionaries 115

7.1: Commands Presented in This Chapter 116

7.2: Basic Dictionary Structure and the dict get Command 118

7.3: Creating and Updating Dictionaries 120

7.4: Examining Dictionaries: The size, exists, keys, and for Subcommands 122

7.5: Updating Dictionary Values 123

7.6: Working with Nested Dictionaries 126

 

Chapter 8: Control Flow 131

8.1: Commands Presented in This Chapter 131

8.2: The if Command 132

8.3: The switch Command 133

8.4: Looping Commands: while, for, and foreach 136

8.5: Loop Control: break and continue 138

8.6: The eval Command 139

8.7: Executing from Files: source 140

 

Chapter 9: Procedures 143

9.1: Commands Presented in This Chapter 143

9.2: Procedure Basics: proc and return 144

9.3: Local and Global Variables 146

9.4: Defaults and Variable Numbers of Arguments 146

9.5: Call by Reference: upvar 148

9.6: Creating New Control Structures: uplevel 150

9.7: Applying Anonymous Procedures 151

 

Chapter 10: Namespaces 155

10.1: Commands Presented in This Chapter 155

10.2: Evaluating Tcl Code in a Namespace 158

10.3: Manipulating Qualified Names 161

10.4: Exporting and Importing Namespace Commands 162

10.5: Inspecting Namespaces 163

10.6: Working with Ensemble Commands 164

10.7: Accessing Variables from Other Namespaces 169

10.8: Controlling the Name Resolution Path 170

 

Chapter 11: Accessing Files 173

11.1: Commands Presented in This Chapter 173

11.2: Manipulating File and Directory Names 176

11.3: The Current Working Directory 179

11.4: Listing Directory Contents 179

11.5: Working with Files on Disk 181

11.6: Reading and Writing Files 185

11.7: Virtual File Systems 194

11.8: Errors in System Calls 196

 

Chapter 12: Processes and Interprocess Communication 197

12.1: Commands Presented in This Chapter 197

12.2: Terminating the Tcl Process with exit 199

12.3: Invoking Subprocesses with exec 199

12.4: I/O to and from a Command Pipeline 203

12.5: Configuring Channel Options 204

12.6: Event-Driven Channel Interaction 206

12.7: Process IDs 209

12.8: Environment Variables 210

12.9: TCP/IP Socket Communication 210

12.10: Sending Commands to Tcl Programs 214

 

Chapter 13: Errors and Exceptions 219

13.1: Commands Presented in This Chapter 219

13.2: What Happens after an Error? 220

13.3: Generating Errors from Tcl Scripts 222

13.4: Trapping Errors with catch 222

13.5: Exceptions in General 223

13.6: Background Errors and bgerror 227

 

Chapter 14: Creating and Using Tcl Script Libraries 229

14.1: Commands Presented in This Chapter 230

14.2: The load Command 232

14.3: Using Libraries 232

14.4: Autoloading 233

14.5: Packages 234

14.6: Tcl Modules 239

14.7: Packaging Your Scripts as Starkits 242

 

Chapter 15: Managing Tcl Internals 247

15.1: Commands Presented in This Chapter 247

15.2: Time Delays 253

15.3: Time and Date Manipulation 254

15.4: Timing Command Execution 259

15.5: The info Command 259

15.6: Tracing Operations on Simple Variables 264

15.7: Tracing Array Variables 268

15.8: Renaming and Deleting Commands 269

15.9: Tracing Commands 270

15.10: Unknown Commands 272

15.11: Slave Interpreters 274

 

Chapter 16: History 283

16.1: Commands Presented in This Chapter 283

16.2: The History List 284

16.3: Specifying Events 285

16.4: Re-executing Commands from the History List 285

16.5: Shortcuts Implemented by unknown 286

16.6: Current Event Number: history nextid 287

 

PART II: Writing Scripts for Tk 289

 

Chapter 17: An Introduction to Tk 291

17.1: A Brief Introduction to Windowing Systems 292

17.2: Widgets 294

17.3: Applications, Toplevel Widgets, and Screens 296

17.4: Scripts and Events 297

17.5: Creating and Destroying Widgets 297

17.6: Geometry Managers 298

17.7: Widget Commands 299

17.8: Commands for Interconnection 300

 

Chapter 18: A Tour of the Tk Widgets 303

18.1: Widget Basics 304

18.2: Frames 306

18.3: Color Options 307

18.4: Toplevels 308

18.5: Labels 309

18.6: Labelframes 312

18.7: Buttons 312

18.8: Listboxes 317

18.9: Scrollbars 318

18.10: Scales 321

18.11: Entries 323

18.12: Menus 327

18.13: Panedwindow 334

18.14: Standard Dialogs 337

18.15: Other Common Options 339

 

Chapter 19: Themed Widgets 343

19.1: Comparing Classic and Themed Widgets 343

19.2: Combobox 345

19.3: Notebook 346

19.4: Progressbar 349

19.5: Separator 350

19.6: Sizegrip 350

19.7: Treeview 350

19.8: Themed Widget States 358

19.9: Themed Widget Styles 360

19.10: Other Standard Themed Widget Options 364

 

Chapter 20: Fonts, Bitmaps, and Images 367

20.1: Commands Presented in This Chapter 367

20.2: The font Command 369

20.3: The image Command 375

 

Chapter 21: Geometry Managers 385

21.1: Commands Presented in This Chapter 386

21.2: An Overview of Geometry Management 387

21.3: The Gridder 389

21.4: The Packer 396

21.5: Padding 404

21.6: The Placer 405

21.7: Hierarchical Geometry Management 405

21.8: Widget Stacking Order 407

21.9: Other Geometry Manager Options 408

21.10: Other Geometry Managers in Tk 409

 

Chapter 22: Events and Bindings 413

22.1: Commands Presented in This Chapter 413

22.2: Events 414

22.3: An Overview of the bind Command 416

22.4: Event Patterns 417

22.5: Sequences of Events 419

22.6: Substitutions in Scripts 419

22.7: Conflict Resolution 421

22.8: Event-Binding Hierarchy 422

22.9: When Are Events Processed? 423

22.10: Named Virtual Events 425

22.11: Generating Events 427

22.12: Logical Actions 428

22.13: Other Uses of Bindings 431

 

Chapter 23: The Canvas Widget 433

23.1: Canvas Basics: Items and Types 433

23.2: Manipulating Items with Identifiers and Tags 436

23.3: Bindings 439

23.4: Canvas Scrolling 444

23.5: PostScript Generation 445

 

Chapter 24: The Text Widget 447

24.1: Text Widget Basics 447

24.2: Text Indices and Marks 450

24.3: Search and Replace 451

24.4: Text Tags 453

24.5: Virtual Events 458

24.6: Embedded Windows 459

24.7: Embedded Images 460

24.8: Undo 462

24.9: Peer Text Widgets 464

 

Chapter 25: Selection and the Clipboard 467

25.1: Commands Presented in This Chapter 468

25.2: Selections, Retrievals, and Types 469

25.3: Locating and Clearing the Selection 470

25.4: Supplying the Selection with Tcl Scripts 471

25.5: The clipboard Command 473

25.6: Drag and Drop 474

 

Chapter 26: Window Managers 477

26.1: Commands Presented in This Chapter 478

26.2: Window Sizes 481

26.3: Window Positions 482

26.4: Gridded Windows 483

26.5: Window States 484

26.6: Decorations 485

26.7: Special Handling: Transients, Groups, and Override-Redirect 486

26.8: System-Specific Window Attributes 487

26.9: Dockable Windows 488

26.10: Window Close 489

26.11: Session Management 490

 

Chapter 27: Focus, Modal Interaction, and Custom Dialogs 491

27.1: Commands Presented in This Chapter 491

27.2: Input Focus 493

27.3: Modal Interactions 495

27.4: Custom Dialogs 499

 

Chapter 28: More on Configuration Options 505

28.1: Commands Presented in This Chapter 505

28.2: The Option Database 506

28.3: Option Database Entries 507

28.4: The RESOURCE_MANAGER Property and .Xdefaults File 508

28.5: Priorities in the Option Database 509

28.6: The option Command 510

28.7: The configure Widget Command 511

28.8: The cget Widget Command 512

 

Chapter 29: Odds and Ends 513

29.1: Commands Presented in This Chapter 513

29.2: Destroying Widgets 514

29.3: The update Command 514

29.4: Information about Widgets 516

29.5: The tk Command 516

29.6: Variables Managed by Tk 517

29.7: Ringing the Bell 518

 

PART III: Writing Tcl Applications in C 519

 

Chapter 30: Tcl and C Integration Philosophy 521

30.1: Tcl versus C: Where to Draw the Line 523

30.2: Resource Names–Connecting C Constructs to Tcl 524

30.3: “Action-Oriented” versus “Object-Oriented” 525

30.4: Representing Information 526

 

Chapter 31: Interpreters 527

31.1: Functions Presented in This Chapter 527

31.2: Interpreters 529

31.3: A Simple Tcl Application 530

31.4: Deleting Interpreters 531

31.5: Multiple Interpreters 531

 

Chapter 32: Tcl Objects 533

32.1: Functions Presented in This Chapter 534

32.2: String Objects 537

32.3: Numerical Objects 537

32.4: Fetching C Values from Objects 538

32.5: The Dynamic Nature of a Tcl Object 539

32.6: Byte Arrays 540

32.7: Composite Objects 540

32.8: Reference Counting 540

32.9: Shared Objects 541

32.10: New Object Types 542

32.11: Parsing Strings 543

32.12: Memory Allocation 544

 

Chapter 33: Evaluating Tcl Code 545

33.1: Functions Presented in This Chapter 545

33.2: Evaluating Tcl Code 546

33.3: Dynamically Building Scripts 548

33.4: Tcl Expressions 549

 

Chapter 34: Accessing Tcl Variables 551

34.1: Functions Presented in This Chapter 551

34.2: Setting Variable Values 553

34.3: Reading Variables 555

34.4: Unsetting Variables 556

34.5: Linking Tcl and C Variables 556

34.6: Setting and Unsetting Variable Traces 558

34.7: Trace Callbacks 559

34.8: Whole-Array Traces 561

34.9: Multiple Traces 561

34.10: Unset Callbacks 562

 

Chapter 35: Creating New Tcl Commands 563

35.1: Functions Presented in This Chapter 563

35.2: Command Functions 566

35.3: Registering Commands 567

35.4: The Result Protocol 569

35.5: Tcl_AppendResult 569

35.6: Tcl_SetResult and interp->result 570

35.7: clientData and Deletion Callbacks 572

35.8: Deleting Commands 575

35.9: Fetching and Setting Command Parameters 576

35.10: How Tcl Procedures Work 578

35.11: Command Traces 579

 

Chapter 36: Extensions 581

36.1: Functions Presented in This Chapter 581

36.2: The Init Function 582

36.3: Packages 583

36.4: Namespaces 584

36.5: Tcl Stubs 584

36.6: The ifconfig Extension 585

 

Chapter 37: Embedding Tcl 593

37.1: Functions Presented in This Chapter 593

37.2: Adding Tcl to an Application 594

37.3: Initialize Tcl 595

37.4: Creating New Tcl Shells 596

 

Chapter 38: Exceptions 599

38.1: Functions Presented in This Chapter 599

38.2: Completion Codes 600

38.3: Setting errorCode 603

38.4: Managing the Return Options Dictionary 604

38.5: Adding to the Stack Trace in errorInfo 605

38.6: Tcl_Panic 608

 

Chapter 39: String Utilities 611

39.1: Functions Presented in This Chapter 611

39.2: Dynamic Strings 617

39.3: String Matching 621

39.4: Regular Expression Matching 622

39.5: Working with Character Encodings 624

39.6: Handling Unicode and UTF-8 Strings 625

39.7: Command Completeness 627

 

Chapter 40: Hash Tables 629

40.1: Functions Presented in This Chapter 630

40.2: Keys and Values 631

40.3: Creating and Deleting Hash Tables 632

40.4: Creating Entries 633

40.5: Finding Existing Entries 635

40.6: Searching 636

40.7: Deleting Entries 637

40.8: Statistics 638

 

Chapter 41: List and Dictionary Objects 639

41.1: Functions Presented in This Chapter 639

41.2: Lists 642

41.3: Dictionaries 644

 

Chapter 42: Channels 649

42.1: Functions Presented in This Chapter 649

42.2: Channel Operations 656

42.3: Registering Channels 658

42.4: Standard Channels 660

42.5: Creating a New Channel Type 661

 

Chapter 43: Handling Events 671

43.1: Functions Presented in This Chapter 671

43.2: Channel Events 672

43.3: Timer Events 676

43.4: Idle Callbacks 677

43.5: Invoking the Event Dispatcher 678

 

Chapter 44: File System Interaction 681

44.1: Tcl File System Functions 681

44.2: Virtual File Systems 683

 

Chapter 45: Operating System Utilities 685

45.1: Functions Presented in This Chapter 685

45.2: Processes 687

45.3: Reaping Child Processes 689

45.4: Asynchronous Events 690

45.5: Signal Names 693

45.6: Exiting and Cleanup 693

45.7: Miscellaneous 694

 

Chapter 46: Threads 695

46.1: Functions Presented in This Chapter 695

46.2: Thread Safety 697

46.3: Building Threaded Tcl 697

46.4: Creating Threads 697

46.5: Terminating Threads 698

46.6: Mutexes 698

46.7: Condition Variables 699

46.8: Miscellaneous 701

 

Chapter 47: Building Tcl and Extensions 703

47.1: Building Tcl and Tk 703

47.2: The Tcl Extension Architecture (TEA) 707

47.3: Building Embedded Tcl 714

 

Appendixes 715

 

Appendix A: Installing Tcl and Tk 717

A.1: Versions 717

A.2: Bundled Tcl Distributions 718

A.3: ActiveTcl 718

A.4: Tclkits 719

A.5: Compiling Tcl/Tk from Source Distributions 719

 

Appendix B: Extensions and Applications 721

B.1: Obtaining and Installing Extensions 721

B.2: TkCon Extended Console 724

B.3: The Standard Tcl Library, Tcllib 725

B.4: Additional Image Formats with Img 725

B.5: Sound Support with Snack 725

B.6: Object-Oriented Tcl 726

B.7: Multithreaded Tcl Scripting 727

B.8: XML Programming 727

B.9: Database Programming 728

B.10: Integrating Tcl and Java 728

B.11: SWIG 729

B.12: Expect 729

B.13: Extended Tcl 730

 

Appendix C: Tcl Resources 731

C.1: Online Resources 731

C.2: Books 732

 

Appendix D: Tcl Source Distribution License 735

 

Index 737

Purchase Info

ISBN-10: 0-13-265206-4

ISBN-13: 978-0-13-265206-3

Format: eBook (Watermarked)?

This eBook includes the following formats, accessible from your Account page after purchase:

ePubEPUBThe open industry format known for its reflowable content and usability on supported mobile devices.

MOBIMOBIThe eBook format compatible with the Amazon Kindle and Amazon Kindle applications.

Adobe ReaderPDFThe popular standard, used most often with the free Adobe® Reader® software.

This eBook requires no passwords or activation to read. We customize your eBook by discretely watermarking it with your name, making it uniquely yours.

Includes EPUB, MOBI, and PDF

$47.99

Add to Cart