[英]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:错误:
turtle.onkey(some_func(), "some key")
is incorrect (assuming some_func
isn't a higher-order function) because you're calling some_func
rather than passing it as the handler. turtle.onkey(some_func(), "some key")
是不正确的(假设some_func
不是高阶函数),因为您正在调用some_func
而不是将其作为处理程序传递。 This should be turtle.onkey(some_func, "some key")
.这应该是turtle.onkey(some_func, "some key")
。 This happens to trigger the terminator error because terminator occurs when you run turtle code after exiting, which is what your handler does.这恰好触发了终止符错误,因为终止符在您退出后运行 turtle 代码时发生,这是您的处理程序所做的。 See Python Turtle.Terminator even after using exitonclick() and onkeypress( ) function in turtle module problem .即使在 turtle 模块问题中使用 exitonclick() 和 onkeypress() 函数后,请参阅Python Turtle.Terminator 。turtle.onkey(space_handler, "Space")
should be turtle.onkey(space_handler, "space")
(lowercase). turtle.onkey(space_handler, "Space")
应该是turtle.onkey(space_handler, "space")
(小写)。 See Couldn't get rid of (_tkinter.TclError: bad event type or keysym "UP") problem .请参阅无法摆脱 (_tkinter.TclError: bad event type or keysym "UP") 问题。turtle.listen()
to activate key handlers that you've registered.您必须调用turtle.listen()
来激活您已注册的密钥处理程序。 See Turtle.onkeypress not working (Python) .请参阅Turtle.onkeypress 不工作 (Python) 。Suggestions/better practices:建议/更好的做法:
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.这些函数中的许多函数都有像update
和reset
这样的通用名称,很容易引起混淆和错误。 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 实例更多的问题。sleep
available in the time
module. Python 在time
模块中有一个本地函数sleep
可用。 But even better is to use turtle's ontimer
callback as shown above.但更好的是使用 turtle 的ontimer
回调,如上所示。t.clear()
.您可以使用t.clear()
清除屏幕。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.