Book Image

Python 2.6 Graphics Cookbook

By : Michael J Ohlson
Book Image

Python 2.6 Graphics Cookbook

By: Michael J Ohlson

Overview of this book

Python is a great object-oriented and interactive programming language that lets you develop graphics, both static and animated, using built-in vector graphics functions that are provided with Python. 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. If you choose to work through all the recipes from the beginning, you will learn to install Python and create basic programs for making lines and shapes using the built-in Tkinter module. The confusing topic of color manipulation is explored in detail using existing Python tools as well as some new tools in the recipes. Next you will learn to manipulate font size, color, and placement of text as placing text exactly where you want on a screen can be tricky because font height, inter-character spacing, and text window dimensions all interfere with each other. Then you will learn how to animate graphics, for example having more than one independent graphic object co-exist and interact using various Python methods. You will also learn how you can work with raster images, such as converting their formats using the Python Imaging Library. Next you will learn how you can combine vector images with raster images so that you can animate the raster images with ease. You will also walk through a set of recipes with the help of which you can handle and manipulate blocks of raw data that may be hundreds of megabytes in size using datastreams, files, and hard drives. You will also learn how you can use Inkscape to dismantle existing images and use parts of them for your own graphics and Python programs. At the end of the book you will learn how you can create GUIs for different purposes.
Table of Contents (18 chapters)
Python 2.6 Graphics Cookbook
Credits
About the Author
About the Reviewers
Preface
Index

Ensuring that the Python modules are present


Here is a slightly longer version of the previous program. However, the following modules are commanded to "report for duty" inside our program even though they are not actually used at this time: Tkinter, math, random, time, tkFont.

We need the assurance that all the Python modules we will be using later are present and accessible to Python, and therefore, to our code. Each module is a self-contained library of code functions and objects that are called frequently by the commands in your programs.

How to do it...

  1. In a text editor type the lines given in the following code.

  2. Save this as a file named simple_2.py, inside the directory called constr as we did previously.

  3. As before, open up an X terminal or a DOS window, if you are using MS Windows.

  4. Change directory into constr - where simple_1.py is located.

  5. Type python simple_2.py and our program should execute. The result should look like the following screenshot:

    This proves that your Python interpreter can access the necessary library functions it will need.

    """
    Program name: simplest_2.py
    Objective: Send more than one line of text to the screen.
    Keywords: text, simple
    ======================================
    Author:          Mike Ohlson de Fine
    """
    import Tkinter
    import math
    import random
    import time
    import tkFont
    print "======================================="
    print "A simple, apparently useless program like this does useful things:"
    print "Most importantly it checks whether your Python interpreter and "
    print "the accompanying operating system environment works"
    print " - including hardware and software"
    print "======================================"
    print " No matter how simple it is always a thrill"
    print " to get your first program to run"

How it works...

The print command is an instruction to write or print any text between quotation marks like "show these words" onto the monitor screen attached to your computer. It will also print the values of any named variables or expressions typed after print.

For example: print "dog's name:", dog_name. Where dog_name is the name of a variable used to store some data.

The print command is very useful when you are debugging a complicated sequence of code because even if the execution fails to complete because of errors, any print commands encountered before the error is reached will be respected. So by thoughtful placing of various print statements in your code, you are able to zero in on what is causing your program to crash.

There's more...

When you are writing a piece of Python code for the first time, you are often a bit unsure if your understanding of the logic is completely correct. So we would like to watch the progress of instruction execution in an exploratory way. It is a great help to be able to see that at least part of the code works. A major strength of Python is the way it takes our instructions one at a time and executes them progressively. It will only stop when the end is reached or a when programming flaw halts progress. If we have a twenty line program and only the first five lines are bug-free and the rest are unexecutable garbage, the Python interpreter will at least execute the first five. This is where the print command is a really potent little tool.

This is how you use print and the Python interpreter. When we are having trouble with our code and it just won't work and we are battling to figure out why, we can just insert print statements at various chosen points in our program. This way you can get some intermediate values of variables as your own private status reports. When we want to switch off our print watchdogs we simply type a hash (#) symbol in front, thus transforming them into passive comments. Later on, if you change your mind and want the prints to be active again you just remove the leading hash symbols.