Python Turtle Crashes When Opening Second Window - python

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

Related

are there any functions for closing turtle graphics's window?

so I want my program to do some lines of code then open turtle window do some staff, close it, do some code again and open turtle window. I have tried turtle.clear(), turtle.reset(), turtle.resetscreen(), turtle.clearscreen(), turtle.exitonclick(), but none of it clears entirely, close it and when I need it again, gives me ability to open it.
so can you help me.
import turtle, time
screen = turtle.Screen()
turtle.color('orange')
turtle.pensize(3)
turtle.circle(75)
time.sleep(5)
# close
screen.bye()
# or turtle.Screen().bye()

Python turtle terminator

I just wrote a program based on turtle module and after last line of code I used turtle.done() but when again I want to use my turtle I will get Terminator error and I should close and again open jupyter or even commond line to be able to run my code again please tell me what's wrong? Is it a bug inside turtle?
my code is something like this:
my_screen = turtle.Screen()
my_turtle= turtle.Turtle()
wm.setup(200, 300)
....
Here is my code(quite long!)
....
turtle.done()
The error occurs when a turtle program is interrupted during its main loop. If the way you close your turtle program is by clicking the X and there's a while loop that never ends, then the turtle.done() statement will never be met.
You can detect a turtle.Terminator error inside your while loop, so that instead of crashing the program, let the program continue to parse outside the while loop.
Something like this:
my_screen = turtle.Screen()
my_turtle= turtle.Turtle()
wm.setup(200, 300)
while True:
try:
# Your code for the while loop
except turtle.Terminator:
break
turtle.done
Or use ontimer().
The turtle module uses a class variable _RUNNING which remains true between executions when running in jupyter instead of running it as a self contained script. I have requested for the module to be updated with a pull request.
Meanwhile, work around/working example
1)
import importlib
import turtle
importlib.reload(turtle)
my_screen = turtle.Screen()
my_turtle= turtle.Turtle()
wm.setup(200, 300)
....
Your code
....
turtle.done()
import turtle
my_screen = turtle.Screen()
turtle.TurtleScreen._RUNNING=True
my_turtle= turtle.Turtle()
wm.setup(200, 300)
....
Your code
....
turtle.done()

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

Object oriented programming turtle (python)

I'm trying to get two turtles on a screen. So far I have this:
import turtle
t1 = turtle.Turtle()
t2 = turtle.Turtle()
t1.color("red")
t2.color("blue")
t1.forward(20)
t2.right(90)
t2.forward(100)
But nothing is happening :(
Im using the Turtle IDE downloaded from:http://pythonturtle.org/
All i want to do is illustrate some object orientation but if that code wont work, i cant
Anyone got any suggestions (ive tried using turtles API but its hard to follow)
Thanks
You didn't take an object from the class Screen
You should do that to make sure that the window will not close immediately, plus you should make the program loop.
import turtle
wn = turtle.Screen() # taking window object from Screen class
t1 = turtle.Turtle()
t2 = turtle.Turtle()
t1.color("red")
t2.color("blue")
t1.forward(20)
t2.right(90)
t2.forward(100)
while True: # Making the program loop to make the program continue running.
wn.update()
The main loop also you can make it this way
wn.mainloop()
I recommend you follow the first solution.

Categories