gpt4 book ai didi

python - Turtle 在 jupyter notebook 中不会运行多次

转载 作者:行者123 更新时间:2023-12-05 03:00:47 28 4
gpt4 key购买 nike

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

我试过换笔记本,重装jupyter,重启内核,都没用。

这是 turtle 代码:

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 中多次运行 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

关于python - Turtle 在 jupyter notebook 中不会运行多次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56581957/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com