To use the graphics library, first import it at the top of your program:
from graphics import *
Next create a new graphics window object, and then create gui objects
to draw into this window:
# creates new GraphWin object, 500x500 pixels in size
win = GraphWin("My GUI Program", 500, 500)
# creates a new Circle object centered at 50,50 with a radius of 20 pixels
circ = Circle(Point(50,50), 20)
# invoke the setFill method of the Circle object refered to by circ
circ.setFill("red")
# draw the circ object to the GraphWin win
circ.draw(win)
Here is some information about the GraphWin object and some of the
Graphics objects you can use (section 5.8 of the text contains a complete
reference for the classes in the graphics library):
class GraphWin
GraphWin(title, width, height) #Constructor-makes a new GraphWin object
close()
flush() # update drawing to graphics window
getHeight()
getWidth()
# wait for mouse click and return Point obj representing click location
getMouse()
setBackground(color)
setMouseHandler(func) # func will be executed on mouse click
class Point
Point(x, y) #Constructor
clone() # create a new Point object that is an exact copy of this one
getX()
getY()
Methods common to all Graphics Object classes:
draw(graphwin): # Draw the object in graphwin, which should be a
# GraphWin object. A GraphicsObject may only be drawn
# into one window.
move(dx, dy): # move object dx units in x and dy units in y direction
setFill(color): # Set interior color to color
setOutline(color): # Set outline color to color
setWidth(width): # Set line weight to width
undraw(): # Undraw the object (i.e. hide it).
class Line
Line(p1, p2)
clone()
setArrow(option)
getCenter()
getP1()
getP2()
draw(graphwin)
move(dx, dy)
setFill(color)
setOutline(color)
setWidth(width)
class Rectangle
Rectangle( p1, p2) # p1 and p2 are points for upper left lower rt
clone()
getCenter()
getP1()
getP2()
draw(graphwin)
move(dx, dy)
setFill(color)
setOutline(color)
setWidth(width)
undraw()
class Circle
Circle(p1, radius) # p1 is a Point at the center of the circle
clone()
getCenter()
getRadius()
getP1() # returns a clone of the corresponding corner of the
getP2() # circle's bounding box (opposite corners of bounding square)
draw(graphwin)
move(dx, dy)
setFill(color)
setOutline(color)
setWidth(width)
undraw()
class Oval
Oval(p1, p2) # p1 and p2 are opposite corners of bounding rectangle
clone()
getCenter()
getP1()
getP2()
draw(graphwin)
move(dx, dy)
setFill(color)
setOutline(color)
setWidth(width)
class Polygon
Polygon(<list of points>)
# ex. triangle: Polygon ([Point(10,20), Point(30,40), Point(10,60)])
getPoints() # return a list of Points in the polygon
clone()
draw(graphwin)
move(dx, dy)
setFill(color)
setOutline(color)
setWidth(width)
undraw()
class Text
Text(p, text) # p is center point and text is string
clone()
getText()
setText(text)
setTextColor(color)
setFace(family) # set font face, ex. "arial" "courier"
setSize(size) # set font size (5-36 are legal)
setStyle(style) # set font style ex. "bold" "italic"
draw(graphwin)
move(dx, dy)
setFill(color)
setOutline(color)
setWidth(width)
undraw()
class Entry
GUI object into which user can type text
Entry(pt, width) # pt is anchor Point (center)
getAnchor() # returns the center Point
getText() # returns the current text
setText(string) # sets the text to string
setSize(point) # sets the found size (5-36 are legal)
setStyle(style) # sets the font style
setTextColor(color) # sets the text color
draw(graphwin)
move(dx, dy)
setFill(color)
setOutline(color)
setWidth(width)
undraw()
Animation
For animation, you may need to import the
sleep function from the
time module.
from time import sleep
sleep(0.5) #pause for 0.5 seconds