Python Turtle Graphics Window only Opens Briefly then Closes
Add:
import Tkinter
Tkinter.mainloop()
to the end of your script, and that'll fix it.
What's happening is that once you've created a screen and drawn to it, there's nothing to stop Python from immediately exiting. The call to Tkinter.mainloop synchronously processes events from Tkinter (the GUI toolkit on which Python's turtle library is built) until the screen window is closed.
Or you can try adding:
wn.exitonclick()
Which will leave the graphics window open until you click on it.
Also, you may want to try
turtle.mainloop()
which in my opinion just works slightly better than with Tk.
From the docs for turtle.mainloop()
:
Starts event loop - calling Tkinter’s mainloop function. Must be the last statement in a turtle graphics program. Must not be used if a script is run from within IDLE in -n mode (No subprocess) - for interactive use of turtle graphics.
turtle.done()
is an alias of turtle.mainloop()
.