问题
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
I have tried to switch notebooks, reinstalling jupyter and restarting the kernel, but none of them worked.
Here is the turtle code:
import turtle
pen = turtle.Pen()
window = turtle.Screen()
pen.color("light blue")
pen.shape("turtle")
pen.forward(100)
window.exitonclick()
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
回答1:
This is apparently a recurrent problem with Jupyter/iPython
You can install iPyTurtle that's should help a lot.
Found on this medium article
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
来源:https://stackoverflow.com/questions/56581957/turtle-does-not-run-more-than-once-in-jupyter-notebook