我写了一些代码,我想将画布导出为图像,但是每次运行代码时,乌龟被隐藏,形状被绘制,然后由于某种原因,乌龟重新出现,图像被保存。我尝试将t.ht()
添加到图像保存部分中的每隔一行,但都无济于事。完整的代码发布在下面
import turtle
from itertools import combinations
from math import pi, cos, sin
def points(n, r):
"""Generate a list of points making the vertices of a regular n-gon centred at the origin"""
return [(r * cos(2 * pi * i / n), r * sin(2 * pi * i / n)) for i in range(n)]
def rotate(points, a):
"""Rotate a given list of points by the angle a (in radians) about the origin"""
return [(x*cos(a) - y*sin(a), y*cos(a) + x*sin(a)) for x, y in points]
def scale(points, s):
"""Scale a given list of points by the scale factor s about the origin"""
return [(x*s, y*s) for x, y in points]
def complete_poly(points, n):
"""Draw a connected graph using the points specified"""
t.pu()
t.goto(points[-1])
t.pd()
for ix, iy in combinations(range(n), 2):
t.goto(points[ix])
t.goto(points[iy])
def incomplete_poly(points, n):
"""Draw a graph connecting only nodes seperating by a single node or none"""
t.pu()
t.goto(points[-1])
t.pd()
for inner in range(n):
t.pu()
t.goto(points[inner])
t.pd()
t.goto(points[(inner+2)%n])
def outer_poly(points, n):
"""Draw only the edges of an n-gon defined by points"""
t.pu()
t.goto(points[-1])
t.pd()
for side in range(n):
t.goto(points[side])
t = turtle.Turtle()
t.lt(90)
t.speed(0)
t.ht()
# n is the number of sides of the polygon it draws
# i is the number of iterations
# r is the starting radius of the polygon
n = 7
i = 10
r = 200
s = 2*cos(pi/n) - 1/cos(pi/n)
p = points(n, r)
outer_poly(p, n)
for _ in range(i):
incomplete_poly(p, n)
p = scale(rotate(p, pi/n), s)
ts = turtle.getscreen()
ts.getcanvas().postscript(file="7-intergon.eps")
turtle.done()
发布于 2019-06-04 03:19:06
替换您的:
ts = turtle.getscreen()
通过以下方式:
ts = turtle.Screen()
绘图中显示的海龟不是您的海龟,而是您通过调用turtle.getscreen()
将默认海龟带到最前面的海龟
https://stackoverflow.com/questions/56432517
复制相似问题