Graphics and Objects


In [ ]:
from graphics import *
## will crash if not using win.close()


def drawStuff():
    
    win = GraphWin()
    p = Point(50, 60)
    p.getX()
    p.getY()
    p.draw(win)
    p2 = Point(140, 100)
    p2.draw(win)
   ### Avoid terrible crashing with the following code ###
    c = win.getMouse()
    if c:
        win.close()
drawStuff()

In [ ]:
#from graphics import *

def drawMore():
    win = GraphWin('Shapes')
     ### Draw a red circle centered at 
    ### point (100, 100), radius 30
    center = Point(100, 100)
    circ = Circle(center, 30)
    circ.setFill('red')
    circ.draw(win)

    
    ### Put a textual label in the 
    ### center of the circle
    label = Text(center, "Red Circle!!!!")
    label.draw(win)
    ### Draw square using Rectangle object
    rect = Rectangle(Point(30, 30), Point(80, 70))
    rect.setFill("purple")
    rect.draw(win)
    ### Draw a line segment using a Line object
    line = Line(Point(20, 30), Point(180, 165))
    line.draw(win)
    ### Draw an oval using the Oval object
    oval = Oval(Point(20, 150), Point(180, 199))
    oval.setFill("peachpuff")
    oval.draw(win)

 ####Avoid terrible crashing with the following code!
    p= win.getMouse()
    if p:
        win.close()
drawMore()

Eye Example


In [ ]:
#import graphics
def drawEye():
    win = GraphWin("EYE")
    leftEye = Circle(Point(80,50), 5)
    leftEye.setFill('yellow')
    leftEye.setOutline('red')
    leftEye.draw(win)
    rightEye = leftEye.clone()
    rightEye.move(20,0)
    rightEye.draw(win)
    p = win.getMouse()
    if p:
        win.close()
drawEye()

Triangle Example


In [ ]:
###NB:  If your kernel crashes and needs to restart, don't forget to reimport graphics!
#from graphics import *

def drawTriangle():
    win = GraphWin("Triangle Time!")
    win.setCoords(0.0, 0.0, 10.0, 10.0)
    message = Text(Point(5, 0.5), "Click three points")
    message.draw(win)
    p1 = win.getMouse()
    p1.draw(win)
    p2 = win.getMouse()
    p2.draw(win)
    p3 = win.getMouse()
    p3.draw(win)
    triangle = Polygon(p1,p2,p3)
    triangle.setFill("peachpuff")
    triangle.setOutline("cyan")
    triangle.draw(win)
    message.setText("Click anywhere to quit")
    c = win.getMouse()
    if c:
        win.close()
drawTriangle()

Practice: Moving Circle


In [ ]:
def drawMovingCircle():
    win = GraphWin()
    shape = Circle(Point(50,50),20)
    shape.setOutline("green")
    shape.setFill("purple")
    shape.draw(win)
    for i in range(5):
        p = win.getMouse()
        c = shape.getCenter()
        dx = p.getX() - c.getX()
        dy = p.getY() - c.getY()
        shape.move(dx,dy)
    win.close()
drawMovingCircle()