简体   繁体   English

如何修复“turtle.terminator 错误?

[英]How do I fix the "turtle.terminator error?

I wanted to make a blinking "Press SPACE to start," screen and if SPACE was pressed.我想制作一个闪烁的“按空格键开始”屏幕,如果按下空格键。 the program should shut down but I always get the turtle.terminator error?该程序应该关闭,但我总是收到 turtle.terminator 错误? Can someone help me please?有谁可以帮助我吗?

from turtle import *
import pygame
while True:
    x = 0
    penup()
    tracer(0)
    if x == 0:
        color("black")
        goto(-80,-170)
        write("Press SPACE to start!",font=50)
        update()
        pygame.time.delay(1*800)
        x = 1
    if x == 1:
        color("white")
        goto(-90,-180)
        begin_fill()
        goto(90,-180)
        goto(90,-140)
        goto(-90,-140)
        goto(-90,-180)
        end_fill()
        update()
        x = 0
        pygame.time.delay(1*800)
    def close():
        bye()
    onkey(close(),"Space")
done()

After integrating advice from the comments, it still raises an error when I use onkey(close,...) instead of onkey(close(),..) .整合评论中的建议后,当我使用onkey(close,...)而不是onkey(close(),..)时,它仍然会引发错误

It is "space", not "Space".是“空间”,不是“空间”。 Also, it is still correct to use onkey(close, ...) .此外,使用onkey(close, ...)仍然是正确的。

The complete function would be onkey(close, "space")完整的功能将是onkey(close, "space")

There are a few issues here.这里有几个问题。 A word of advice: work in small bursts and run your code often to validate all of your assumptions at each step of the way.一句忠告:以小爆发的方式工作并经常运行您的代码以验证您在每个步骤中的所有假设。 This code looks as if it was all written in one fell swoop, and once it didn't work, there was too much complexity to isolate the bugs.这段代码看起来好像是一口气写完的,一旦不行,就太复杂了,无法隔离bug。 Had you run the code to test it often as you went along, you'd see those errors immediately and the solutions would be more obvious.如果您在进行过程中经常运行代码来测试它,您会立即看到这些错误,并且解决方案会更加明显。

Try to minimize the problem space by breaking your functionality into small pieces and validating each one.尝试通过将您的功能分成小块并验证每个小块来最小化问题空间。

First, let me provide my solution:首先,让我提供我的解决方案:

import turtle


def render_white_screen():
    win.ontimer(render_press_space, delay_ms)
    t.clear()
    turtle.update()


def render_press_space():
    win.ontimer(render_white_screen, delay_ms)
    t.write(
        "Press SPACE to start!",
        move=False,
        align="center",
        font=("Arial", 20, "normal"),
    )
    turtle.update()


delay_ms = 800
turtle.tracer(0)
win = turtle.Screen()
win.onkey(turtle.bye, "space")
win.listen()
t = turtle.Turtle()
t.hideturtle()
t.color("black")
render_press_space()
turtle.mainloop()

Turtle is surprisingly not especially beginner-friendly and has a huge list of gotchas , so it's important to check the docs and do research on Stack Overflow as soon as you encounter an error.令人惊讶的是,Turtle对初学者来说并不是特别友好,并且有大量陷阱,所以一旦遇到错误,检查文档并研究 Stack Overflow 是很重要的。

Errors:错误:

Suggestions/better practices:建议/更好的做法:

  • Always use import turtle , never from turtle import * .始终使用import turtle ,永远不要使用from turtle import * The reason is that from turtle import * dumps 160+ functions into the global namespace, causing potential clashes with your own functions.原因是from turtle import *将 160 多个函数转储到全局命名空间中,导致与您自己的函数发生潜在冲突。 Many of these functions have common names like update and reset that can easily cause confusion and bugs.这些函数中的许多函数都有像updatereset这样的通用名称,很容易引起混淆和错误。 Turtle has a module-level instance ostensibly to avoid confusion for beginners, but it only winds up introducing more problems in the long run than simply creating a turtle instance. Turtle 有一个模块级实例,表面上是为了避免初学者混淆,但从长远来看,它只会引入比简单地创建一个 turtle 实例更多的问题。
  • Don't import pygame just to sleep.不要只是为了睡觉而导入 pygame。 Python has a native function sleep available in the time module. Python 在time模块中有一个本地函数sleep可用。 But even better is to use turtle's ontimer callback as shown above.但更好的是使用 turtle 的ontimer回调,如上所示。
  • You can clear the screen with t.clear() .您可以使用t.clear()清除屏幕。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM