Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống
1
/ 455 trang
THÔNG TIN TÀI LIỆU
Thông tin cơ bản
Định dạng
Số trang
455
Dung lượng
1,92 MB
Nội dung
PracticalProgramminginTclandTk Brent Welch DRAFT, January 13, 1995 Updated for Tcl 7.4 andTk 4.0 THIS IS NOT THE PUBLISHED TEXT THE INDEX IS INCOMPLETE SOME SECTIONS ARE MISSING THE MANUSCIRPT HAS NOT BEEN EDITED GET THE REAL BOOK: ISBN 0-13-182007-9 An enhanced version of this text has been published by Prentice Hall: ISBN 0-13-182007-9 Send comments via email to welch@acm.org with the word “book” in the subject. http://www.sunlabs.com/~bwelch/book/index.html The book is under copyright. Print for personal use only. This on-line DRAFT is available curtesty the kind folks at PH. Table of Contents Created: May 9, 1994 —bookTOC.doc—Copyright Prentice Hall—DRAFT: 1/13/95 1. Tcl Fundamentals 1 Getting Started 1 Tcl Commands 2 Hello World 3 Variables 3 Command Substitution 4 Math Expressions 4 Backslash Substitution 6 Double Quotes 7 Procedures 7 A While Loop Example 8 Grouping And Command Substitution 10 More About Variable Substitution 11 Substitution And Grouping Summary 11 Fine Points 12 Comments 13 Command Line Arguments 13 Reference 14 Backslash Sequences 14 Arithmetic Operators 14 Built-in Math Functions 15 Core Tcl Commands 15 Predefined Variables 18 2. Strings and Pattern Matching 19 The string Command 19 Strings And Expresssions 20 The append Command 21 The format Command 21 The scan Command 23 String Matching 24 Regular Expressions 25 The regexp Command 26 The regsub Command 28 Created: May 9, 1994 —bookTOC.doc—Copyright Prentice Hall—DRAFT: 1/13/95 ii 3. Tcl Data Structures 29 More About Variables 29 The unset command 30 Using info to find out about variables 30 Tcl Lists 31 Constructing Lists: list, lappend, and concat 32 Getting List Elements: llength, lindex, and lrange 33 Modifying Lists: linsert and lreplace 34 Searching Lists: lsearch 34 Sorting Lists: lsort 35 The split And join Commands 35 Arrays 36 The array Command 37 Environment Variables 38 Tracing Variable Values 39 4. Control Flow Commands 41 If Then Else 42 Switch 43 Foreach 44 While 45 For 46 Break And Continue 46 Catch 46 Error 48 Return 49 5. Procedures and Scope 51 The proc Command 51 Changing command names with rename 52 Scope 53 The global Command 53 Use Arrays for Global State 55 Call By Name Using upvar 55 Passing arrays by name 56 The uplevel Command 57 6. Eval 59 Eval And List 59 iii Created: May 9, 1994 —bookTOC.doc—Copyright Prentice Hall—DRAFT: 1/13/95 Eval And Concat 61 Double-quotes and eval 62 Commands That Concat Their Arguments 62 The subst Command 63 7. Working with UNIX 65 Running Unix Programs With exec 65 auto_noexec 67 Looking At The File System 67 Input/Output 70 Opening Files For I/O 70 Reading And Writing 72 The puts and gets commands 72 The read command 73 Random access I/O 73 Closing I/O streams 74 The Current Directory - cd And pwd 74 Matching File Names With glob 74 The exit And pid commands 75 8. Reflection and Debugging 77 The info Command 77 Variables 78 Procedures 79 The call stack 79 Command evaluation 80 Scripts and the library 80 Version numbers 80 Interactive Command History 81 History syntax 82 A comparision to /bin/csh history syntax 82 Debugging 83 Don Libes’ debugger 84 Breakpoints by pattern matching 85 Deleting break points 86 The tkerror Command 87 The tkinspect Program 87 Performance Tuning 87 Created: May 9, 1994 —bookTOC.doc—Copyright Prentice Hall—DRAFT: 1/13/95 iv 9. Script Libraries 89 The unknown Command 89 The tclIndex File 90 Using A Library: auto_path 90 Disabling the library facility: auto_noload 91 How Auto Loading Works 91 Interactive Conveniences 92 Auto Execute 92 History 92 Abbreviations 92 Tcl Shell Library Environment 93 Coding Style 93 A module prefix for procedure names 94 A global array for state variables 94 10. Tk Fundamentals 95 Hello World InTk 96 Naming Tk Widgets 98 Configuring Tk Widgets 98 About The Tk Man Pages 99 Summary Of The Tk Commands 99 11. Tk by Example 103 ExecLog 103 Window title 105 A frame for buttons, etc. 105 Command buttons 106 A label and an entry 106 Key bindings and focus 106 A resizable text and scrollbar 107 The Run proc 107 The Log procedure 108 The Stop procedure 108 The Example Browser 109 More about resizing windows 110 Managing global state 111 Searching through files 111 Cascaded menus 112 The Browse proc 112 v Created: May 9, 1994 —bookTOC.doc—Copyright Prentice Hall—DRAFT: 1/13/95 A Tcl Shell 113 Naming issues 114 Text marks and bindings 114 12. The Pack Geometry Manager 115 Packing towards a side 116 Shrinking frames and pack propagate 116 Horizontal And Vertical Stacking 117 The Cavity Model 118 Packing Space and Display Space 119 The -fill option 119 Internal padding with -ipadx and -ipady 120 External padding with -padx and -pady 123 Expand And Resizing 123 Anchoring 125 Packing Order 126 pack slaves and pack info 127 Pack the scrollbar first 127 Choosing The Parent For Packing 128 Unpacking a Widget 129 Packer Summary 129 The pack Command 130 The Place Geometry Manager 130 The place Command 131 Window Stacking Order 132 13. Binding Commands to X Events 133 The bind Command 133 All, Class, And Widget Bindings 134 The bindtags command 135 break and continue in bindings 135 A note about bindings in earlier versions of Tk 135 Event Syntax 136 Key Events 137 Button Events 138 Other Events 138 Modifiers 139 Events inTk 3.6 and earlier 141 Event Sequences 141 Created: May 9, 1994 —bookTOC.doc—Copyright Prentice Hall—DRAFT: 1/13/95 vi Event Keywords 142 14. Buttons and Menus 145 Button Commands and Scope Issues 145 Buttons Associated with Tcl Variables 149 Button Attributes 151 Button Operations 153 Menus and Menubuttons 153 Manipulating Menus and Menu Entries 155 A Menu by Name Package 156 Popup Menus and Option Menus 159 Keyboard Traversal 159 Menu Attributes 160 15. Simple Tk Widgets 163 Frames and Top-Level Windows 163 Attributes for frames and toplevels 164 The label Widget 165 Label attributes 166 Label width and wrapLength 166 The message Widget 167 Message Attributes 168 Arranging Labels and Messages 169 The scale Widget 169 Scale attributes 170 Programming scales 171 The scrollbar Widget 172 Scrollbar attributes 174 Programming scrollbars 175 The Tk 3.6 protocol 175 The bell Command 176 16. Entry and Listbox Widgets 177 The entry Widget 177 entry attributes 180 Programming entry widgets 181 The listbox Widget 183 Programming listboxes 185 Listbox Bindings 189 Browse select mode 190 vii Created: May 9, 1994 —bookTOC.doc—Copyright Prentice Hall—DRAFT: 1/13/95 Single select mode 190 Extended select mode 191 Multiple select mode 192 Scroll bindings 192 listbox attributes 193 Geometry gridding 194 17. Focus, Grabs, and Dialogs 195 Input Focus 195 The focus command 196 Focus follows mouse 196 Click to type 197 Hybrid models 197 Grabbing the Focus 197 Dialogs 198 The tkwait Command 198 Prompter dialog 198 Destroying widgets 200 Focusing on buttons 200 Animation with the update command 200 File Selection Dialog 201 Creating the dialog 201 Listing the directory 204 Accepting a name 205 Easy stuff 207 File name completion 207 18. The text Widget 211 Text widget taxonomy 211 Text Indices 212 Text Marks 213 Text Tags 214 Tag attributes 215 Mixing attributes from different tags 216 Line Spacing and Justification 217 The Selection 219 Tag Bindings 219 Embedded Widgets 220 Text Bindings 222 Created: May 9, 1994 —bookTOC.doc—Copyright Prentice Hall—DRAFT: 1/13/95 viii Text Operations 223 Text Attributes 225 19. The canvas Widget 227 Hello, World! 227 The Double-Slider Example 229 Canvas Coordinates 233 Arcs 233 Bitmap Items 235 Images 236 Line Items 236 Oval Items 238 Polygon Items 239 Rectangle Items 240 Text Items 241 Window Items 244 Canvas Operations 246 Generating Postscript 248 Canvas Attributes 250 Hints 251 Large coordinate spaces 251 Scaling and Rotation 251 X Resources 252 Objects with many points 252 20. Selections and the Clipboard 253 The selection Command 254 The clipboard Command 255 Interoperation with OpenLook 255 Selection Handlers 255 A canvas selection handler 256 21. Callbacks and Handlers 259 The after Command 259 The fileevent Command 260 The send Command 261 The sender script 262 Using sender 264 Hooking the browser to a shell 266 [...]... Command pipelines 351 Tracing the actions of the Tcl interpreter 351 Evalutating Tcl commands 351 Manipulating Tcl variables 352 Evalutating expressions 352 Converting numbers 352 Hash tables 352 Dynamic Strings 353 Regular expressions and string matching 353 Tilde Substitution 353 Working with signals 353 30 C Programmingand Tk 355 Tk_ Main and. .. Defining Preferences 333 The Preferences User Interface 335 Managing The Preferences File 338 Tracing Changes To Preference Variables 340 29 C ProgrammingandTcl .341 Using the Tcl C Library 342 Application Structure 342 Tcl_ Main and Tcl_ AppInit 343 The standard main inTcl 7.3 344 A C Command Procedure 345 Managing The Result’s Storage 346 Invoking... settings to a file .338 Read settings from the preferences file 339 Tracing a Tcl variable in a preference item 340 290C Programmingand Tcl 3 1 4 29.1 29.2 29.3 29.4 A canonical Tcl main program and Tcl_ AppInit 343 The RandomCmd C command procedure 345 Calling C command procedure directly 348 A Makefile for a simple Tcl C program 349 300C Programming and. .. package and an associated user interface The preference package links a Tcl variable used in your application to an X resource specification Chapter 29 provides a short introduction to using Tcl at the C programming level It gets you started with integrating Tcl into an existing application, and it provides a survey the the facilities in the Tcl C library Chapter 30 introduces C programming with the Tk toolkit... atoms and window ids 312 Information about colormaps and visual classes 312 260A User Interface to bind 3 5 1 270Using X Resources 3 3 2 280Managing User Preferences 3 1 3 290C Programmingand Tcl 3 1 4 300C Programmingand Tk 3 5 5 310Writing a Tk Widget in C 3 1 7 31-1 Configuration flags and corresponding... frustrated In addition, working at Xerox PARC, with many experts in languages and systems, I was compelled to understand both the strengths and weaknesses of TclandTk While many of my colleagues adopted TclandTk for their projects, they were also just as quick to point out its flaws In response, I have built up a set of programming techniques that exploit the power of TclandTk while avoiding troublesome... 346 Invoking Scripts From C 347 Bypassing Tcl_ Eval 347 Putting A Tcl Program Together 349 An Overview of the Tcl C library 349 Application initialization 350 Creating and deleting interpreters 350 Creating and deleteing commands 350 Managing the result string 350 Created: May 9, 1994 —bookTOC.doc—Copyright Prentice Hall—DRAFT: 1/13/95 xi Lists and command parsing ... Tk_ Main and Tcl_ AppInit 355 A Custom Main Program 357 A Custom Event Loop 360 An Overview of the Tk C library 361 Parsing command line arguments 361 The standard application setup 362 Creating windows 362 Application name for send 362 Configuring windows 362 Window coordinates 362 Window stacking order 363 Window information 363 Configuring widget... functionality, and a Tcl interface to the library Examples include socket access for network programming, database access, telephone control, MIDI controller access, and expect, which adds Tcl commands to control interactive programs The most notable extension is Tk, a toolkit for X windows Tk defines T cl commands that let you create and manipulate user interface widgets The scriptbased approach to UI programming. .. loop using read and split 73 Finding a file by name 74 80Reflection and Debugging 77 8.1 8.2 8.3 8.4 8.5 Printing a procedure definition 79 Getting a trace of the Tcl call stack 80 Interactive history usage 82 Implementing special history syntax 83 A Debug procedure 83 90Script Libraries 89 9.1 9.2 Maintaining a tclIndex . place Command 131 Window Stacking Order 132 13. Binding Commands to X Events 133 The bind Command 133 All, Class, And Widget Bindings 134 The bindtags command 135 break and continue in bindings 135 A. 338 Tracing Changes To Preference Variables 340 29. C Programming and Tcl 341 Using the Tcl C Library 342 Application Structure 342 Tcl_ Main and Tcl_ AppInit 343 The standard main in Tcl 7.3 344 A. 352 Dynamic Strings 353 Regular expressions and string matching 353 Tilde Substitution 353 Working with signals 353 30. C Programming and Tk 355 Tk_ Main and Tcl_ AppInit 355 A Custom Main Program