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

A basic Tkinter program


Here we attempt to execute a Tkinter command inside the Python program. The Tkinter instruction will create a canvas and then draw a straight line on it.

How to do it...

  1. In a text editor, type the code given below.

  2. Save this as a file named simple_line_1.py, inside the directory called constr again.

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

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

  5. Type python simple_line_1.py and your program should execute. The command terminal result should look like the following screenshot:

  6. The Tkinter canvas output should look like the following screenshot:

  7. This proves that your Python interpreter works, your editor works, and the Tkinter module works. This is not a trivial achievement – you are definitely ready for great things. Well done.

    #>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
    from Tkinter import *
    root = Tk()
    root.title('Very simple Tkinter line')                             
    canvas_1 = Canvas(root, width=300, height=200, background="#ffffff")
    canvas_1.grid(row=0, column=0)
    
    canvas_1.create_line(10,20 ,   50,70)
    root.mainloop()
    #>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

How it works...

To draw a line, we only need to give the start point and the end point.

The start point is the first pair of numbers in canvas_1.create_line(10,20 , 50,70).In another way, the start is given by the coordinates x_start=10 and y_start=20. The end point of the line is specified by the second pair of numbers x_end=50 and y_end=70. The units of measurement are pixels. A pixel is the smallest dot that can be displayed on our screen.

For all other properties like line thickness or color, default values of the create_line() method are used.

However, should you want to change color or thickness, you just do it by specifying the settings.