Graphics.py

Download/Documentation

1. Download the latest graphics.py from HERE image missing.

2. Download graphics.py V5 documentation from HERE image missing. (pdf)

3. Copy graphics.py to the same directory as your Python programs.

Note: You may need to convert the file by using the DOS2UNIX program.

Brief Description

Graphics.py is a simple object oriented graphics library. It is designed to make it very easy for novice programmers to experiment with computer graphics in an object oriented fashion. It is written by John Zelle for use with the book "Python Programming: An Introduction to Computer Science" (Franklin, Beedle & Associates).

The package is a wrapper around Tkinter and should run on any platform where Tkinter is available.

There are two kinds of objects in the library. The GraphWin class implements a window where drawing can be done and various GraphicsObjects are provided that can be drawn into a GraphWin.

Graphics.py V5 is an open-source software released under the terms of the GPL (http://www.gnu.org/licenses/gpl.html).

What if I am Drawing a Plot?

I suggest if you are drawing a plot, create a plot class. It should be independent of the particular problem you are currently working on. This way you can use it over and over again.

Define/create the methods you will need in this class. For example

Start slow. Create a plot class with a few (minimal but important) methods at first. Over time add or modify them as needed.

Simple Example Program

Here is a simple example. It is a complete program to draw a circle of radius 10 centered in a 100x100 window:

from graphics import *

def main():
    win = GraphWin("My Circle", 100, 100)
    c = Circle(Point(50,50), 10)
    c.draw(win)
    win.getMouse() # Pause to view result
    win.close()    # Close window when done

main()
Display the graphic.py version
import graphics as gr

print(f'graphics.py version is {gr.__version__}')

Graphics.py is Missing a Button

How would you create a button? You would want to click on it and have some action performed. See the following code for a hint.

# Create a rectangle Or other shape. Was the mouse click inside it?

while True:

    mp = win.checkMouse()      # mouse click location (if any)

    if mp is not None:         # was their a mouse click?
        x = mp.getX()          # mouse click x coordinate
        y = mp.getY()          # mouse click y coordinate

        if x > 10 and x < 50 and y > 10 and y < 50:

            Perform Some Action

Links

(YouTube)
graphics.py example program (python)
Python Graphics Programming (Graphics.py 1): The Basics
Python Graphics Programming (Graphics.py 2): Primitive shapes
Python Graphics Programming (Graphics.py 3): Images and text
Python Graphics Programming (Graphics.py 4): Text boxes