python - turtle 代码被执行,但不在canvas中绘制

标签 python python-3.x recursion turtle-graphics

我目前正在准备一个简单的 Python 类(class)的期末考试,但是我似乎遇到了一些相当奇怪的事情。目标是创建一个具有错误异常检查功能的菜单,并允许用户启动预定义的功能。除了 drawKochFractal 函数之外,一切都运行良好。

它似乎编译和运行完全正常,我知道逻辑和数学是正确的。我什至添加了打印语句来检查它在控制台中的位置,并且它肯定正在运行。但 Canvas 窗口上从未绘制或显示任何内容!我是否忽略了某些东西,或者可能是我自己的 python 环境?谢谢!

elif select == 3:
            #Koch Function:
            """
            File: koch.py
            Project 7.3

            This program displays a Koch snowflake using
            the user's input level.
            """
            def drawKochFractal(width, height, size, level):
                """Draws a Koch fractal of the given level and size."""
                t.screen.colormode(255)
                t.pencolor(random.randint(1, 255),
                random.randint(1, 255),
                random.randint(1, 255))
                t.up()
                t.goto(-width // 3, height // 4)
                t.down()
                print("I am the begin")
                drawFractalLine(t, size, 0, level);
                print("I am here")
                drawFractalLine(t, size, -120, level)
                drawFractalLine(t, size, 120, level)

            def drawFractalLine(t, distance, theta, level):
                """Either draws a single line in a given direction
                or four fractal lines in new directions."""
                if (level == 0):
                    drawPolarLine(t, distance, theta)
                else:
                    drawFractalLine(t, distance // 3, theta, level - 1)
                    drawFractalLine(t, distance // 3, theta + 60, level - 1)
                    drawFractalLine(t, distance // 3, theta - 60, level - 1)
                    drawFractalLine(t, distance // 3, theta, level - 1)
                    print("I am here too!")

            def drawPolarLine(t, distance, theta):
                """Moves the given distance in the given direction."""
                t.setheading(theta)
                print("im turning!")
                t.forward(distance)

            width = input("Enter the width: ")
            height = input("Enter the height: ")
            size = input("Enter the size value: ")
            level = input("Enter the level (0 - 10): ")
            t = Turtle()
            t.hideturtle()
            t.screen.clear()
            #Let's make sure these variables are calculatable!
            try:
                width = int(width)
                height = int(height)
                size = int(size)
                level = int(level)
            except:
                print("####Invalid Response####")
                #If parameters do not work, send back to home menu
                main()
            #If level is not in correct range, send back to home menu with error response:
            if level > 10 or level < 0:
                print("####Level must be between 0 and 10####")
                main()
            #create drawKochFractal with user parameters, then take back to home menu:
            drawKochFractal(width, height, size, level)
            print("####Koch Fractal Complete! Taking you back to the main menu...####")
            main()

最佳答案

问题出在这里:

t = Turtle()
t.hideturtle()
t.screen.clear()

屏幕的 clear() 方法会损坏现有的用户 turtle 并重新创建默认的 turtle 。我建议你尝试:

from turtle import Screen, Turtle

# ...

screen = Screen()
screen.clear()
t = Turtle()
t.hideturtle()

也就是说,在屏幕被清除后创建你的 turtle ,并且不要使用你的 turtle 访问屏幕 clear() 方法,而是使用指向单个屏幕实例的独立指针。

enter image description here

关于python - turtle 代码被执行,但不在canvas中绘制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53635192/

相关文章:

python - 在scipy中使用pdist计算杰卡德距离

使用单独(顺序)Python 进程的 Python Nose 单元测试

python - 如何运行超过 10 万个任务的 Airflow dag?

Python 使用键 : possible to parallelize? 排序

python - 使用 smtplib 发送邮件在本地部署时有效,但在 swarm 中部署时失败(无法分配请求的地址)

python - 它在哪里被视为一个整数?

python - 如何在 Windows cmd 上打印不支持的 unicode 字符,例如 "?"而不是引发异常?

java - 如何通过递归显示链表中的元素?

scala - 如何在 Scala 中对列表中的两个邻居求和

java - 递归方法的问题