19

I am creating diagrams with the turtle package in Python, and it is successful to some extent, except for one problem. Once turtle generates the diagram that I have in code, it causes the program to say "Not responding" and eventually I have to end the task. I am using Windows 7.

Have any of you experienced this or know the root cause? I tried reinstalling Python completely, but that didn't seem to affect the problem.

Here is some example code that will make it fail to respond:

import turtle
from turtle import forward, right, left

forward(50)
Alec
  • 8,529
  • 8
  • 37
  • 63
ElectroNerd
  • 375
  • 1
  • 6
  • 18
  • 1
    Better still, try to find a small but complete subset of your code that causes the same problem. – Tom Zych Aug 27 '11 at 21:28
  • If I do any command, it causes turtle to not respond. I added some code in my original post. – ElectroNerd Aug 27 '11 at 21:47
  • Are you running it out of IDLE? – Mark Aug 27 '11 at 21:56
  • Oh I see what you mean, yes, I am running it through IDLE. I click "Edit it with IDLE" and then I press F5 to run the script. – ElectroNerd Aug 27 '11 at 22:04
  • I ran it through the command line and it worked fine. What is wrong with IDLE? – ElectroNerd Aug 27 '11 at 22:05
  • IDLE and turtle are both tk programs (so one tk program launching another), probably some strange glitch on Win7. I tried it under Linux and it works as expected. – Mark Aug 27 '11 at 22:11
  • I suppose I can run it under Linux, but that is a pity it doesn't work on Windows 7. – ElectroNerd Aug 27 '11 at 22:20
  • I think Mark was saying that the problem might be due to two tk programs running at once, so if you run it from a batch file or DOS window, maybe it'll work. – Tom Zych Aug 27 '11 at 23:42
  • I got it working from the Python command line, in addition to IDLE on Ubuntu. eryksun, how do I run mainloop() ? – ElectroNerd Aug 28 '11 at 00:26

12 Answers12

24

I had the same problem (I was on Win 7 as well, and I then got the same problem on Win XP), and I just figured it out.

You have to say turtle.done() when you're done.

Now that I know this, it makes more sense, because since Python doesn't know that the turtle is done, it's probably waiting for another command for the turtle.

Here's the documentation (in Python 2.7) of what library I assume you're using. It's how I figured that out. It says Python 2.7 but this also works for Python 2.5.
http://docs.python.org/library/turtle.html

Hope that helps (for you or anyone else reading this),
Alex

Alec
  • 8,529
  • 8
  • 37
  • 63
aengelberg
  • 386
  • 3
  • 3
  • Wow, I never knew that. I thought I had to go make a screen variable and make it mainloop or something at the end, which I am both not really good at and too lazy to do. This is a one-line piece of code that effectively solves the problem. How helpful! – Kevin May 13 '14 at 22:45
  • Thanks, I was facing the same issue. – Pj_ Aug 05 '14 at 20:29
10

Just add a call to exitonclick at the end. The Turtle class is implemented using Tkinter and exitonclick() invokes mainloop() which will keep the turtle window open until you click anywhere in the canvas. So, a simple program looks like this:

from turtle import *
#make a square
for _ in range(4):
   forward(100)
   left(90)
exitonclick()

Enjoy!

Alec
  • 8,529
  • 8
  • 37
  • 63
arevirlegna
  • 101
  • 1
  • 3
5

I'm using python 3.6.0 and ran into the same issue. The turtle.done() after your code block prevents the turtle graphic window from becoming unresponsive.

import turtle 

for _ in range(5):
    turtle.forward(100)
    turtle.right(360/5)

turtle.done() # <------------
Alec
  • 8,529
  • 8
  • 37
  • 63
Joseph Welt
  • 51
  • 1
  • 1
2

Add a mainloop() or exitonclick() or done() or something that shows python that you want to exit the turtle window

Alec
  • 8,529
  • 8
  • 37
  • 63
1

I thought the trick to closing the turtle program was to rename the 'turtle' module and store it as a new object, then call the .done() method, or .exitonclick() method, for example:

from turtle import *
import turtle
#make a square
for i in range(4):
   turtle.forward(100)
   turtle.left(90)
   turtle.forward(100)
   turtle.left(90)
   turtle.forward(100)
   turtle.left(90)
   turtle.forward(100)
   turtle.done()

Or alternatively, something like:

from turtle import *
import turtle as t
#draw a right angle
for i in range(4):
   t.forward(100)
   t.left(90)
   t.forward(100)
   t.exitonclick()

Simply importing the 'turtle' module on its own will not work. Use the 'from turtle import *' wildcard. Then you'll be able to use the call functions such as forward(), or left() without having to use any prefix or 'object_name.forward()' for example. So long as you finish the code with a simple done() or exitonclick() command, it works:

from turtle import *

forward(100)
shape('turtle')
right(90)
forward(100)
right(90)
forward(100)
right(90)
forward(100)
done()
1

I tested lots of things for this problem myself as I faced same problem.

So what I found after searching many resources was this:

NB. This problem happens for windows users when the click on the turtle graphic window.

First, I found spyder environment very useful

Second, this line works just fine:

Turtle.bye()

this would end the execution without any need of clicking.

Of course anyone needs to see what is happening on the window, so I suggest adding this:

import time
import turtle

<your code on turtle>

time.sleep(5)
Turtle.bye() 

remember to write Turtle with capital t.

Nill
  • 9
  • 1
1

It has some problem with IDLE. It will work if run from python command line

Alec
  • 8,529
  • 8
  • 37
  • 63
PyPy
  • 11
  • 1
0

I've come across your problem ever,and then I try to create a shortcut for IDLE as follow(not forget the " -n"):

target:D:\Python27\Lib\idlelib\idle.pyw -n

And launch the IDLE by the shortcut,type yr code and enjoy!FYI.

Alec
  • 8,529
  • 8
  • 37
  • 63
Chorola
  • 21
  • 1
0

I tried the code in my IDLE and it worked perfectly. Do you have an old/slower machine? Although I don't think that's the problem. Try adding a line at the end:

exitonclick()

Its probably just as turtle seem a bit temperamental. Also, If you have found an answer that helped or solved your problem, be sure to upvote and accept the answer (the arrow icon near the question), as the question otherwise displays as unsolved and you will continue getting answers.

-Harry

Alec
  • 8,529
  • 8
  • 37
  • 63
HarryCBurn
  • 755
  • 1
  • 8
  • 17
0

add the following line at the end of your code :

wait_for_user()

That should solve your problem!

Alec
  • 8,529
  • 8
  • 37
  • 63
Sahba
  • 9
  • 1
  • That's not a turtle.py, nor Python, nor tkinter command. It looks like it's a Swampy TurtleWorld command which isn't applicable here. – cdlane Feb 27 '17 at 06:14
0

There might be many reasons for it :- 1.You might have a python file named turtle(if you have one rename it) 2.try by using t.done() at the end of your code

ve.py
  • 1
  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jan 07 '22 at 18:13
-1

when using turtle.done(), the first time will work, but the second time not.

to solve this:

turtle.done()
try:
    turtle.bye()   
except turtle.Terminator:
    pass

from here Problems with running turtle programs in Spyder