[英]Why do I get a Terminator error when I run this program?
Here is my code:这是我的代码:
import os
from turtle import *
sc = Screen()
sc.setup(600,600)
image = os.path.expanduser("~\OneDrive\Desktop\AlienGameImage.gif")
sc.register_shape(image)
t = Turtle()
t.shape(image)
sc.exitonclick()
Whenever I run this program, the first time it comes up with this error (Error below).每当我运行这个程序时,它第一次出现这个错误(下面的错误)。 However, the second time I run it, it runs fine.
但是,我第二次运行它时,它运行良好。 Error:
错误:
Traceback (most recent call last):
File "C:\Users\hulks\.spyder-py3\untitled0.py", line 14, in <module>
t = Turtle()
File "C:\Users\hulks\anaconda3\lib\turtle.py", line 3813, in __init__
RawTurtle.__init__(self, Turtle._screen,
File "C:\Users\hulks\anaconda3\lib\turtle.py", line 2557, in __init__
self._update()
File "C:\Users\hulks\anaconda3\lib\turtle.py", line 2660, in _update
self._update_data()
File "C:\Users\hulks\anaconda3\lib\turtle.py", line 2646, in _update_data
self.screen._incrementudc()
File "C:\Users\hulks\anaconda3\lib\turtle.py", line 1292, in _incrementudc
raise Terminator
Terminator
The error comes from here in turtle.py:错误来自turtle.py中的此处:
def _incrementudc(self):
"""Increment update counter."""
if not TurtleScreen._RUNNING:
TurtleScreen._RUNNING = True
raise Terminator
if self._tracing > 0:
self._updatecounter += 1
self._updatecounter %= self._tracing
I don't want to have to run this code twice every time so if anyone has a solution, I thank you in advance.我不想每次都运行这段代码两次,所以如果有人有解决方案,我提前谢谢你。
Don't call both:不要同时调用:
sc.exitonclick()
mainloop()
It's one or the other as exitonclick()
simply sets up the exit key event and calls mainloop()
它是其中之一,因为
exitonclick()
只是设置退出键事件并调用mainloop()
TurtleScreen._RUNNING
is False
when the program is executed for the first time, so it will enter the judgment formula and throw a Terminator
exception TurtleScreen._RUNNING
程序第一次执行时为False
,所以会进入判断公式,抛出Terminator
异常
TurtleScreen._RUNNING
is True
when the program is executed for the second time, skipping the judgment formula, so it executes smoothly. TurtleScreen._RUNNING
程序第二次执行时为True
,跳过判断公式,因此执行流畅。
Delete raise Terminator
, you can solve the problem.删除
raise Terminator
,即可解决问题。
I can see from your filepath that you are running it from spyder.我可以从您的文件路径中看到您正在从 spyder 运行它。 The turtle module uses a class variable TurtleScreen._RUNNING which remains false after a destroy between executions when running in spyder instead of running it as a self contained script.
Turtle 模块使用 class 变量 TurtleScreen._RUNNING 在 spyder 中运行而不是将其作为自包含脚本运行时,在执行之间的破坏后保持为假。 I have requested for the module to be updated.
我已要求更新模块。
Meanwhile, work around/working examples同时,解决/工作示例
1) 1)
import os
import importlib
import turtle
importlib.reload(turtle)
sc = Screen()
sc.setup(600,600)
image = os.path.expanduser(r"~\OneDrive\Desktop\AlienGameImage.gif")
sc.register_shape(image)
t = turtle.Turtle()
t.shape(image)
sc.exitonclick()
import os
import turtle
sc = Screen()
sc.setup(600,600)
image = os.path.expanduser(r"~\OneDrive\Desktop\AlienGameImage.gif")
sc.register_shape(image)
turtle.TurtleScreen._RUNNING=True
t = turtle.Turtle()
t.shape(image)
sc.exitonclick()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.