from graphics import *Next create a new graphics window object, and then create gui objects to draw into this window:
win = GraphWin("My GUI Program", 500, 500) # creates new GraphWin object, 500x500 pixels in size win.setBackground("blue") # set the background color to blue circ = Circle(Point(50,50), 20) # creates a new Circle object centered at 50,50 with a radius of 20 pixels circ.setFill("red") # invoke the setFill method of the Circle object refered to by circ circ.draw(win) # draw the circ object to the GraphWin win
$ 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
There is a lot to sort through in the graphics library help documentation, so to simplify things a bit, here is some information about the GraphWin object and some of the Graphics objects you may want to use (section 5.8 of the Zelle text also contains a more complete reference for the classes in the graphics library):
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" # Sets the coordinate system of the window so that the lower # left corner is at (xll, yll) and the upper right corner is # at (xur, yur) setCoords(xll, yll, xur, yur) class Point ------------ Point(x, y) clone() # create a new Point object that is an exact copy of this one getX() # the int value of the Point's x-coordinate getY() # the int value of the Point's y-coordinate ----------------------- Methods commone 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) # p1 and p2 are the end Points of the line clone() setArrow(option) getCenter() # returns a Point object corresponding to the Line's center getP1() # get one end-point Point object getP2() # get the other Point object draw(graphwin) move(dx, dy) # move the Line dx pixels on the x-axis and dy on the y-axis setFill(color) setOutline(color) setWidth(width) class Rectangle --------------- Rectangle( p1, p2) # p1 and p2 are points for upper left lower rt clone() getCenter() # returns a Point object corresponding to the center getP1() # returns the upper left corner Point getP2() # returns the lower right corner Point draw(graphwin) move(dx, dy) # move the Rectangle dx pixels on x-axis, dy on y-axis setFill(color) setOutline(color) setWidth(width) # set the width, in pixels of the outline undraw() class Circle ---------- Circle(p1, radius) # p1 is a Point at the center of the circle, radius is an int clone() getCenter() # returns the Point object describing the Circle's center getRadius() # returns the int value of the Circle's radius 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) # distance in x and y axis to move the circle setFill(color) setOutline(color) setWidth(width) # the width of the circle's outline undraw() class Oval ---------- Oval(p1, p2) # p1 and p2 are Points of opposite corners of bounding rectangle clone() getCenter() getP1() getP2() draw(graphwin) move(dx, dy) setFill(color) setOutline(color) setWidth(width) class Polygon ------------- Polygon() # 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) width is an int 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()