在 OS X 中运行 Python 3 Turtle 时出现奇怪的终结者错误

Bor*_*ell 5 turtle-graphics python-3.x ipython-notebook anaconda osx-elcapitan

当我运行一个海龟图形单元并绘制一些东西时,它没问题,但是如果我关闭窗口并再次运行该单元,我会收到一个名为 Terminator 的奇怪错误,我必须重新启动内核才能再次运行该单元. 如果我尝试连续运行 2 个不同的海龟图形单元(这 2 个单元绘制不同的内容并且编码良好),也会发生这种情况。如果我运行其中一个,然后我重新启动内核,然后运行另一个,不会发生错误,但是必须一直重新启动内核并不好,让我感到不安。这只会发生在我的新 macbook 上,我的 PC Windows 一切都很好,我可以连续重复运行海龟图形单元,唯一需要做的就是关闭当前的海龟窗口来运行另一个。

import turtle
window = turtle.Screen()
t = turtle.Turtle()

t.forward(50)
turtle.mainloop()
Run Code Online (Sandbox Code Playgroud)

如果我运行此代码一次就可以了。但是如果我关闭乌龟窗口并再次运行它而不重新启动内核,我会收到此错误:

--------------------------------------------------------------------------
Terminator                                Traceback (most recent call last)
<ipython-input-2-48bfc10d8dfd> in <module>()
      2 
      3 window = turtle.Screen()
----> 4 t = turtle.Turtle()
      5 
      6 t.forward(50)

/Users/marti/anaconda/lib/python3.5/turtle.py in __init__(self, shape,   undobuffersize, visible)
   3814                            shape=shape,
   3815                            undobuffersize=undobuffersize,
-> 3816                            visible=visible)
   3817 
   3818 Pen = Turtle

/Users/marti/anaconda/lib/python3.5/turtle.py in __init__(self, canvas,     shape, undobuffersize, visible)
   2555         self._undobuffersize = undobuffersize
   2556         self.undobuffer = Tbuffer(undobuffersize)
-> 2557         self._update()
   2558 
   2559     def reset(self):

/Users/marti/anaconda/lib/python3.5/turtle.py in _update(self)
   2658             return
   2659         elif screen._tracing == 1:
-> 2660             self._update_data()
   2661             self._drawturtle()
   2662             screen._update()                  #     TurtleScreenBase

/Users/marti/anaconda/lib/python3.5/turtle.py in _update_data(self)
   2644 
   2645     def _update_data(self):
-> 2646         self.screen._incrementudc()
   2647         if self.screen._updatecounter != 0:
   2648             return

/Users/marti/anaconda/lib/python3.5/turtle.py in _incrementudc(self)
   1290         if not TurtleScreen._RUNNING:
   1291             TurtleScreen._RUNNING = True
-> 1292             raise Terminator
   1293         if self._tracing > 0:
   1294             self._updatecounter += 1

Terminator: 
Run Code Online (Sandbox Code Playgroud)

我不知道为什么我会收到这个错误,并且我自己在互联网上发现了关于它的糟糕信息。如果我有不同的海龟细胞并且在另一个之前运行一个,也会发生同样的错误。我发现的唯一一件事是在 finder 上的 help() 命令倾翻海龟中。这就是我在这里找到的关于终结者的内容:

CLASSES
    builtins.Exception(builtins.BaseException)
        Terminator

class Terminator(builtins.Exception)
     |  Will be raised in TurtleScreen.update, if _RUNNING becomes False.
     |  
     |  This stops execution of a turtle graphics script.
     |  Main purpose: use in the Demo-Viewer turtle.Demo.py.
     |  
     |  Method resolution order:
     |      Terminator
     |      builtins.Exception
     |      builtins.BaseException
     |      builtins.object
     |  
     |  Data descriptors defined here:
     |  
     |  __weakref__
     |      list of weak references to the object (if defined)
     |  
Run Code Online (Sandbox Code Playgroud)

我是编程的新手,这个错误让我很不高兴。我希望有人能帮助我。也可能有助于解决以下问题:当我运行海龟然后以正确的方式关闭窗口时,例如使用 mainloop(),海龟图形窗口似乎已关闭,但实际上我一直在码头上看到它bar,就像它被最小化或者它已经在运行,然后当我运行另一个海龟图形窗口时,旧的以某种奇怪的方式保持打开的事实可能会影响新的,我得到这个终结者错误。

Pre*_*mbu 2

也许这些小江湖代码就可以了。模块导入代码之后。调用这些行turtle.clear()然后用turtle完成绘制后,转到下一个单元格并运行turtle.bye()

这是一个快速修复。

最好是在海龟完成绘制后在海龟窗口/画布上调用 exitonclick() 方法。例如

turtle_window = turtle.Screen() 
........Draw something 
turtle_window.exitonclick()
Run Code Online (Sandbox Code Playgroud)