Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Turtle Graphics Not Responding

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)
like image 930
ElectroNerd Avatar asked Aug 27 '11 21:08

ElectroNerd


People also ask

How do I reset my turtle graphics?

turtle. reset() Does a turtle. clear() and then resets this turtle's state (i.e. direction, position, etc.) turtle.

Why turtle is not working in Python?

1 Answer. Please be informed that your main issue is that you've defined your program "turtle.py". #the first matching package named turtle that it finds is your program, "turtle.py". In other words, your program is essentially importing itself and not the turtle graphics module.

Are turtle graphics important?

Turtle graphics are a great way to introduce kids to coding. With short programs of just five to ten lines of code, kids can create and modify while learning.

Why are turtle graphics so popular?

Turtle can draw intricate shapes using programs that repeat simple moves. By combining together these and similar commands, intricate shapes and pictures can easily be drawn.


2 Answers

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

like image 112
aengelberg Avatar answered Sep 30 '22 10:09

aengelberg


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!

like image 24
arevirlegna Avatar answered Sep 30 '22 08:09

arevirlegna