Objects & Graphics 2

A graphics.py Tutorial

First, let's import a whole slew of items from the graphics module.

In [1]:
from graphics import GraphWin, Point, Circle, Text, Rectangle, Line, Oval

Make a New Window

It is isn't necessary to make a window before creating your shapes and text, but we do it anyways.

In [2]:
win = GraphWin(title="Shapes", width=600, height=600)

Circle

We make a Circle object. It needs a center point for it to be drawn, as well as a radius. The centerpoint is done through a Point object we created before making the circle.

In [3]:
center = Point(300,300)

circ = Circle(center, 70)
circ.setFill("red")
circ.draw(win)
Out[3]:
Circle(Point(300.0, 300.0), 70)

Play around with some colors!

In [4]:
circ.setFill("purple")
circ.setFill("azure")
circ.setFill("Cadet Blue")

Text

A Text object is used to to draw, yes, text! It places the text at its center point of the text placed at some $x$ and $y$ positioned defined by a Point object.

In [5]:
label = Text(center, "Blue Circle")
label.draw(win)
Out[5]:
Text(Point(300.0, 300.0), 'Blue Circle')

Namespace

Take a look at what's in your namespace now.

In [6]:
print(dir())
['Circle', 'GraphWin', 'In', 'Line', 'Out', 'Oval', 'Point', 'Rectangle', 'Text', '_', '_3', '_5', '__', '___', '__builtin__', '__builtins__', '__doc__', '__loader__', '__name__', '__package__', '__spec__', '_dh', '_i', '_i1', '_i2', '_i3', '_i4', '_i5', '_i6', '_ih', '_ii', '_iii', '_oh', 'center', 'circ', 'exit', 'get_ipython', 'label', 'quit', 'win']

Rectangle

A lower-left and upper-right pair of points are needed. We can define them inside the call to Rectangle() to save us from defining them as separate variables.

In [7]:
rect = Rectangle(Point(40, 60), Point(70*2, 70*2))
rect
rect.draw(win)
rect.setOutline("cyan")
rect.setFill("Dark Khaki")

Line

Line is very similar to Rectangle in that it needs two points (endpoints) to draw.

In [8]:
line = Line(Point(20*5, 30*5), Point(180*3, 165*3))
line.draw(win)
line.setFill("Dark Salmon")

Not Last, Not Least: Oval

There are still more data types (classes) to use from the graphics module, but our examples stop here for now.

In [10]:
oval = Oval(Point(20*5, 30*5), Point(180*3, 165*3))
oval.draw(win)
oval.setFill("Blanched Almond")

You can make a graphics object dissapear by using .undraw(). No reference to a window is needed because a graphics object already knows which window it is associated with if it has been drawn. Call .draw(win) to bring it back!

In [11]:
oval.undraw()
oval.draw(win)
Out[11]:
Oval(Point(100.0, 150.0), Point(540.0, 495.0))

You can make any number of windows you need. Let's make a second window called win_alt and "move" the oval we just made to this new window. Notice you can't just draw it on the new window. If it is already drawn on, say win, the method .undraw() must be called on oval so that it can then be drawn on win_alt.

In [13]:
win_alt = GraphWin("Shapes Overflow", 600, 600)
oval.draw(win_alt)  # This will fail because it's already drawn!
oval.undraw()
oval.draw(win_alt)  # Now it will work!
---------------------------------------------------------------------------
GraphicsError                             Traceback (most recent call last)
<ipython-input-13-db0aa51761be> in <module>()
      1 win_alt = GraphWin("Shapes Overflow", 600, 600)
----> 2 oval.draw(win_alt)  # This will fail because it's already drawn!
      3 oval.undraw()
      4 oval.draw(win_alt)  # Now it will work!

~/Library/Python/3.6/lib/python/site-packages/graphics.py in draw(self, graphwin)
    479         is already visible."""
    480 
--> 481         if self.canvas and not self.canvas.isClosed(): raise GraphicsError(OBJ_ALREADY_DRAWN)
    482         if graphwin.isClosed(): raise GraphicsError("Can't draw to closed window")
    483         self.canvas = graphwin

GraphicsError: Object currently drawn