Learning Perl Tk.Pdf
Total Page:16
File Type:pdf, Size:1020Kb
Learning Perl/Tk Nancy Walsh Beijing • Cambridge • Farnham • Köln • Paris • Sebastopol • Taipei • Tokyo Learning Perl/Tk by Nancy Walsh Copyright (c) 1999 O'Reilly & Associates, Inc. All rights reserved. Printed in the United States of America. Published by O'Reilly & Associates, Inc., 101 Morris Street, Sebastopol, CA 95472. Editor:Linda Mui Editorial and Production Services: TIPS-Technical Publishing, Inc. Production Editor: Ellie Fountain Maden Printing History: January 1999: First Edition. March 1999: Minor corrections. Nutshell Handbook, the Nutshell Handbook logo, and the O'Reilly logo are registered trademarks of O'Reilly & Associates. The use of an emu image in association with Perl/ Tk is a trademark of O'Reilly & Associates, Inc. Permission may be granted for non- commercial use; please inquire by sending mail to [email protected]. Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in this book, and O'Reilly & Associates, Inc. was aware of a trademark claim, the designations have been printed in caps or initial caps. While every precaution has been taken in the preparation of this book, the publisher assumes no responsibility for errors or omissions, or for damages resulting from the use of the information contained herein. This book is printed on acid-free paper with 85% recycled content, 15% post-consumer waste. O'Reilly & Associates is committed to using paper with the highest recycled content available consistent with high quality. ISBN: 1-56592-314-6:[5/99] Table of Contents Preface xi 1. Introduction to Perl/Tk 1 A Bit of History About Perl (and Tk) 1 Perl/Tk for Both Unix and Windows 95/NT 2 Why Use a Graphical Interface? 2 Why Use Perl/Tk? 3 Installing the Tk Module 5 Creating Widgets 6 Coding Style 8 Displaying a Widget 9 The Anatomy of an Event Loop 9 Hello World Example 10 Using exit Versus Using destroy 12 Naming Conventions for Widget Types 12 Using print for Diagnostic/Debugging Purposes 13 Designing Your Windows (A Short Lecture) 14 2. Geometry Management 15 Pack 16 Grid 34 Place 47 Geometry Management Summary 56 3. The Basic Button 57 The Button Widget 57 Some Fun Things to Try 80 4. Checkbuttons and Radiobuttons 81 The Checkbutton Widget 81 The Radiobutton Widget 93 Fun Things to Try 101 5. Label and Entry Widgets 102 The Label Widget 102 The Entry Widget 108 Fun Things to Try 123 6. Scrollbars 124 Defining Scrollbar Parts 124 The Scrolled Method 126 The Scrollbar Widget 128 Examples 137 Fun Things to Try 140 7. The Listbox Widget 141 Creating and Filling a Listbox 141 Listbox Options 142 Selection Modes 144 Colors 145 Listbox Style 146 Configuring a Listbox 147 Inserting Items 147 Deleting Items 148 Retrieving Elements 148 Selection Methods 149 Moving to a Specific Index 150 Translating Indexes 150 Counting Items 150 Active Versus Selected 150 Bounding Box 151 Finding an Index by Y Coordinate 151 Scrolling Methods 151 Listbox Example 152 Fun Things to Try 153 8. The Text Widget 154 Creating and Using a Text Widget 154 Text Widget Options 155 A Short Break for a Simple Example 160 Text Indexes 161 Text Tags 164 Inserting Text 171 Deleting Text 172 Retrieving Text 172 Translating Index Values 172 Comparing Index Values 173 Showing an Index 173 Getting the Size of a Character 173 Getting Line Information 173 Searching the Contents of a Text Widget 174 Scrolling 175 Marks 175 Embedding Widgets 176 Internal Debug Flag 179 Fun Things to Try 180 9. The Canvas Widget 181 Creating a Canvas 181 Coordinate System 182 The Scrollable Region 183 Using Bind with a Canvas 184 Canvas Options 184 Creating Items in a Canvas 188 Configuring the Canvas Widget 198 Configuring Items in the Canvas Widget 199 Tags 199 Retrieving Bounding Box Coordinates 202 Translating Coordinates 202 Moving Items Around 202 Changing the Display List 203 Deleting Items 203 Deleting Tags 204 Determining Item Type 204 Set Keyboard Focus 204 Rendering the Canvas as PostScript 204 Scaling the Canvas 205 Scanning 206 A Drawing Program Example 206 Fun Things to Try 209 10. The Scale Widget 210 Creating a Scale 210 Assigning a Callback 213 Orientation 213 Minimum and Maximum Values 213 Displayed Versus Stored Value 214 Adding a Label 214 Displaying Value Increments 214 Changing the Size of the Scale 215 Options You'll Probably Never Need 215 Configuring a Scale 216 Getting the Value of a Scale 216 Setting the Value of a Scale 216 Determining Coordinates 216 Identifying Parts of a Scale 216 Fun Things to Try 217 11. Menus 218 Different Types of Menus 218 The Menubutton Widget 220 Complete Menubutton Examples 236 The Menu Widget 238 Optionmenu Widget 248 Fun Things to Try 250 12. Frames 251 Creating a Frame 251 Frame Style 253 Frames Aren't Interactive 255 Colormap Complications 255 Frame Methods 256 Fun Things to Try 256 13. Toplevel Widgets 257 Creating a Toplevel Widget 257 Toplevel Methods 260 Review 269 Fun Things to Try 269 14. Binding Events 270 The bind Method 270 Arguments Sent to the Callback 272 Defining Event Sequences 273 Event Information 278 Bailing Out of a Callback Created with bind 279 The bindtags Method 280 Ways to Use bind 280 15. Composite Widgets 281 Looking at an Example Sideways 282 Location of Files 283 Creating a Composite Widget Based on Frame 284 Toplevel-Based Composite Widgets 289 16. Methods for Any Widget 290 Building a Family Tree 290 Color-Related Methods 292 Option Databases 293 The Application's Name 295 Widget Existence 295 Is the Widget Mapped? 295 Converting Screen Distances 295 Size of Widget 296 Widget Position 297 Screen Information 298 Atom Methods 300 Ringing a Bell 300 Clipboard Methods 300 Selection Methods 301 Destroying a Widget 302 Focus Methods 302 Grab Methods 303 Interapplication Communication 304 Waiting for Events to Happen 304 Parsing Command-Line Options 306 Time Delays 306 A. Configuring Widgets with configure and cget 309 B. Operating System Differences 331 C. Fonts 334 Index 341 Preface Perl is a great language for file processing, connecting to databases, and many other tasks that are too tedious to do manually. For many years, however, Perl programs were limited to a command-line interface. The Tk interface changed all that. The Tk extension to Perl allows you to create graphical interfaces for your programs. Using the modules included with the distribution of Tk, you can create windows with buttons, lists, text, and other types of widgets to help your user navigate within your application. What You Should Already Know To get the most out of this book, you should already know the basics of Perl (specifically, Perl version 5). You should be familiar enough with Perl to be able to at least read some code and know what the code is doing. You don't have to be a Perl guru or Perl hacker to learn Perl/Tk, but it will help if you feel comfortable with the language. Here's the laundry list of things you should at least recognize: hashes, arrays, subroutines, and their anonymous versions, $_ and @_. Perl/Tk utilizes the object-oriented features available in Perl 5, so even if you don't completely understand them, you should be able to recognize them when you see them. The only other thing you'll need is your prior knowledge of other graphical user interfaces (GUIs) and what you did and did not like about them. This helps when deciding what features to include in your own applications. Take a look at the word processor you use on your PC, your web browser, or any program that has buttons and scrollbars and accepts both mouse and keyboard input. Those applications are pretty major ones; we'll start with much simpler examples and build up from there. We'll be covering each basic widget and all its associated options in detail. You'll learn how to make a window look the way you want it to look. You'll also learn how to make a window user-friendly and attractive. If you want to know more about Perl in general, you should read Learning Perl, Programming Perl, Advanced Perl Programming, and Perl Cookbook, which are also published by O'Reilly & Associates, Inc. There are also numerous FAQs and documents available on the Web. This book's focus is the Tk extension to Perl, which is a fairly specific portion of Perl. What's in This Book Chapter 1, Introduction to Perl/Tk The first chapter contains some interesting history about Perl and the Tk module. It starts you out with a simple Hello World program and gives a short introduction to event- driven programs. Chapter 2, Geometry Management Geometry management is probably the most important concept in using Perl/Tk. It determines how your widgets are to be drawn on the screen (or, in some cases, how not to be drawn on the screen). The three geometry managers-pack, grid, and place-are covered here. Most examples in the book use pack. Chapter 3, The Basic Button The button is the first widget we cover and there are lots of details here. There are also tons of code snippets and screen shots showing different ways to manipulate and mutilate the button widget. Many of the options discussed here are common among the other standard widgets. Chapter 4, Checkbuttons and Radiobuttons Checkbuttons and radiobuttons are similar to the standard button, but they look different and are usually programmed differently. Chapter 5, Label and Entry Widgets The label is the simplest widget of all.