Additions to Zelle's Graphics for the Hands-on Python Tutorials
Normal (y increases upward) Coordinates
GraphWin method yUp
When you first create a GraphWin, the y coordates increase down the screen. To reverse to the normal orientation use my GraphWin yUp method.
win = Graphwin('Right side up', 300, 400)
win.yUp()
Prompt and Close Graphics Window
GraphWin method promptClose
You generally want to continue displaying your graphics window until
the user chooses to have it closed. The GraphWin promptClose
method posts a prompt, waits for a mouse click, and closes the
GraphWin. There are two ways to call it, depending on whether you
want to use an existing Text object, or just specify a location for the
center of the prompt.
win.promptClose(win.width/2, 30) # specify x, y coordinates for prompt
or
msg = Text(Point(100, 50), 'Original message...')
msg.draw(win)
# ...
# ... just important that there is a drawn Text object
win.promptClose(msg) # use existing Text object
String Representations of all Graphics Object Types
Each graphical type can be converted to a string or printed, and a
descriptive string is produced (for debugging purposes). It only
shows position, not the current color or style.
>>> pt = Point(30, 50)
>>> print(pt)
Point(30, 50)
>>> ln = Line(pt, Point(100, 150))
>>> print(ln)
Line(Point(30, 50), Point(100, 150))
Asynchonous Detection of the Mouse
Graphwin method checkMouse
A Graphwin method is included in Zelle's package (but not documented) for detecting the last mouse click
checkMouse()
Returns the last mouse click point detected (or None if the mouse has not been clicked since the last call to getMouse() or checkMouse()). This method is unlike getMouse(), in that it does not wait for a click. It returns the most recent click in the past, if it has not already been returned, no matter what the program was doing at the time.
In the example below, the animation stops when the mouse has been clicked.
from graphics import *
import time
win = GraphWin()
circle = Circle(Point(100,100), 15)
circle.draw(win)
Text(Point(100, 40), "Click to quit.").draw(win)
while win.checkMouse() == None:
circle.move(50, 0)
time.sleep(.5)
circle.move(-50, 0)
time.sleep(.5)
win.close()