问题
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?
回答1:
With turtle, it is turtle.done()
.
If you are using graphics
for python I recommend John Zelle's Graphics Library. To close a window use window.close()
. I have linked the documentation for the graphics
library in case you are interested.
回答2:
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.
来源:https://stackoverflow.com/questions/34033701/python-how-to-reset-the-turtle-graphics-window