Python Turtle Graphics Window only Opens Briefly then Closes [duplicate]
Asked Answered
S

7

16

I have recently begun using the turtle module in Python, and I admit, I am a complete novice. I have been having trouble getting the graphics window in which the turtle does its drawing to stay open. Even when I try to run something as simple as this:

import turtle
wn = turtle.Screen()
tur = turtle.Turtle()
tur.forward(50)

all I get is the Python launcher icon to appear on my dock for a split second and close. Any help is appreciated, and I am, by the way, doing this in Aptana Studio 3.

Subsidy answered 26/9, 2013 at 2:16 Comment(0)
A
23

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

Alltime answered 3/10, 2013 at 1:17 Comment(2)
They're actually the exact same function (turtle just re-exports it from Tkinter), but you're right, this would save an unneeded import and as such is probably the better way to go.Complete
@Jas0n That depends - if you're just running a fixed set of drawing commands and want to see the result, then yes, you would place it at the end of your program.Complete
C
4

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.

Complete answered 27/9, 2013 at 2:48 Comment(0)
R
3

Or you can try adding:

wn.exitonclick()

Which will leave the graphics window open until you click on it.

Returnable answered 30/6, 2018 at 12:46 Comment(0)
E
2

When I enter the following code:

import turtle as t
t.fd(100)

The window containing the turtle graphics just appears and closes. But when I enter the following code:

import turtle as t
t.fd(100)
t.mainloop()

The window does not automatically disappear the way it did before.

Hence t.mainloop() or turtle.mainloop() depending on how you import the library, can be used to make the window stay open as long as you want.

Hope this was helpful!

Exhalation answered 8/7, 2020 at 12:26 Comment(1)
This was already explained in the accepted answer.Wayzgoose
V
1

I have the same problem. I can see the Turtle window very briefly, just a short flash, and then it's gone. To remedy, I just write input() at the end of my code. This will prevent the Turtle window from closing so one can see what is going on.

Also turtle.mainloop() is working for me.

Versicle answered 17/2, 2019 at 22:10 Comment(0)
B
0

Adding turtle.done() as the last statement in your turtle graphics program will keep the Window open.

Bucky answered 7/7, 2020 at 23:48 Comment(1)
Thanks, but this is just an alias of mainloop() which has already been provided as an answer.Sacristy
S
0

For all programmers who's still having the same issue, make sure that you have the latest version of python installed. In case you don't, download it and try to run your code again.

Sosthina answered 30/4, 2022 at 0:14 Comment(1)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Gawky

© 2022 - 2024 — McMap. All rights reserved.