3

First Question

I was doing an example from ThinkPython 2e Chapter 4 Case Study: Interface Design, when I stumbled upon turtle module.

I use jupyter notebook as an IDE to do exercise. Here's the code

import turtle
bob = turtle.Turtle()

When I ran the code above, the code is exected but Python Turtle Graphic failed to run with 'Not Responding' status

The same goes for this, although when I ran the following, the kernal is busy for a while before stating an error

import turtle
bob = turtle.Turtle()
print(bob)
turtle.mainloop()

What caused this error?

Second Question

Then I tried the next code which is

bob.fd(100)

and it just froze so I have to interrupt the code, here's the error

---------------------------------------------------------------------------
KeyboardInterrupt                         Traceback (most recent call last)
<ipython-input-7-a3ce06f254ac> in <module>
      2 bob = turtle.Turtle()
      3 print(bob)
----> 4 turtle.mainloop()

~\Anaconda3\lib\turtle.py in mainloop()

~\Anaconda3\lib\turtle.py in mainloop(self)
    811 
    812         """
--> 813         TK.mainloop()
    814 
    815     def textinput(self, title, prompt):

~\Anaconda3\lib\tkinter\__init__.py in mainloop(n)
    558 def mainloop(n=0):
    559     """Run the main loop of Tcl."""
--> 560     _default_root.tk.mainloop(n)
    561 
    562 getint = int

KeyboardInterrupt: 

What I have tried

I've tried to refer to this thread Turtle does not run more than once in jupyter notebook
which eventually leads me back to here

https://medium.com/@jiyuanli93/how-to-make-python-turtle-works-in-jupyter-notebook-6c506b9a973f

or here

https://github.com/gkvoelkl/ipython-turtle-widget

a little information : when I run this on my Anaconda prompt

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

It generated some problems, but ran perfectly (it's said validating: ok) when I ran them as admin

any help would be appreciated, I've been stuck with this stuff for 2.5 hours

ordem
  • 130
  • 2
  • 9

1 Answers1

4

I had the same problem as you.

This worked for me.

Put the following code into three Jupyter Notebook cells:

Cell 1

import turtle
bob = turtle.Turtle()
print(bob)

Cell 2

for i in range(4):
    bob.fd(100)
    bob.lt(90)

Cell 3

turtle.mainloop()

And run it one by one.

I am a noob, like you, but what I think is happening is that the code turtle.mainloop() terminates the object bob, or something like that. What you should do is execute all the movements like bob.fd(100) before executing turtle.mainloop(). Does it make sense?