graphics using the Zelle library

Try the Jupyter notebook version.


Two things for this week: have fun with graphics/drawing pictures, learn about objects and object-oriented programming (OOP).

Technically, almost everything in python is an object. Most objects have both data and functions (now called methods), together in one thing (the object).

Here are some simple examples of calling methods on a python list object:

>>> L = []
>>> L.append("A")
>>> L.append("B")
>>> L.append("C")
>>> print(L)
['A', 'B', 'C']
>>> L.reverse()
>>> print(L)
['C', 'B', 'A']
>>> L.sort()
>>> print(L)
['A', 'B', 'C']
>>> L.count("A")
>>> L.count("Z")

The list methods used above are append() (add an item to the list), reverse() (reverse the order of the list), sort() (sort items from low to high), and count() (count how many are in list). There are other python list methods available.

Here are some string methods, acting on a string object:

>>> S = "swarthmore"
>>> S.upper()
>>> S.count("r")
>>> S.index("w")
>>> S.isalpha()
>>> print(S)

The string methods used above are: upper() (return uppercase version of string), count() (count how many are in string), index() (show where char is in string), and isalpha() (return True if string in only alphabetic chars). There are many other python str methods available.

Note: strings are immutable, so even calling S.upper() doesn't change what is stored in S (it just returns a new, uppercase, string). If you want to change what is stored in the variable S, you could use S = S.upper().

Using objects in your programs is just a different way to organize your data, but also provides advantages as programs and programming teams get larger and more complex (divide and conquer, code reuse, easier to read/write/debug, etc).


The syntax for calling methods on objects is object.method(), where the method may or may not require arguments.

This week we will learn the Zelle graphics library, which is an excellent example of using objects (i.e., it's easy to see the objects, because they are graphical objects, like circles and squares, drawn on the screen).

To use the Zelle graphics library, we need to import the classes and functions with this at the top of our programs:

from graphics import *

(and don't name your program, because that's what the Zelle graphics file you are importing is named).


This program creates a graphics window and draws a circle in the middle of the window:

from graphics import *

def main():

  width = 400
  height = 400
  gw = GraphWin("first graphics!", width, height)

  cp = Point(width/2, height/2)   # center point
  c = Circle(cp, width/3)

  # wait for click to end


Things to note:

And here's the image:

circle graphics

circle graphics


Look through the documentation on the Zelle graphics library and learn about all of the possible objects (Circle, Rectangle, Text, etc), as well as the methods that go with each object (draw(), move(), setFill(), etc).

See if you can draw this simple scene:

tree and sun graphics

tree and sun graphics

CS21 Topics