【问题标题】:Hidden turtle showing up on exported PostScript file隐藏的海龟出现在导出的 PostScript 文件中
【发布时间】:2019-10-19 07:30:45
【问题描述】:

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

【问题讨论】:

    标签: python python-3.x tkinter turtle-graphics tkinter-canvas


    【解决方案1】:

    替换你的:

    ts = turtle.getscreen()
    

    与:

    ts = turtle.Screen()
    

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

    而脱颖而出的默认乌龟

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-24
      • 1970-01-01
      • 2018-11-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多