Saving output of Turtle Graphics?
Dick Moores
rdm at rcblue.com
Sat Apr 7 07:09:56 EDT 2007
I accidentally stumbled across the Turtle Graphics module (turtle.py)
the other day and have been having some fun with it.
Now I'm wondering if there is a way to build into a script the saving
of each window just before it is cleared. For example, here are a
couple that I've saved by screen capture:
<http://www.rcblue.com/Misc/RandomTriangles.jpg>
<http://www.rcblue.com/Misc/RandomTriangles2.jpg>
They were produced by this script:
=========================================================
# randomTriangles.py
import turtle as T
from random import *
def twoRndN(low=0, high=1):
"""
generate two random floats x, y in the range [low, high) such that x <= y
"""
x, y = uniform(low, high), uniform(low, high)
if x <= y:
return x, y
else:
return y, x
T.setup(width=1000, height=700, startx=0, starty=0)
T.title("Random Triangles with random R,G,B")
colorRange = "all"
if colorRange == "random":
lowR, highR = twoRndN()
lowG, highG = twoRndN()
lowB, highB = twoRndN()
count = 0
for n in range(300):
wdth = randrange(0,7,3)
T.width(wdth)
T.speed("fastest")
if colorRange == "dark":
R = uniform(.1, .5)
G = uniform(.1, .5)
B = uniform(.1, .5)
elif colorRange == "pastel":
R = uniform(.5, .9)
G = uniform(.5, .9)
B = uniform(.5, .9)
elif colorRange == "all":
R = uniform(0, 1)
G = uniform(0, 1)
B = uniform(0, 1)
# set RGB for one color of your choice
elif colorRange == "manual":
R = .45
G = .2
B = .2
elif colorRange == "random":
R = uniform(lowR, highR)
G = uniform(lowG, highG)
B = uniform(lowB, highB)
T.color(R,G,B)
T.begin_fill()
# 2 connected lines will fill as a triangle
for x in range(2):
coord = (randint(-500,500), randint(-350,350))
T.goto(coord)
T.end_fill()
count += 1
if count > 5:
clr = randint(0,5)
if clr == 0:
T.clear()
count = 0
T.done()
==============================================
(The docs for Turtle graphics for Tk are at
<http://www.python.org/doc/2.5/lib/module-turtle.html>)
But how could I have saved them "automatically"?
The script as shown will clear (T.clear() -- the 3rd line from the
bottom) the window after producing 6 to maybe 15 superimposed
triangles, so clearing will take place maybe 30 times. How can I save
as images each of the 30 windows just before they are cleared?
Thanks,
Dick Moores
More information about the Python-list
mailing list