20

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?

Dharman
  • 30,962
  • 25
  • 85
  • 135
st4tic
  • 369
  • 2
  • 3
  • 8

3 Answers3

24

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

cdlane
  • 40,441
  • 5
  • 32
  • 81
  • This does not help, next run ends with strange error: ` \AppData\Local\Programs\Python\Python39\lib\turtle.py in _update_data(self) 2645 2646 def _update_data(self): -> 2647 self.screen._incrementudc() 2648 if self.screen._updatecounter != 0: 2649 return ~\AppData\Local\Programs\Python\Python39\lib\turtle.py in _incrementudc(self) 1291 if not TurtleScreen._RUNNING: 1292 TurtleScreen._RUNNING = True -> 1293 raise Terminator ` – bmi May 30 '21 at 08:16
  • @bmi, if you want help with your issue, post a question to SO (not within a comment) including your (runnable) code and the error message. – cdlane May 30 '21 at 15:35
  • cdlane, I finally found a solution, it's not a shame to let others know that this solution doesn't always work. Don't be afraid of it. – bmi May 31 '21 at 19:23
-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()
J. Jaksche
  • 131
  • 9
emaestro
  • 1
  • 1
  • 1
    This code doesn't work as shown and the proposed solution is the opposite of what the OP is after -- `mainloop()` keeps the window open, it doesn't close it. Leaving the `mainloop()` off of the end of the file will allow the window to close. – cdlane Aug 27 '18 at 02:18
  • @clane at least you *can close the window* with this code. If you omit this code, then you can't even close the window manually. Thus, while not 100% on spot, it helps. –  Sep 19 '21 at 07:51
-2

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

Chandrashekhar Swami
  • 1,742
  • 23
  • 39
Victor O
  • 7
  • 3
  • 1
    Are there any other methods except those two? I'm running a code using IDLE, and it does not work out as I expected. – study Nov 14 '16 at 15:01
  • 1
    `turtle.done()` does not close the window, it's a synonym for `turtle.mainloop()`, which transfers control to the tkinter event loop which keeps the window open until some user action closes it! – cdlane Feb 16 '17 at 19:37