python turtle graphics window won't open - python

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()

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()

Python Turtle Crashes When Opening Second Window

I have a small program where I would like to pop up a Python turtle window, display something, close on user click, then pop up a next window. An example program for this is
import turtle
window = turtle.Screen()
t1 = turtle.Turtle()
t1.forward(100)
window.exitonclick()
window2 = turtle.Screen()
t2 = turtle.Turtle()
t2.forward(100)
window2.exitonclick()
The program crashes when trying to open the second window. This is with Python3.6 on a Mac OS X machine. I have seen some reference to crashes when re-running a program twice, and seen advice about adding a bye() call after the first window close, but I haven't managed to work around this yet.
Any suggestions?
It seems like you should probably using a different function then exitonclick to exit out of the screen or clear it. But I was able to get your code working by running turtle.bye() and doing a try/except around it in case of errors.
It's definitely not an elegant solution but is a workaround to get the second window to appear properly.
import turtle
window = turtle.Screen()
t1 = turtle.Turtle()
t1.forward(100)
window.exitonclick()
try:
turtle.bye()
except Exception:
pass
window2 = turtle.Screen()
t2 = turtle.Turtle()
t2.forward(100)
window2.exitonclick()

Invalid syntax for turtle name (python)

I am trying to write a program to draw a flower, but no matter what I do it keeps throwing an "invalid syntax" error for the turtle name. I have taken out all of my other code, tried naming the turtle something different, yet nothing works. Any ideas?
import turtle
def draw_flower():
window = turtle.Screen()
window.bgcolor(#42dff4)
sam = turtle.Turtle()
sam.forward(50)
window.exitonclick()
draw_flower()
Besides quoting the color string, as noted in the comments, your lines of code are in the wrong order. For example, generally nothing should follow window.exitonclick():
window.exitonclick()
draw_flower()
Make it (or window.mainloop()) the last statement of your program as that's when your code ends and the Tk event handler loop begins. I.e. reverse the order of these two statements. The second problem is that the variable window is in the wrong scope:
def draw_flower():
window = turtle.Screen()
...
window.exitonclick()
Since it's defined locally in draw_flower(), it's not available to use globally. Here's a rework of your code addressing both issues:
import turtle
def draw_flower():
sam = turtle.Turtle()
sam.forward(50)
window = turtle.Screen()
window.bgcolor("#42dff4")
draw_flower()
window.exitonclick()

turtle.done() not working in Spyder

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()

Python output window disappears immediately in eclipse after run

I use eclipse IDE and pydev plugins for python development. In eclipse when I run the following code snippet the program output window appears and disappears immediately. How to solve this when using ecliple (not using by terminal or command prompt)?
import turtle
wn = turtle.Screen()
alex = turtle.Turtle()
alex.forward(50)
alex.left(90)
alex.forward(30)
wn.mainloop()
That is because you get an error, it has nothing to do with eclipse:
AttributeError: '_Screen' object has no attribute 'mainloop'
If you want a continuous loop use a while loop or a for loop:
import turtle
while True:
wn = turtle.Screen()
alex = turtle.Turtle()
alex.forward(50)
alex.left(90)
alex.forward(30)
If you want to just see the keep the screen up use exitonclick()
wn.exitonclick()
the docs

Categories