Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: How to reset the turtle graphics window

I am a making a blackjack game with cards using turtle and each time I play a hand turtle just prints over the last game instead of clearing the window. Is there a method that closes the window when it is called or is there another why of doing this?

like image 970
Allie Hart Avatar asked Dec 02 '15 02:12

Allie Hart


2 Answers

I want to clarify what various turtle functions do as there are misunderstandings in this discussion, including in the currently accepted answer, as the method names themselves can be confusing:


turtle.mainloop() aka turtle.Screen().mainloop() Turns control over to tkinter's event loop. Usually, a lack of turtle.Screen().mainloop() (or turtle.Screen().exitonclick(), etc.) will cause the window to close just because the program will end, closing everything. This, or one of its variants, should be the last statement in a turtle graphics program unless the script is run from within Python IDLE -n.

turtle.done() (Does not close window nor reset anything.) A synonym for turtle.mainloop()

turtle.clear() Deletes everything this turtle has drawn (not just the last thing). Otherwise doesn't affect the state of the turtle.

turtle.reset() Does a turtle.clear() and then resets this turtle's state (i.e. direction, position, etc.)

turtle.clearscreen() aka turtle.Screen().clear() Deletes all drawing and all turtles, reseting the window to it's original state.

turtle.resetscreen() aka turtle.Screen().reset() Resets all turtles on the screen to their initial state.

turtle.bye() aka turtle.Screen().bye() Closes the turtle graphics window. I don't see a way to use any turtle graphics commands after this is invoked.

turtle.exitonclick() aka turtle.Screen().exitonclick() After binding the screen click event to do a turtle.Screen().bye() invokes turtle.Screen().mainloop()


It's not clear that you can close and reopen the graphics window from within turtle without dropping down to the tkinter level that underpins turtle (and Zelle's graphics.py)

For purposes of starting a new hand in your blackjack game, I'd guess turtle.reset() or turtle.resetscreen() are your best bet.

like image 84
cdlane Avatar answered Oct 19 '22 20:10

cdlane


turtle.clear()

turtle.reset()

clears screen but turtle position does not change.

clears screen; turtle goes back to center

like image 1
Mazharul Haque Avatar answered Oct 19 '22 22:10

Mazharul Haque