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

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

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

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

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.

My turtle program in python acts like it has been clicked

import turtle
window = turtle.Screen()
pen = turtle.Turtle()
def star():
pen.forward(100);
turtle.onscreenclick(star())
window.mainloop()
I am very new to turtle,and python itself, I am just using some of the documentation that I found on the python website, but when I run this program it runs my function star without the screen even being clicked. The end goal of this program would be for me to click then it would run the function, then if I clicked again the screen would clear and the function would be run again.
Thanks for the help!
You need to pass reference to start function into onscreenclick, and make start to accept two arguments:
def star(x,y):
pen.forward(100);
turtle.onscreenclick(star)

Categories