简体   繁体   中英

Turtle does not run more than once in jupyter notebook

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

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

I was having this problem too! Try:

$ pip install ipyturtle

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


In the setup.py file, in lines 74 & 75, change

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

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

to

os.path.join(here, 'js', 'lib', 'extension.js'),

os.path.join(here, 'js', 'lib', 'index.js')


$ pip install -e .

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

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


Should say 'Successfully installed ipyturtle' at the end

If you want to run the turtle module multiple times in Jupyter try turtle.bye() at the end:

import turtle

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

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

(I added try/except because .bye() raises an Terminator Error)

Based on turtle.done() not working in Spyder

ipyturtle seems to not work with JupyterLab and VSCode.

Try ipyturtle3.

With python3.6+:

python -m pip install ipyturtle3

Try the examples listed in this repo: https://github.com/williamnavaraj/ipyturtle3

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

I found this to work in JupyterLab and VSCode

ipyturtle seems to not work with JupyterLab or VS Code. Try ipyturtle3. With python3.6+:

python -m pip install ipyturtle3

Try the examples listed in this repo: https://github.com/williamnavaraj/ipyturtle3

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

I found this to work in JupyterLab and VSCode

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM