play games on your computer, think for a moment about
the graphics you see on the screen. Two-dimensional
(2D) graphics are flat—the characters generally move
only up and down or left and right— as in many Nintendo DS, PlayStation Portable (PSP), and mobile phone games. In pseudo-three-dimensional (3D) games—ones that are almost 3D—images look a little more real, but the characters generally move only in relation to a flat plane (this is also known as isometric graphics).
And, finally, we have 3D games, where the pictures drawn on the screen attempt to mimic reality. Whether the games use 2D, pseudo-3D, or 3D graphics, all have one thing in common: the need to draw on the computer screen very quickly.
If you’ve never tried to create your own animation, try this simple project:
1. Get a blank pad of paper, and in the bottom corner of the first page, draw something (perhaps a stick figure).
2. On the corner of the next page, draw the same stick figure, but move its leg slightly.
3. On the next page, draw the stick figure again, with the leg moved a little more.
4. Gradually go through each page, drawing a modified stick fig- ure on the bottom corner.
When you’re finished, flip quickly through the pages, and you should see your stick figure moving. This is the basic method used with all animation, whether it’s cartoons on TV or games on your console or computer. An image is drawn, and then drawn again with a slight change to create the illusion of movement. To make an image look like it is moving, you need to display each frame, or piece of the animation, very quickly.
Python offers different ways to create graphics. In addition to
the turtle module, you can use external modules (which need to
be installed separately), as well as the tkinter module, which you
should already have in your standard Python installation. tkinter
can be used to create full applications, like a simple word proces- sor, as well as for simple drawing. In this chapter, we’ll explore how to use tkinter to create graphics.
Using tkinter for Better Graphics 165
Creating a Clickable Button
For our first example, we’ll use tkinter to create a basic application
with a button. Enter this code:
>>> from tkinter import *
>>> tk = Tk()
>>> btn = Button(tk, text="click me")
>>> btn.pack()
On the first line, we import the contents of the tkinter module.
Using from module-name import * allows
us to use the contents of a module without using its name. In contrast, when using import turtle in previous
examples, we needed to include the module name to access its contents:
import turtle t = turtle.Pen()
When we use import *, we don’t need to call turtle.Pen, as we
did in Chapters 4 and 11. This isn’t so useful with the turtle mod-
ule, but it is when you are using modules with a lot of classes and functions, because it reduces the amount you need to type.
from turtle import *
t = Pen()
On the next line in our button example, we create a variable containing an object of the class Tk with tk = Tk(), just like we
create a Pen object for the turtle. The tk object creates a basic win-
dow to which we can then add other things, such as buttons, input boxes, or a canvas to draw on. This is the main class provided by
the tkinter module—without creating an object of the Tk class, you
won’t be able to do any graphics or animations.
On the third line, we create a button, with btn = Button and
pass the tk variable as the first parameter, and "click me" as
the text that the button will display, with (tk, text="click me").
Although we’ve added this button to the window, it won’t be dis- played until you enter the line btn.pack(), which tells the button to
appear. It also lines everything up correctly on the screen, if there are other buttons or objects to display. The result should be some- thing like this:
The click me button doesn’t do much. You can click it all day, but nothing will happen until we change the code just a bit. (Be sure to close the window you created earlier!)
First, we create a function to print some text:
>>> def hello():
print('hello there')
Then we modify our example to use this new function:
>>> from tkinter import *
>>> tk = Tk()
>>> btn = Button(tk, text="click me", command=hello)
>>> btn.pack()
Notice that we’ve made only a slight change to the previous version of this code: We’ve added the parameter command, which tells
Python to use the hello function when the button is clicked.
Now when you click the button, you will see “hello there” writ- ten to the shell. This will appear each time the button is clicked. In the following example, I’ve clicked the button five times.
Using tkinter for Better Graphics 167
This is the first time we’ve used named parameters in any of our code examples, so let’s talk about them a bit before continuing with our drawing.