隐藏的乌龟出现在导出的 PostScript 文件中

Hidden turtle showing up on exported PostScript file

我已经编写了一些代码,我想将 canvas 导出为图像,但是每次我 运行 代码时,海龟被隐藏,形状被绘制,然后海龟由于某种原因重新出现,图像被保存。我尝试将 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()

替换你的:

ts = turtle.getscreen()

与:

ts = turtle.Screen()

出现在您绘图中的乌龟不是您的乌龟,而是您通过呼叫 turtle.getscreen()