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.

unor
  • 92,415
  • 26
  • 211
  • 360
Randoms
  • 2,110
  • 2
  • 20
  • 31

7 Answers7

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

ggorlen
  • 44,755
  • 7
  • 76
  • 106
5813
  • 1,073
  • 3
  • 14
  • 28
  • 1
    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. – javawizard Oct 09 '13 at 22:01
  • 1
    @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. – javawizard Jul 11 '20 at 23:07
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.

javawizard
  • 1,277
  • 1
  • 12
  • 17
3

Or you can try adding:

wn.exitonclick()

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

Mohammad Kanan
  • 4,452
  • 10
  • 23
  • 47
SaretMagnoslove
  • 301
  • 2
  • 4
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!

Anshika Singh
  • 994
  • 12
  • 20
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.

grooveplex
  • 2,492
  • 4
  • 28
  • 30
jay123
  • 55
  • 9
0

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

Ying
  • 2,660
  • 24
  • 23
  • 1
    Thanks, but this is just an alias of `mainloop()` which has already been provided as an answer. – ggorlen Sep 27 '22 at 20:36
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.

  • 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](/help/how-to-answer). – Community Apr 30 '22 at 02:33