龟在jupyter笔记本中运行不止一次 [英] Turtle does not run more than once in jupyter notebook

查看:415
本文介绍了龟在jupyter笔记本中运行不止一次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在jupyter笔记本中运行一些海龟代码.当我运行一次代码时,代码运行良好.但是,如果我再次运行代码,Python Turtle Graphics将冻结并关闭(aka python turtle Graphics没有响应),并且jupyter笔记本的内核崩溃

I am trying to run some turtle code in jupyter notebook. When I run the code once, the code runs fine. However, if I run the code again, Python Turtle Graphics freezes and closes (aka python turtle graphics is not responding) and the jupyter notebook's kernel crashes

我试图切换笔记本电脑,重新安装jupyter并重新启动内核,但是它们都没有起作用.

I have tried to switch notebooks, reinstalling jupyter and restarting the kernel, but none of them worked.

这是海龟代码:

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":

Here is the error message when I run the code for the second time. The note book is saved at C:\Users\fligh\OneDrive\Jupyter Notebooks\ and the file name is "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:

有人可以帮助我吗? 谢谢

Can someone help me? Thanks

推荐答案

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

This is apparently a recurrent problem with Jupyter/iPython

您可以安装 iPyTurtle ,这应该会很有帮助.
在此中篇文章中找到

You can install iPyTurtle that's should help a lot.
Found on this medium article

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

You can also create a file containing your commands using %%file turtle_example.py in top of cell and in cell below, then run it using !python turtle_example.py.
But this is not really great

这篇关于龟在jupyter笔记本中运行不止一次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆