Python Graphics

Just like Scratch and BASIC, it’s possible to incorporate graphics in your Python programs. These can simply be used to indicate a button that interacts with electronics on the breadboard or they can be the content of your Python game.

GOING GRAPHICAL

You can draw simple graphics, lines, squares and so on or you can use one of the many Python modules available, to bring out some spectacular effects.

STEP 1

One of the best modules to begin learning Python graphics is Turtle. The Turtle module is, as the name suggests, based on the turtle robots used in many schools that can be programmed to draw something on a large piece of paper on the floor. The Turtle module can be imported with: import turtle.

STEP 2

Let’s begin by drawing a simple circle. Start a New File, then enter the following code: import turtle

turtle.circle(50)
turtle.getscreen()._root.mainloop()

Pressing F5 saves the code and executes it as usual. You now have a new window open up and the ‘Turtle’ will draw a circle.

STEP 3

The command turtle.circle(50) is what draws the circle on the screen, with 50 being the size. You can play around with the sizes if you like, going up to 100, 150 and beyond; you can draw an arc by entering: turtle.circle(50, 180), so the size is 50 but you’re telling Python to only draw 180o of the circle.

STEP 4

The last part of the circle code tells Python to keep the window where the drawing is taking place to remain open, so the user can click to close it. Now, let’s make a square:

import turtle
print(“Drawing a square…”)
for t in range(4):
turtle.forward(100)
turtle.left(90)
turtle.getscreen()._root.mainloop()

You can see that we’ve inserted a loop to draw the sides of the square.

STEP 5

You can add a new line to the square code to add some colour: turtle.color(“Red”) You can even change the character to an actual turtle by entering: turtle.shape(“turtle”) We can also use the commands: turtle.begin_fill() and turtle. end_fill() to fill in the square with the chosen colours: in this case red outline, yellow fill.

STEP 6

You can see that the Turtle module can draw out some quite good shapes and become a little more complex as you begin to master the way it works. Enter this example:

from turtle import *
color(‘red’, ‘yellow’)
begin_fill()
while True:
       forward(200)
       left(170)
       if abs(pos()) < 1:
       break 
       end_fill()  
       done() 
It’s a different method but very effective.

STEP 7

Yet another way in which you can display graphics is by using the Pygame module. There are numerous ways in which Pygame can help you output graphics to the screen but for now let’s look at displaying a predefined image. Start by opening a browser and finding an image, then save it to the folder where you save your Python code.

STEP 8

Now let’s get the code by importing the Pygame module:
import pygame
pygame.init()
img = pygame.image.load(“RPi.png”)
white = (255, 255, 255)
w = 900
h = 450
screen = pygame.display.set_mode((w, h))
screen.fill((white))
screen.fill((white))
screen.blit(img,(0,0))
pygame.display.flip()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()

STEP 9

In the previous step you imported Pygame, initiated the Pygame engine and asked it to import our saved Raspberry Pi logo image, saved as RPi.png. Next you defined the background colour of the window to display the image and the window size as per the actual image dimensions. Finally there’s a quick (and dirty) loop to close the window.

STEP 10

Press F5 to save and execute the code and your image will be displayed in a new window. Have a play around with the colours, sizes and so on and take time to look up the many functions within the Pygame module too.

More information