资源说明:Preface
1
Chapter 1: Start your Engines
5
Introduction
5
Running a shortest Python program
6
Ensuring that the Python modules are present
7
A basic Tkinter program
9
Make a compiled executable under Windows and Linux
11
Chapter 2: Drawing Fundamental Shapes
15
Introduction
16
A straight line and the coordinate system
17
Draw a dashed line
18
Lines of varying styles with arrows and endcaps
20
A two segment line with a sharp bend
22
A line with a curved bend
23
Drawing intricate shapes – the curly vine
24
Draw a rectangle
27
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
Simple text
43
ii
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 field
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
113
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
iii
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
139
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 file 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-line editor
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
iv
Table of Contents
Grid Geometry Manager and button arrays
213
Drop-down menus to select from a list
215
Listbox variable selection
216
Text in a window
218
Chapter 11: GUI Construction: Part 2
219
Introduction
219
The Grid Layout Geometry Manager
220
The Pack Geometry Manager
222
Radiobuttons to select one from many
223
Checkbuttons (Tickboxes) to select some of many
224
Key-stroke event handling
226
Scrollbar
227
Custom DIY controller widgets
228
Organizing widgets inside frames
232
Appendix: Quick tips for running Python programs in
Microsoft Windows
235
Running Python programs in Microsoft Windows
235
Where will we find the windows installer?
235
Do we have to use Python version 2.7?
236
Why do we get "python is not recognized…"?
236
Index
239
Preface
Python 2.6 Graphics Cookbook is a collection of straightforward recipes and illustrative
screenshots for creating and animating graphic objects using the Python language. This
book makes the process of developing graphics interesting and entertaining by working in a
graphic workspace, without the burden of mastering complicated language definitions and
opaque examples.
What this book covers
Chapter 1, Start your Engines: This chapter explains how to acquire and install the Python
interpreter, for MS Windows or Linux as well as how to verify that Python is correctly installed.
This chapter explains how to create complete working programs that can be run on client
computers that do not have Python installed.
Chapter 2, Drawing Fundamental Shapes: This shows how to create all the fundamental
graphic elements including lines, circles, ovals, rectangles, polygons, and complex curves.
Simple examples are provided to demonstrate how to draw the elementary shapes. The
examples also provide a ready for reference for later use.
Chapter 3, Handling Text: This chapter demonstrates how to control font size, color, and
position using any of the font typefaces installed on the specific operating system being used.
A simple means of discovering and demonstrating all available fonts on the operating system
is shown.
Chapter 4, Animation Principles: This chapter starts with examples of simple sequences of a
circle in different positions and systematically progresses to smoothly-moving animations of
elastic balls bouncing inside a gravity field.
Chapter 5, The Magic of Color: This chapter begins with the assembling of color palettes
using color names recognizable to Python. The way colors are constructed using numbers
to mix controlled amounts of red, green, and blue is explained. Tools for matching colors
to any sample are constructed. This chapter demonstrates how to vary shadings of one
color into another.
Download from Wow! eBook
Preface
Chapter 6, Working with Pictures: This chapter reveals how to acquire and use the Python
Imaging Library to manipulate photo images. It also shows methods of image format
conversion, re-sizing, rotating, color transforming, and complex filtering.
Chapter 7, Combining Vector and Raster Images: This chapter demonstrates the ways
of combining animated vector graphics with photographic images to produce
complex animations.
Chapter 8, Data in and Data Out: This chapter starts with basic storing and retrieving of files
to a hard drive and progresses to the construction of programs that are tools for creating,
storing, and retrieving free-form shapes drawn using a mouse.
Chapter 9, Exchanging Inkscape SVG Drawings with Tkinter Shapes: This chapter shows in
detail how to use the Inkscape drawing tool to convert shapes traced from a photographic
image into a sequence of points which reproduce the shape in Python. Once a line is
expressed as a Python sequence, it can be transformed numerically in many ways.
Chapter 10, GUI Construction: Part 1: This chapter provides basic examples of how to create
buttons, data entry boxes, drop-down menus, list-boxes, and text labels. It also covers how
to customize button appearance.
Chapter 11, GUI Construction: Part 2: Here the Grid Layout Manager and the Pack Layout
Manager are explained and demonstrated. Examples of radio buttons, check buttons,
scrollbars, frames, and keystroke event coding are given. It also shows how to construct
widgets using graphic elements on a canvas.
Appendix, Quick tips for running Python programs in Microsoft Windows: This gives
explanations of how to overcome some of the difficulties a new python programmer
might encounter when trying to use Python in Windows.
What you need for this book
To run the code in this book, the reader will need a Linux operating system or Microsoft
Windows, and some way of downloading Python, the Python Imaging Library, and Inkscape
from the internet. All these applications are free and open source. The code has been
developed on Linux Ubuntu version 9.04, Microsoft Windows XP, and Windows 7.
Who this book is for
This book is for Python programmers wanting simple, clear examples of graphic programming
using Python. The examples are aimed at anyone wanting to use graphic elements and
images inside Python programs with the minimum of complexity. The intended reader
ranges from scholars and teachers to engineers and technicians
本源码包内暂不包含可直接显示的源代码文件,请下载源码包。