Python 类利用 Turtle 对象在 Windows 上不工作,在 Linux 上工作

标签 python linux windows turtle-graphics

下面的代码在 Linux 机器上执行良好,但在 Windows 上不起作用。我不确定为什么。

我也尝试了添加 d.getscreen().mainloop() 的建议答案,但无济于事。该代码已在两台不同的 Windows 机器上进行了测试。

import turtle
import time

class Display(turtle.Turtle):
    def __init__(self):
        turtle.Turtle.__init__(self)
        self.screen = self.getscreen()
        self.hideturtle()
        self.speed(0)
        self.screen.tracer(0)
        self.screen.colormode(255)

    def koch(self, order, size):
        # self.forward(size)
        # self.left(order)
        if order == 0:
            self.forward(size)
            return
        else:
            for angle in [60, -120, 60, 0]:
                self.koch(order - 1, size / 3)
                self.left(angle)

    def clearscreen(self):
        self.clear()

    def moveTurtle(self, x=50, y=50):
        self.goto(self.xcor() - x, self.ycor() + 50)

def main():
    d = Display()

    prompt = True
    while prompt:
        order = input("What order do you want your fractal? ")
        size = input("What size do you want your fractal? ")
        if order == '' or size == '':
            prompt = False
        else:
            d.koch(int(order), int(size))
        d.penup()
        d.moveTurtle()
        d.pendown()

if __name__ == '__main__':
    main()

最佳答案

我的猜测是问题出在这里:

self.screen.tracer(0)

您关闭了图形并且再也没有打开它们。这需要在某个时候由以下任一者遵循:

self.screen.tracer(1)

或:

self.screen.update()

或两者皆有。

出于测试目的,将其完全注释掉。某些操作会导致更新发生(例如 penup()),但这可能是特定于实现以及您正在运行的内容。

我对您的代码进行了修改以合并上述内容和一些样式更改:

from turtle import Turtle

class Display(Turtle):
    def __init__(self):
        Turtle.__init__(self, visible=False)
        self.screen = self.getscreen()
        self.speed('fastest')

    def koch(self, order, size):
        if order == 0:
            self.forward(size)
            return

        for angle in [60, -120, 60, 0]:
            self.koch(order - 1, size / 3)
            self.left(angle)

    def moveTurtle(self, x=50, y=50):
        self.goto(self.xcor() - x, self.ycor() + y)

def main():
    d = Display()

    prompt = True

    while prompt:
        order = input("What order do you want your fractal? ")

        if order == '':
            prompt = False
            continue

        size = input("What size do you want your fractal? ")

        if size == '':
            prompt = False
            continue

        d.screen.tracer(False)
        d.koch(int(order), int(size))
        d.screen.tracer(True)

        d.penup()
        d.moveTurtle()
        d.pendown()

if __name__ == '__main__':
    main()

关于Python 类利用 Turtle 对象在 Windows 上不工作,在 Linux 上工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49837756/

相关文章:

python - PyCharm 覆盖用作解释器的 docker 容器中的 PYTHONPATH

python - python 中的强数字

linux - gcc架构问题

在 "index"的情况下,PHP $_GET 无法获取 RewriteRule 的值

Java 线程在 Linux 中进入 hibernate 状态

windows - 安装程序 : WIX or Inno Setup?

windows - 忘记了 Postgres 的管理员密码(Windows 安装),无法重置

python - 选择性分组依据,np.where Pandas/Python 查询

python - Pandas 进行字典查找的惯用方式

windows - 如何隐藏特定的 Windows 更新?