Exercises for Lesson 6

Back to Lesson 6

Trying out graphics.py

Read through and experiment with this program, which draws a circle and a point to the screen using the graphics.py module.

Note that you’ll want the file graphics.py either saved in the same folder as where you save simpleShapes.py or any other file that imports it, or you’ll want graphics.py saved somewhere that is on your system path (if you’re not familiar with that or don’t want to, it’s easy enough to always copy it to the folder where you’re saving your code).

# simpleShapes.py
# A simple graphics program.

from graphics import *

def main():
    # Create a point object
    point = Point(50, 100)

    # Print out some information about that point
    x = point.getX()
    y = point.getY()
    print("The point is at ({0},{1})".format(x, y))

    # Create a circle object
    radius = 20
    circle = Circle(point, radius)

    # Print out some infrmation about that circle
    circleLoc = circle.getCenter()
    cx = circleLoc.getX()
    cy = circleLoc.getY()
    r = circle.getRadius()
    print("The circle is at ({0},{1}) and has radius {2}".format(cx, cy, r))

    # Give each a color
    point.setFill("white")
    circle.setFill("blue")

    # Create a window object
    win = GraphWin("My window for simple shapes")

    # Draw the circle and the point in the window
    circle.draw(win)
    point.draw(win)

    ## Question: does anything change if you swap the order of the draw statements?

main()

Back to Lesson 6