英文:
The error "turtle.Terminator" occurs when using turtle
问题
I have created a function which uses the turtle module in python:
def printmaze(sizeX, sizeY, walls):
startX = -300
startY = 300
high = (-startX // sizeX) * 2
width = (startY // sizeY) * 2
turtle.clear()
turtle.speed(0)
turtle.penup()
turtle.goto(-startX, startY)
turtle.pendown()
turtle.goto(-startX, -startY)
turtle.goto(startX, -startY)
turtle.setheading(0)
for i in range(sizeX):
turtle.penup()
turtle.goto(startX, startY - high * i)
for l in range(sizeY):
if walls[i][l][1] == 1:
turtle.pendown()
else:
turtle.penup()
turtle.forward(width)
turtle.right(90)
for i in range(sizeY):
turtle.penup()
turtle.goto(startX + width * i, startY)
for l in range(sizeX):
if walls[l][i][0] == 1:
turtle.pendown()
else:
turtle.penup()
turtle.forward(high)
screen = turtle.Screen()
screen.exitonclick()
When I execute this function for the first time, there is no error. However, when I execute it for the second time, it returns turtle.Terminator. Why is this happening?
英文:
I have created a function which uses the turtle module in python:
def printmaze(sizeX, sizeY, walls):
startX = -300
startY = 300
high = (-startX // sizeX) * 2
width = (startY // sizeY) * 2
turtle.clear()
turtle.speed(0)
turtle.penup()
turtle.goto(-startX, startY)
turtle.pendown()
turtle.goto(-startX, -startY)
turtle.goto(startX, -startY)
turtle.setheading(0)
for i in range(sizeX):
turtle.penup()
turtle.goto(startX, startY - high * i)
for l in range(sizeY):
if walls[i][l][1] == 1:
turtle.pendown()
else:
turtle.penup()
turtle.forward(width)
turtle.right(90)
for i in range(sizeY):
turtle.penup()
turtle.goto(startX + width * i, startY)
for l in range(sizeX):
if walls[l][i][0] == 1:
turtle.pendown()
else:
turtle.penup()
turtle.forward(high)
screen = turtle.Screen()
screen.exitonclick()
When I execute this function for the first time, there is no error. However, when I execute it for the second time, it returns turtle.Terminator. Why is this happening?
答案1
得分: 1
屏幕 = turtle.Screen() # 在函数之外的开头调用
# 创建绘制迷宫的函数
def 打印迷宫(sizeX, sizeY, walls):
起始X = -300
起始Y = 300
高度 = (-起始X // sizeX) * 2
宽度 = (起始Y // sizeY) * 2
turtle.clear()
turtle.speed(0)
turtle.penup()
turtle.goto(-起始X, 起始Y)
turtle.pendown()
turtle.goto(-起始X, -起始Y)
turtle.goto(起始X, -起始Y)
turtle.setheading(0)
for i in range(sizeX):
turtle.penup()
turtle.goto(起始X, 起始Y - 高度 * i)
for l in range(sizeY):
if walls[i][l][1] == 1:
turtle.pendown()
else:
turtle.penup()
turtle.forward(宽度)
turtle.right(90)
for i in range(sizeY):
turtle.penup()
turtle.goto(起始X + 宽度 * i, 起始Y)
for l in range(sizeX):
if walls[l][i][0] == 1:
turtle.pendown()
else:
turtle.penup()
turtle.forward(高度)
屏幕.exitonclick() # 在函数之外的结尾调用
屏幕 = turtle.Screen() # 在函数之外的开头调用
# 创建绘制迷宫的函数
def 打印迷宫(sizeX, sizeY, walls):
起始X = -300
起始Y = 300
高度 = (-起始X // sizeX) * 2
宽度 = (起始Y // sizeY) * 2
turtle.clear()
turtle.speed(0)
turtle.penup()
turtle.goto(-起始X, 起始Y)
turtle.pendown()
turtle.goto(-起始X, -起始Y)
turtle.goto(起始X, -起始Y)
turtle.setheading(0)
for i in range(sizeX):
turtle.penup()
turtle.goto(起始X, 起始Y - 高度 * i)
for l in range(sizeY):
if walls[i][l][1] == 1:
turtle.pendown()
else:
turtle.penup()
turtle.forward(宽度)
turtle.right(90)
for i in range(sizeY):
turtle.penup()
turtle.goto(起始X + 宽度 * i, 起始Y)
for l in range(sizeX):
if walls[l][i][0] == 1:
turtle.pendown()
else:
turtle.penup()
turtle.forward(高度)
屏幕.exitonclick() # 在函数之外的结尾调用
turtle.TurtleScreen._RUNNING = True # 将 _RUNNING 变量手动设置为 true
英文:
Turtle uses a variable called _RUNNING
which becomes false when .exitonclick()
is called. So, you need to call turtle.Screen()
at the beginning and call .exitonclick()
at the end of the program. Both of them should be called outside the function. Modified code:
#Import necessary modules
import turtle
screen = turtle.Screen() #At beginning outside function
#Create the function to draw the maze
def printmaze(sizeX, sizeY, walls):
startX = -300
startY = 300
high = (-startX // sizeX) * 2
width = (startY // sizeY) * 2
turtle.clear()
turtle.speed(0)
turtle.penup()
turtle.goto(-startX, startY)
turtle.pendown()
turtle.goto(-startX, -startY)
turtle.goto(startX, -startY)
turtle.setheading(0)
for i in range(sizeX):
turtle.penup()
turtle.goto(startX, startY - high * i)
for l in range(sizeY):
if walls[i][l][1] == 1:
turtle.pendown()
else:
turtle.penup()
turtle.forward(width)
turtle.right(90)
for i in range(sizeY):
turtle.penup()
turtle.goto(startX + width * i, startY)
for l in range(sizeX):
if walls[l][i][0] == 1:
turtle.pendown()
else:
turtle.penup()
turtle.forward(high)
screen.exitonclick() #At the end outside the function
You need to call screen = turtle.Screen()
and .exitonclick()
only once in your program. There is no need to call them every time the function is called. So, you need to put them outside the function.
Edit:
If the above method doesn't work, then you can manually set the _RUNNING
variable to true after .exitonclick()
is called. Modified code:
#Import necessary modules
import turtle
screen = turtle.Screen() #At beginning outside function
#Create the function to draw the maze
def printmaze(sizeX, sizeY, walls):
startX = -300
startY = 300
high = (-startX // sizeX) * 2
width = (startY // sizeY) * 2
turtle.clear()
turtle.speed(0)
turtle.penup()
turtle.goto(-startX, startY)
turtle.pendown()
turtle.goto(-startX, -startY)
turtle.goto(startX, -startY)
turtle.setheading(0)
for i in range(sizeX):
turtle.penup()
turtle.goto(startX, startY - high * i)
for l in range(sizeY):
if walls[i][l][1] == 1:
turtle.pendown()
else:
turtle.penup()
turtle.forward(width)
turtle.right(90)
for i in range(sizeY):
turtle.penup()
turtle.goto(startX + width * i, startY)
for l in range(sizeX):
if walls[l][i][0] == 1:
turtle.pendown()
else:
turtle.penup()
turtle.forward(high)
screen.exitonclick() #At the end outside the function
turtle.TurtleScreen._RUNNING = True #Set the _RUNNING variable to true
答案2
得分: 0
你需要在开始时创建屏幕,并且exitonclick
需要在函数外部:
import turtle
screen = turtle.Screen()
def printmaze(sizeX, sizeY, walls):
startX = -300
startY = 300
high = (-startX // sizeX) * 2
width = (startY // sizeY) * 2
turtle.clear()
turtle.speed(0)
turtle.penup()
turtle.goto(-startX, startY)
turtle.pendown()
turtle.goto(-startX, -startY)
turtle.goto(startX, -startY)
turtle.setheading(0)
for i in range(sizeX):
turtle.penup()
turtle.goto(startX, startY - high * i)
for l in range(sizeY):
if walls[i][l][1] == 1:
turtle.pendown()
else:
turtle.penup()
turtle.forward(width)
turtle.right(90)
for i in range(sizeY):
turtle.penup()
turtle.goto(startX + width * i, startY)
for l in range(sizeX):
if walls[l][i][0] == 1:
turtle.pendown()
else:
turtle.penup()
turtle.forward(high)
screen.exitonclick()
英文:
You would have to create the Screen at the beginning and exitonclick needs to be outside the function:
import turtle
screen = turtle.Screen()
def printmaze(sizeX, sizeY, walls):
startX = -300
startY = 300
high = (-startX // sizeX) * 2
width = (startY // sizeY) * 2
turtle.clear()
turtle.speed(0)
turtle.penup()
turtle.goto(-startX, startY)
turtle.pendown()
turtle.goto(-startX, -startY)
turtle.goto(startX, -startY)
turtle.setheading(0)
for i in range(sizeX):
turtle.penup()
turtle.goto(startX, startY - high * i)
for l in range(sizeY):
if walls[i][l][1] == 1:
turtle.pendown()
else:
turtle.penup()
turtle.forward(width)
turtle.right(90)
for i in range(sizeY):
turtle.penup()
turtle.goto(startX + width * i, startY)
for l in range(sizeX):
if walls[l][i][0] == 1:
turtle.pendown()
else:
turtle.penup()
turtle.forward(high)
screen.exitonclick()
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论