【发布时间】:2016-04-29 15:46:27
【问题描述】:
当我运行一个海龟图形单元并绘制一些东西时没问题,但是如果我关闭窗口并再次运行该单元,我会收到一个名为 Terminator 的奇怪错误,我必须重新启动内核才能运行再次细胞。如果我尝试连续运行 2 个不同的海龟图形单元(这 2 个单元绘制不同的东西并且编码良好),我也会遇到这种情况。如果我运行其中一个,然后我重新启动内核,然后运行另一个,不会发生错误,但是必须一直重新启动内核并不好,并且让我感到不安。这只会发生在我的新 Macbook 上, 使用我的 PC Windows 一切都很好,我可以连续重复地运行海龟图形单元,我唯一需要做的就是关闭当前的海龟窗口以运行另一个。
import turtle
window = turtle.Screen()
t = turtle.Turtle()
t.forward(50)
turtle.mainloop()
如果我运行一次这段代码就可以了。但是如果我关闭乌龟窗口并再次运行它而不重新启动内核,我会得到这个错误:
--------------------------------------------------------------------------
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:
我不知道为什么我会收到此错误,并且我自己在互联网上发现了有关它的不良信息。如果我有不同的海龟细胞并在另一个之前运行,则会发生同样的错误。我发现的唯一东西是在查找器上的 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)
|
我在编程方面有点新手,这个错误让我很不安。我希望有人能帮助我。也可能有助于解决这个问题,即当我运行乌龟然后我以正确的方式关闭窗口时,比如使用 mainloop(),乌龟图形窗口似乎已关闭,但实际上我一直在码头上看到它栏,就像它是否已最小化或者它已经在运行,然后当我运行另一个海龟图形窗口时,旧的以某种奇怪的方式保持打开的事实可能会影响新的,我得到这个终结者错误。
【问题讨论】:
-
我没有使用 ipython、anaconda 或 osx 的经验,我不明白 you 所说的“turtle cell”或“restart the kernal”是什么意思。如果在命令行运行
python myturtle.py,关闭窗口,再次运行,应该没有问题。试试这个以确保。在hg.python.org/cpython/rev/1ae2382417dc 中,我修补了海龟文件以捕获由于海龟程序被外部演示程序停止的特殊情况而产生的终结者。可能是 OSX 上的 Anaconda 没有正确设置为运行海龟程序。尝试从 IDLE 运行。 -
感谢特里的回答。对于海龟单元,我的意思是 ipython 笔记本中的一个代码单元,我在其中编写海龟图形代码,使一个或多个海龟绘制一些东西。对于内核,我的意思是使 ipython notebook 工作的东西,程序的核心之类的东西。我尝试运行
python myturtle.py,但它说没有这样的文件或目录。 -
python.org 网站的东西我不知道如何管理它,似乎在 IDLE turtle 工作但我的目标是在 ipython notebook 上运行它,因为它更容易操作并学习它。也许你是对的,OS X 上的 anaconda 没有正确设置为运行海龟程序……或者我安装了 anaconda 或一些奇怪的东西。还是谢谢你!
-
使用 help() 打印的 Terminator 文档字符串有点令人困惑。我不知道 turtle 是否曾经是一个包,但它不在 2.7 或 3.x 中。在 3.x 中,“演示查看器”位于 turtledemo/__main__.py 中。查看器设置
_RUNNING = True并调用somedemo.main()。停止按钮设置_RUNNING = False。Terminator被somedemo或查看器本身捕获。我查看了海龟代码,我的假设是 Anaconda 以受监督的方式运行海龟,这样 turtle.py 不会从头开始重新读取,_RUNNING 不会重置为 True。 -
您必须将
myturtle.py替换为完整路径或更改为包含它的director。
标签: python-3.x ipython-notebook anaconda osx-elcapitan turtle-graphics