st4tic
st4tic

Reputation: 409

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?

Upvotes: 24

Views: 63738

Answers (3)

emaestro
emaestro

Reputation: 1

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

Upvotes: -1

cdlane
cdlane

Reputation: 41872

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

Upvotes: 25

Victor O
Victor O

Reputation: 7

Try exitonclick() or done() at the end of the file to close the window .

Upvotes: -2

Related Questions