Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 255
st4tic Avatar asked Apr 24 '16 17:04

st4tic


People also ask

How do you close a turtle window in Python?

turtle. bye() , aka turtle. Screen(). bye() , closes a turtle graphics window.

How do I hide the turtle screen in Python?

hideturtle() to your turtle's name or to turtle directly and it will hide the turtle. This method can be used as this Python code sample: turtle. hideturtle()

How do you undo the turtle's last action?

undo() This function is used to undo (repeatedly) the last turtle action. The number of available undo actions is determined by the size of the undobuffer.


1 Answers

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

like image 122
cdlane Avatar answered Oct 29 '22 11:10

cdlane