This module is known also as graphics.py
or Zelle's graphics
and you should use these names to search more information.
Home Page for graphics.py with documentation and examples
Hands-on Python Tutorial > Graphics
You could also check source code to see all classes and functions.
This module is built on top of tkinter
so you may have to learn also tkinter
for some functions which are not available directly in graphics.py
but you still can use it.
For example tkinter
has after(milliseconds, function_name)
to execute function with delay or repeate the same function after delay - so you can use it to create animation.
BTW: in after()
has to be function's name without ()
and without arguments - and later tkinter
will use ()
to execute it. It is called callback
.
Example which uses after()
to move point. This way it animates element and you still can click window to close program.
from graphics import * # PEP8: `import *` is not preferred
import random
# --- functions ---
def move():
dx = random.randint(-10, 10)
dy = random.randint(-10, 10)
pt.move(dx, dy)
# move again after 100ms (0.1s)
win.after(100, move)
# --- main ---
win = GraphWin("My Window",500,500)
win.setBackground(color_rgb(0,0,0))
pt = Point(250, 250)
pt.setOutline(color_rgb(255,255,0))
pt.draw(win)
# move first time after 100ms (0.1s)
win.after(100, move)
#win.mainloop()
win.getMouse()
win.close()
EDIT: after digging in information I created version without after()
from graphics import * # PEP8: `import *` is not preferred
import random
import time
# --- main ---
win = GraphWin("My Window",500,500)
win.setBackground(color_rgb(0,0,0))
pt = Point(250, 250)
pt.setOutline(color_rgb(255,255,0))
pt.draw(win)
while True:
if win.checkMouse():
break
dx = random.randint(-10, 10)
dy = random.randint(-10, 10)
pt.move(dx, dy)
time.sleep(0.1)
win.close()