Turtle 在 jupyter notebook 中不会 运行 多次

Turtle does not run more than once in jupyter notebook

我正在尝试 运行 jupyter notebook 中的一些 turtle 代码。当我 运行 代码一次时,代码 运行 没问题。但是,如果我再次 运行 代码,Python Turtle Graphics 冻结并关闭(又名 python turtle graphics 没有响应)并且 jupyter notebook 的内核崩溃

我尝试过更换笔记本,重新安装 jupyter 并重新启动内核,但 none 成功了。

海龟代码如下:

import turtle
pen = turtle.Pen()
window = turtle.Screen()
pen.color("light blue")
pen.shape("turtle")
pen.forward(100)

window.exitonclick()

这是我第二次运行代码时的错误信息。笔记本保存在C:\Users\fligh\OneDrive\Jupyter Notebooks\,文件名为"Principles 2 Playground":

<ipython-input-2-79042881c88e> in <module>
      1 import turtle
----> 2 pen = turtle.Pen()
      3 window = turtle.Screen()
      4 pen.color("light blue")
      5 pen.shape("turtle")

c:\users\fligh\appdata\local\programs\python\python37-32\lib\turtle.py in __init__(self, shape, undobuffersize, visible)
   3814                            shape=shape,
   3815                            undobuffersize=undobuffersize,
-> 3816                            visible=visible)
   3817 
   3818 Pen = Turtle

c:\users\fligh\appdata\local\programs\python\python37-32\lib\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):

c:\users\fligh\appdata\local\programs\python\python37-32\lib\turtle.py in _update(self)
   2658             return
   2659         elif screen._tracing == 1:
-> 2660             self._update_data()
   2661             self._drawturtle()
   2662             screen._update()                  # TurtleScreenBase

c:\users\fligh\appdata\local\programs\python\python37-32\lib\turtle.py in _update_data(self)
   2644 
   2645     def _update_data(self):
-> 2646         self.screen._incrementudc()
   2647         if self.screen._updatecounter != 0:
   2648             return

c:\users\fligh\appdata\local\programs\python\python37-32\lib\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:

有人可以帮助我吗? 谢谢

这显然是 Jupyter/iPython

的一个反复出现的问题

您可以安装 iPyTurtle 这应该会有很大帮助。
在此 medium article

上找到

您还可以在单​​元格顶部和下方单元格中使用 %%file turtle_example.py 创建一个包含您的命令的文件,然后使用 !python turtle_example.py.
运行 它 但这不是很好

我也遇到了这个问题!尝试:

$ pip 安装 ipyturtle

$ jupyter nbextension enable --py --sys-prefix ipyturtle


在 setup.py 文件的第 74 和 75 行中,更改

os.path.join(这里,'ipyturtle','static','extension.js'),

os.path.join(这里,'ipyturtle','static','index.js')

os.path.join(这里,'js','lib','extension.js'),

os.path.join(这里,'js','lib','index.js')


$ pip install -e .

$ jupyter nbextension install --py --symlink --sys-prefix ipyturtle

$ jupyter nbextension enable --py --sys-prefix ipyturtle


最后应该说'Successfully installed ipyturtle'

如果你想在 Jupyter 中多次 运行 turtle 模块,最后尝试 turtle.bye():

import turtle

bob = turtle.Turtle()
bob.forward(50)
turtle.done()

try:
    turtle.bye()
except:
    print("bye") 

(我添加了 try/except 因为 .bye() 会引发终止符错误)

基于turtle.done() not working in Spyder

ipyturtle 似乎不适用于 JupyterLab 或 VS Code。尝试 ipyturtle3。 使用 python3.6+:

python -m pip install ipyturtle3

尝试此存储库中列出的示例: https://github.com/williamnavaraj/ipyturtle3

https://pypi.org/project/ipyturtle3/

我发现它可以在 JupyterLab 和 VSCode

中使用