from graphics import *Note that the origin of the graphics window is at the top left corner. Here is a short example showing how to create a new graphics window object, create graphical objects, and then draw them in the window:
win = GraphWin("Test", 500, 500) # creates new GraphWin object, 500 pixels in width and 500 pixels in height p = Point(50, 50) # creates a new Point object at 50,50 c = Circle(p, 20) # creates new Circle object centered at point p with a radius of 20 pixels c.setFill("red") # invokes the setFill method of the Circle object c.draw(win) # draws the Circle object in the window win
REFERENCE INFO BELOW
You can see all of the classes in the graphics library and their methods by running help(graphics) in interactive mode:
$ python >>> import graphics >>> help(graphics)To see the full set of colors available to you:
$ python >>> from colorPicker import * >>> colorPicker()then click on a color and its name will show up in the python interpreter
Below is a short summary of the graphical objects that you will be focusing on in this lab.
class GraphWin -------------- GraphWin(title, width, height) close() flush() # update drawing to graphics window getHeight() getWidth() getMouse() # wait for mouse click and return Point obj representing click location setBackground(color) # color could be something like "red"
Example: mywin = GraphWin("ponies!!", 600, 500) mywin.setBackground("lightblue")
Methods common to all Graphics objects -------------------------------------- draw(graphwin): # Draw the object in graphwin, which should be a # GraphWin object. A GraphicsObject may only be drawn # into one window. undraw() # Undraw the object (i.e. hide it). clone() # create a new object that is an exact copy of this one 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
class Point ------------ Point(x, y) getX() # the int value of the Point's x-coordinate getY() # the int value of the Point's y-coordinate
Example: p1 = Point(300, 450) p1.draw(mywin) x1 = p1.getX() x2 = p1.getY()
class Line ---------- Line(p1, p2) # p1 and p2 are the end Points of the line getCenter() # returns a Point object corresponding to the Line's center getP1() # get one end-point Point object getP2() # get the other Point object
Example: p1 = Point(0, 250) p2 = Point(600, 250) longline = Line(p1, p2) longline.setWidth(5) longline.draw(mywin)
class Circle ---------- Circle(p1, radius)# p1 is a Point at the center of the circle, radius is an int getCenter() # returns the Point object describing the Circle's center getRadius() # returns the int value of the Circle's radius
Example: center = Point(100, 100) radius = 35 sun = Circle(center, radius) sun.setFill("yellow") sun.setOutline("yellow") sun.draw(mywin)