【问题标题】:Turtle does not run more than once in jupyter notebookTurtle 在 jupyter notebook 中不会多次运行
【发布时间】:2019-10-28 03:13:09
【问题描述】:

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

我尝试过切换笔记本,重新安装 jupyter 并重新启动内核,但都没有成功。

这是海龟代码:

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:

有人可以帮助我吗? 谢谢

【问题讨论】:

    标签: python jupyter-notebook turtle-graphics


    【解决方案1】:

    这显然是 Jupyter/iPython 经常出现的问题

    您可以安装iPyTurtle,这应该会有很大帮助。
    发现在这个medium article

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

    【讨论】:

      【解决方案2】:

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

      $ pip install 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


      最后应该说“成功安装 ipyturtle”

      【讨论】:

        【解决方案3】:

        如果您想在 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

        【讨论】:

          【解决方案4】:

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

          python -m pip install ipyturtle3
          

          试试这个 repo 中列出的例子: https://github.com/williamnavaraj/ipyturtle3

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

          我发现这可以在 JupyterLab 和 VSCode 中使用

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2019-06-26
            • 2017-08-30
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2022-12-28
            • 2018-01-08
            相关资源
            最近更新 更多