We begin with an import of all the stuff we need. We could have used
import graphics and referenced everything with the prefix of
graphics., but since we know eactly what we need, we can import then selectively.
Note: I used the "
\" (backslash) character to indicate a line will be continued on the next line. It also makes it a little nicer to read.
from graphics import GraphWin, Point, Circle, Polygon, \ Rectangle, Line, Oval, Text
The graphics library is a simple library for graphics drawing under Python. It's use is mainly for educational purposes, as there are many limitations with it.
Let's start by introducting a basic graphics object, the
Point class. The class creates as many
Point instances you want. It's a basic object. Note that we are creating it before we even have a window to draw on. Not a problem! We can set up the point
p to our liking and then display it.
The point below will be created, stored in
p and placed at x=50 and y=60 in whatever window will display it.
I've introduced you to objects in the previous lecture. This lecture goes into more depth about objects and how they are made. Every graphical object you are making is an object — it has data (state) and methods (actions on that state).
p = Point(50, 60)
Let's create a window in which to draw our objects. It's quite simple in this one form. We call the
GraphWin class constructor to make a window. We can make many windows, but one will suffice here. We store a
GraphWin window instance in the variable
win = GraphWin()
Let's have Python evaluate the
win variable as an expression. That allows Python to tell us what this object is. You know, just because.
When you evaluate a Python object, it gives you an unambigious representation of how this object was created.
So we see that
win represents a
GraphWin instance that was created with the title
'Graphics Window', a width of
200, and a height of
200. These are default values! Notice we made the window above without providing these parameters. If you leave them out, they are choosen for you by default.
GraphWin('Graphics Window', 200, 200)
We have an idea how to make a window, a basic graphical object (
Point), how about just drawing already! OK, let's start by drawing the point
p we made earlier.
To draw a graphics object in this library, each object needs to be associated with a window. We tell the object using its
.draw() method to associate with a particular window.
It will then appear!
We can set point
p's color to red. This will occur whether or not it was currently drawn on a window. We are setting the state of this object.
Let's make a second point. Why not!? Let's call it
p2 place it at x = 140 and y = 100 and draw it on window
p2 = Point(140, 100) p2.draw(win)
center = Point(100, 100) circ = Circle(center, 30) circ.draw(win) Circle(Point(100.0, 100.0), 30) circ.setFill("purple")
label = Text(center, "Red Circle") label.draw(win) Text(Point(100.0, 100.0), 'Red Circle')
Text(Point(100.0, 100.0), 'Red Circle')
rect = Rectangle(Point(20, 30), Point(70, 70)) rect.draw(win)
Rectangle(Point(20.0, 30.0), Point(70.0, 70.0))
line = Line(Point(20, 30), Point(180, 165)) line.draw(win) Line(Point(20.0, 30.0), Point(180.0, 165.0))
Line(Point(20.0, 30.0), Point(180.0, 165.0))
oval = Oval(Point(20, 150), Point(180, 199)) oval.draw(win) Oval(Point(20.0, 150.0), Point(180.0, 199.0))
Oval(Point(20.0, 150.0), Point(180.0, 199.0))
I've been using the term "classes" carelessly throughout these notes. What the heck are "classes"?
Classes are a object that looks a lot like functions. You call them like functions. They return values like a function. They are not functions!
Classes are program components that you can write that create instances of itself that are distinct and unique. These instances are set up to handle the storage of data within them, and methods to manipulate that data.
In effect, you create data types using classes. A
GraphWin, etc are all data types! They represent data for a graphics objects they are named after and have data and methods (actions) that are appropriate for that data.
In Python traditional style, all classes are named using "Pascal Case", or "Modified Camel Case" which means each work in the name of the class has its first letter capitalized (e.g.
def main(): pass type(main)
Below are statements that make the point about how objects in Python work.
Point(50.0, 60.0) dir(p)
['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_draw', '_move', '_reconfig', 'canvas', 'clone', 'config', 'draw', 'getX', 'getY', 'id', 'move', 'setFill', 'setOutline', 'setWidth', 'undraw', 'x', 'y']
p.getX() 50.0 p.x 50.0
When in doubt, use the
help function and give it any name defined in Python, and there is very likely help available within it. Let's get help on our variable
circ, which is a
Circle class instance.
Help on Circle in module graphics object: class Circle(Oval) | Generic base class for all of the drawable objects | | Method resolution order: | Circle | Oval | _BBox | GraphicsObject | builtins.object | | Methods defined here: | | __init__(self, center, radius) | Initialize self. See help(type(self)) for accurate signature. | | __repr__(self) | Return repr(self). | | clone(self) | | getRadius(self) | | ---------------------------------------------------------------------- | Methods inherited from _BBox: | | getCenter(self) | | getP1(self) | | getP2(self) | | ---------------------------------------------------------------------- | Methods inherited from GraphicsObject: | | draw(self, graphwin) | Draw the object in graphwin, which should be a GraphWin | object. A GraphicsObject may only be drawn into one | window. Raises an error if attempt made to draw an object that | is already visible. | | move(self, dx, dy) | move object dx units in x direction and dy units in y | direction | | setFill(self, color) | Set interior color to color | | setOutline(self, color) | Set outline color to color | | setWidth(self, width) | Set line weight to width | | undraw(self) | Undraw the object (i.e. hide it). Returns silently if the | object is not currently drawn. | | ---------------------------------------------------------------------- | Data descriptors inherited from GraphicsObject: | | __dict__ | dictionary for instance variables (if defined) | | __weakref__ | list of weak references to the object (if defined)