turtle.done() not working in Spyder - python

This simple Python script:
import turtle
bob = turtle.Turtle()
bob.forward(50)
turtle.done()
yields an error when running it using Spyder. The first run works as expected, but after that, the IPython console raises a "Terminator". The same happens when using turtle.exitonclick() instead of turtle.done(). What am I doing wrong?
Note that running it from the CLI works fine.

I don't use Spyder but have exchanged comments with others who have similar problems running turtle in similar environments.
If you close the turtle graphics window, it's dead and won't reopen for you. Short of restarting Spyder, you can try adding a turtle.bye() (which cleans up some things) after turtle.done() (which returns after the main loop exits). Then try running your program multiple times.

I was having trouble running a turtle graphics program more than once in Spyder, and I solved it by going to "Tools / Preferences". In the "Run" section, under "Console", select "Execute in a new dedicated Python console".

This is because the turtle module (most reference implementations as of today) uses a class variable called _RUNNING. Class variables remain false between executions when running in environments like Spyder instead of running as a self-contained Python script. There are two work around for this:
1)
import importlib
import turtle
importlib.reload(turtle)
bob = turtle.Turtle()
bob.forward(50)
turtle.done()
import turtle
turtle.TurtleScreen._RUNNING=True
bob = turtle.Turtle()
bob.forward(50)
turtle.done()

Related

Turtle window not opening with turtle.mainloop

I have the following code:
import turtle
my_pen = turtle.Turtle()
window = turtle.Screen()
window.setup(width=1000, height=1000)
window.title('Tutorial')
my_pen.color("red")
my_pen.penup()
my_pen.goto(0, 0)
window.delay(1000)
my_pen.pendown()
window.delay(100)
my_pen.forward(100)
my_pen.left(90)
my_pen.forward(100)
my_pen.left(90)
turtle.mainloop()
When I try to run the code, the window opens and closes immediately. I am using the newest python 3.9, and I am using the newest PyCharm Community
I will attach a video of me running it and the window closing immediately
Two things: first, I don't know that the delay() method is doing anything for you, it doesn't add anything in my environment, so I'd leave it out until you've debugged the rest of the code.
Second, your code works fine for me. So I suggest you stop looking at the code and look at the environment in which you're running it. You're not simply running Python at the console but rather using some sort of IDE (Idle?). If so, you should include that information in your question.
A simple code clean up for testing purposes:
from turtle import Screen, Turtle
window = Screen()
window.setup(width=1000, height=1000)
window.title('Tutorial')
my_pen = Turtle()
my_pen.color("red")
my_pen.forward(100)
my_pen.left(90)
my_pen.forward(100)
my_pen.left(90)
window.mainloop()

Re-open turtle after turtle.bye()

I have some code as follows:
# My code here
turtle.bye()
After that, is there any way I can reopen the turtle window.
I know you can do turtle.clearscreen() but that does not close the turtle window.
I will accept any answer which allows me to close the turtle graphics window and then reopen it without opening and running another python program to do this.
Thank you in advance
I've seen situations where the approach of #LukeTimmons worked but not always reliably and not in every situation. Give this solution a try:
import time
import turtle
turtle.dot(200, 'green')
time.sleep(2)
turtle.bye()
# These two lines (indirectly) resurrect turtle environment after turtle.bye()
turtle.Turtle._screen = None # force recreation of singleton Screen object
turtle.TurtleScreen._RUNNING = True # only set upon TurtleScreen() definition
turtle.dot(200, 'red')
turtle.mainloop()
It resets two flags that keep turtle from starting up again. It may be safer to create your own turtle after restart rather than use the default turtle which may point back to the departed environment.
There may be other ways but this is the only way I know.
from turtle import *
def turtle1():
#Your code here
turtle1()
turtle.bye()
turtle1()
This should re-run your code without re-typing it.

ImportError: No module named Tkinter when importing swampy.TurtleWorld

I am using Python 3.4 and following along the book "Think Python: how to think like a computer scientist". I actually figured out this issue a week ago, but saved over the original code when it failed to run like it did last week. Right now I have:
import tkinter
from swampy.TurtleWorld import *
which yields:
ImportError: No module named 'Tkinter'
When I had the code working last week, I loosely recall that in the 'import tkinter' line, there was a portion at the end that looked like this: [Tkinter]. I tried import tkinter as Tkinter but it doesn't work.
If I change it to Python2.7. And run
import Tkinter
from swampy.TurtleWorld import *
world = TurtleWorld()
bob = Turtle()
print (bob)
fd(bob, 100)
lt(bob)
fd(bob, 100)
wait_for_user()
The TurtleWorld window opens but there is no turtle. How can I get this to work again (Python 3.4 preferred)?
You're trying to run Python 2 code in Python 3 that loads Python 2 specific modules (Tkinter) and it's not going to work.
The default TurtleWorld package is Python 2, but there is an unsupported Python 3 version from the Green Tea Press' Swampy: Installation Instructions page. Go to the Python 3 section at the bottom. You will either need to install this package manually or just keep it in your working directory and import it from there. (The instructions explain this.)
Another alternative is to use the turtle module that comes with Python 3, as it's functionally similar for most turtle-related experiments. (I've answered TurtleWorld questions on SO using the Python turtle module.) For example:
from turtle import Turtle, Screen
bob = Turtle(shape="turtle")
print(bob)
bob.fd(100)
bob.lt(90)
bob.fd(100)
screen = Screen()
screen.exitonclick()

How to close the Python turtle window after it does its code?

I'm working on a simple program in Python 3.5 that contains turtle graphics
and I have a problem: after the turtle work is finished the user has to close the window manually.
Is there any way to program the window to close after the turtle work is done?
turtle.bye(), aka turtle.Screen().bye(), closes a turtle graphics window.
Usually, a lack of turtle.mainloop(), or one of its variants, will cause the window to close because the program will exit, closing everything. turtle.mainloop() should be the last statement executed in a turtle graphics program unless the script is run from within Python IDLE -n which disables turtle.mainloop() and variants.
turtle.Screen().mainloop() and turtle.done() are variants of turtle.mainloop().
turtle.exitonclick() aka turtle.Screen().exitonclick() binds the screen click event to do a turtle.bye() and then invokes turtle.mainloop()
Try exitonclick() or done() at the end of the file to close the window .
Add tkinter.mainloop()at the end of the file.
example
import turtle
import tkinter as TK
t = turtle.Pen()
for x in range(100):
t.forward(x)
t.left(90)
TK.mainloop()

python turtle graphics window won't open

I have little piece of code from a tutorial which should work fine but I don't get the turtle graphics window to show (I'm on Windows 10 using python 2.7.10). The code looks like this
import turtle
def draw_square():
window = turtle.Screen()
window.bgcolor("red")
brad = turtle.Turtle()
brad.forward(100)
window.exitonclick()
However, when I execute it nothing happens, I don't even get an error message. Instead, the shell just says
================================ RESTART ================================
and displays the little Windows circle (indicating it is working on something) but the turtle graphics window does not pop up.
I have tried repairing my python installation and additionally installing the x86 version, but I get the same outcome on the other installation, too.
Does anyone please know how to fix this?
Thank you,
Tomislav
Functions don't do anything unless you call them. Try:
import turtle
def draw_square():
window = turtle.Screen()
window.bgcolor("red")
brad = turtle.Turtle()
brad.forward(100)
window.exitonclick()
draw_square()

Categories