“turtle.Terminator” 出现在使用 turtle 时发生的错误。

huangapple go评论65阅读模式
英文:

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()

huangapple
  • 本文由 发表于 2023年5月13日 17:55:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/76242110.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定