Exercises for 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).
# File: simpleShapes.py
# Purpose: A simple graphics program.
# Author: Tanya Amert
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 information about that circle
circleLoc = circle.getCenter()
cx = circleLoc.getX()
cy = circleLoc.getY()
r = circle.getRadius()
print(f"The circle is at ({cx},{cy}) and has radius {r}")
# ^ documentation for format strings: https://docs.python.org/3/tutorial/inputoutput.html#tut-f-strings
# 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)
## Questions:
# - Does anything change if you swap the order of the draw statements?
# - How can you change the size of the circle?
# - How can you change the color of the circle?
# - How big is a Point?
# - Which direction has increasing x values? What about increasing y values?
# Wait for the user to click, then exit
win.getMouse()
win.close()
main()