为什么运行此程序时会出现终止符错误?

2024-09-30 20:30:41 发布

您现在位置:Python中文网/ 问答频道 /正文

这是我的密码:

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

每当我运行这个程序时,它第一次出现这个错误(下面的错误)。但是,我第二次运行它时,它运行良好。错误:

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

错误来自于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

我不想每次都要运行这段代码两次,所以如果有人有解决方案,我提前向您表示感谢


Tags: inpyimageselflib错误lineupdate
2条回答

TurtleScreen._RUNNING是程序第一次执行时的False,因此它将进入判断公式并抛出Terminator异常

TurtleScreen._RUNNING是程序第二次执行时的True,跳过了判断公式,因此执行平稳

删除raise Terminator,您可以解决问题

不要两者都称之为:

sc.exitonclick()
mainloop()

它是一个或另一个,因为exitonclick()只需设置退出键事件并调用mainloop()

相关问题 更多 >