Python 2. 6 Graphics Cookbook Copyright C 2010 Packt Publishing All rights reserved. No part of this book may be reproduced stored in a retrieval system or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews Every effort has been made in the preparation of this book to ensure the accuracy of the iformation presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the author, nor Packt Publishing, and its dealers and distributors will be held liable for any damages caused or alleged to be caused directly or indirectly by this book Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals However, Packt Publishing cannot guarantee the accuracy of this information First published: November, 2010 Production reference: 1181110 Published by Packt Publishing Ltd 32 Lincoln road Olton Birmingham, B27 6PA, UK SBN9781-849513845 www.packtpub.com Cover Image by Asher Wishkerman(a. wishkerman@ mpic. de) Credits Editorial Team Leader Mike ohlson de fine Mithun Sehgal Reviewers Project Team Leader Flavio barbosa Ashwin Shetty Michael driscoll Warren noronha Project Coordinator Michelle Quadros Acquisition Editor Proofreader Dilip venkates Mario cecere Development Editor Graphics Meeta rajani Nilesh R Technical editor Production Coordinator Gauri lyel Aparna bhagat Indexer Cover work Tejal Daruwale Aparna bhagat About the author Mike ohlson de Fine is a graduate Electrical Engineer specializing in industrial process measurement and control. He has a diploma in Electronics and Instrumentation from Technikon Witwatersrand, an Electrical Engineering degree from the University of Cape Town, and a Masters in Automatic Control from Rand Afrikaans University. He has worked for mining and mineral extraction companies for the last 30 years. His first encounter with computers was learning Fortran 4 using punched cards on an IBM 360 as an undergraduate. Since then he has experimented with Pascal, Forth, Intel 8080 Assembler, MS Basic, C, and C++, but was never satisfied with any of these Always restricted by corporate control of computing activities, he encountered Linux in 2006 and Python in 2007 and became free at last As a working engineer he needs tools that facilitate the understanding and solution of industrial process control problems using simulations and computer models of real processes. Linux and Python proved to be excellent tools for these challenges. When he retires he would like to be part of setting up a Free and open Source engineering virtual workshop for his countrymen and people in other poor countries to enable the bright youngsters of these countries to be intellectually free at last. His hobbies are writing computer simulations, paddling kayaks in wild water, and surf skiing in the sea At the top of the pyramid of people who have helped and encouraged me Genevieve, Candace, and Peter, who have bravely chosen a steep ano= to write this book is my wonderful wife Suzanne. Thank you Suzy, with a my heart. I want to dedicate this book also to three courageous people difficult path in their lives. I salute you Next who I would like to thank are the very professional and pleasant staff of PACKT Publishing. A special thanks to the restrained and long suffering reviewers mike driscoll, Warren noronha and flavio barbosa. Your criticism was invaluable About the reviewers Michael Driscoll has been programming Python since 2006 and has dabbled in other languages since the late nineties. He graduated from university with a bachelors in Science degree, majoring in Management Information Systems. Michael enjoys programming for fun and profit. his hobbies include biblical apologetics blogging aboutPythonathttp://www.blog.pythonlibrary.org/andlearningphotography Michael currently works for local government where he programs with Python as much as possible. Michael was also a technical editor for Python 3: object Oriented Programming by Dusty Phillips I would like to thank my brothers for their support and the fun times they share with me and my dad for his indirect support. Most of all, I want to thank Jesus for saving me from myself Warren Noronha is an entrepreneur and geek computers have been part of Warrens life since he was four years old. He began his career as a system administrator, but ended up doing everything from security and design to product development. he enjoys managing people as much as he does managing code or machines. Having worked with small startups as well as Fortune 500 companies, Warren is also a staunch supporter of free software and free speech He has been a frequent speaker at various colleges and events, discussing subjects ranging from technology and media to launching a startup Warren loves working with new technologies; a trait which lead him to become one of the first users of GNU/Linux, Drupal, and Ruby on Rails, much before they grew exponentially and became mainstream technologies. He spends his time working on databases, distributed computing, social computing, and enjoys using internet and communication technology to bridge the digital divide Table of contents Preface Chapter 1: Start your Engines 5 Introduction Running a shortest Python program 6 Ensuring that the Python modules are present a basic Tkinter program Make a compiled executable under Windows and Linux 11 Chapter 2: Drawing Fundamental shapes Introduction 16 A straight line and the coordinate system Draw a dashed line 18 Lines of varying styles with arrows and endcaps 20 A two segment lIne wlth a sharp bend 22 A line with a curved bend 23 Drawing Intricate shapes- the curly vine 24 Draw a rectangle Draw overlapping rectangles 28 Draw concentric squares 30 A circle from an oval 32 A circle from an arc 34 Three arc ellipses 35 Polygons 36 A star polygon 37 Cloning and resizing stars 39 Chapter 3: Handling Text 43 Introduction 43 Sim ple text 43 Table of contents Text font type size, and color 45 Alignment of text -left and right justify 49 All the fonts available on your computer 54 Chapter 4: Animation Principles 57 Introduction 57 Static shifting of a ball 58 Time-controlled shifting of a ball 59 Complete animation using draw-move-pause-erase cycles 62 More than one moving object 63 a ball that bounces 65 BouncIng in a gravIty fleld 67 Precise collisions using floating point numbers 70 Trajectory tracing and ball-to-ball collisions 72 Rotating line 76 Trajectory tracing on multiple line rotations 78 A rose for you 82 Chapter 5: The Magic of color 85 Introduction 85 A limited palette of named colors 86 Nine ways of specifying color 90 A red beachball of varying hue 91 A red color wedge of graded hue 94 Newton's grand wheel of color mixing 96 The numerical color mixing matching palette 101 The animated graded color wheel 106 Tkinter's own color picker-mixer 110 Chapter 6: Working with Pictures Opening an image file and discovering its attributes 114 Open, view, and save an image in a different file format 117 Image format conversion for JPEG, PNG, TIFF, GIF, BMP 118 Image rotation in the plane of the image 120 Image size alteration 121 Correct proportion image resizing 123 Separating one color band in an image 124 Red, green, and blue color alteration in images 125 Slider controlled color manipulation 127 Combining images by blending 130 BlendIng Images by varying percentages 131 Make a composite image using a mask image 132 Table of contents offset (roll) image horizontally and vertically 134 Flip horizontally, vertically, and rotate 134 Filter effects: blur, sharpen contrast, and so on 135 Chapter 7: Combining Raster and vector Pictures 39 Simple animation of a GiF beach ball 140 The vector walking creature 141 Bird with shoes walking in the Karroo 145 Making GiF images with transparent backgrounds using GIMP 149 DIplomat walking at the palace 152 Spider in the forest 156 Moving band of Images 160 Continuous band of images 162 Endless background 164 Chapter 8: Data In and Data out 167 Introduction 167 Creation of a new fille on a hard drive 168 WrIting data to a newly-created file 169 Writing data to multiple files 169 Adding data to existing files 170 Saving a Tkinter-drawing shape to disk 171 Retrieving Python data from disk storage 172 Simple mouse input 173 Storing and retrieving a mouse-drawn shape 174 A mouse-ine edit 177 All possible mouse actions 181 Chapter 9: Exchanging Inkscape SVG Drawings with Tkinter Shapes 185 Introduction 185 The structure of an sVG drawing 186 Tracing the shape of an image in Inkscape 189 Converting an svG path into a Tkinter Line 194 Chapter 10: GUI Construction: Part 1 199 Introduction 199 Widget configuration -a label 200 Button focus 201 The simplest push button with validation 203 a data entry box 204 Colored button causing a message pop-up 207 Complex interaction between buttons 208 Images on buttons and button packing 211 II
Python 2.6 Text Processing Beginner’s Guide is the easiest way to learn how to manipulate text with Python. Packed with examples, it will teach you text processing techniques and give you the skills ...