Turtle 代码已执行,但未绘制 canvas
Turtle code is executed, but does not draw in canvas
我目前正在为一门简单的 Python 课程准备期末考试,但是我似乎遇到了一些相当奇怪的事情。目标是创建一个带有错误异常检查的菜单,并允许用户启动预定义的功能。对于 drawKochFractal
函数,一切都很好 EXCEPT。
似乎编译 运行 完全没问题,而且我知道逻辑和数学是正确的。我什至添加了打印语句来检查它在控制台中的位置,它是 definitely运行ning。但是 canvas window 上没有绘制或显示任何内容!我是否忽略了什么,或者可能是我自己的 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()
方法会损坏现有的用户海龟并重新创建默认海龟。我建议你试试:
from turtle import Screen, Turtle
# ...
screen = Screen()
screen.clear()
t = Turtle()
t.hideturtle()
也就是说,在清除屏幕后创建你的乌龟并且不要使用你的乌龟访问屏幕clear()
方法,使用一个独立的指针指向单个屏幕实例。
我目前正在为一门简单的 Python 课程准备期末考试,但是我似乎遇到了一些相当奇怪的事情。目标是创建一个带有错误异常检查的菜单,并允许用户启动预定义的功能。对于 drawKochFractal
函数,一切都很好 EXCEPT。
似乎编译 运行 完全没问题,而且我知道逻辑和数学是正确的。我什至添加了打印语句来检查它在控制台中的位置,它是 definitely运行ning。但是 canvas window 上没有绘制或显示任何内容!我是否忽略了什么,或者可能是我自己的 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()
方法会损坏现有的用户海龟并重新创建默认海龟。我建议你试试:
from turtle import Screen, Turtle
# ...
screen = Screen()
screen.clear()
t = Turtle()
t.hideturtle()
也就是说,在清除屏幕后创建你的乌龟并且不要使用你的乌龟访问屏幕clear()
方法,使用一个独立的指针指向单个屏幕实例。