Python:如何重置海龟图形窗口
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34033701/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Python: How to reset the turtle graphics window
提问by Allie Hart
I am a making a blackHyman 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?
我正在制作一个使用乌龟牌的二十一点游戏,每次我玩一只手乌龟时,只打印最后一场比赛而不是清除窗口。是否有一种方法可以在调用时关闭窗口,或者还有另一个为什么要这样做?
采纳答案by mmghu
With turtle, it is turtle.done()
.
对于乌龟,它是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.
如果您使用的graphics
是 python,我推荐John Zelle's Graphics Library。要关闭窗口,请使用window.close()
. graphics
如果您有兴趣,我已经链接了图书馆的文档。
回答by cdlane
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.mainloop()
akaturtle.Screen().mainloop()
将控制权交给 tkinter 的事件循环。通常,缺少turtle.Screen().mainloop()
(或turtle.Screen().exitonclick()
等)会导致窗口关闭,因为程序将结束,关闭所有内容。除非该脚本是从 Python IDLE -n 中运行的,否则该语句或其变体之一应该是海龟图形程序中的最后一条语句。
turtle.done()
(Does not close window nor reset anything.) A synonym for turtle.mainloop()
turtle.done()
(不关闭窗口也不重置任何东西。)的同义词 turtle.mainloop()
turtle.clear()
Deletes everything thisturtle has drawn (not just the last thing). Otherwise doesn't affect the state of the turtle.
turtle.clear()
删除这只海龟绘制的所有内容(不仅仅是最后一件事)。否则不会影响乌龟的状态。
turtle.reset()
Does a turtle.clear()
and then resets thisturtle's state (i.e. direction, position, etc.)
turtle.reset()
执行 aturtle.clear()
然后重置这个海龟的状态(即方向、位置等)
turtle.clearscreen()
aka turtle.Screen().clear()
Deletes alldrawing and allturtles, reseting the window to it's original state.
turtle.clearscreen()
akaturtle.Screen().clear()
删除所有绘图和所有海龟,将窗口重置为其原始状态。
turtle.resetscreen()
aka turtle.Screen().reset()
Resets allturtles on the screen to their initial state.
turtle.resetscreen()
akaturtle.Screen().reset()
将屏幕上的所有海龟重置为其初始状态。
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.bye()
akaturtle.Screen().bye()
关闭海龟图形窗口。在调用此命令后,我看不到使用任何海龟图形命令的方法。
turtle.exitonclick()
aka turtle.Screen().exitonclick()
After binding the screen click event to do a turtle.Screen().bye()
invokes turtle.Screen().mainloop()
turtle.exitonclick()
akaturtle.Screen().exitonclick()
绑定屏幕点击事件后进行turtle.Screen().bye()
调用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)
不清楚您是否可以从乌龟内部关闭并重新打开图形窗口,而无需下降到支持乌龟的 tkinter 级别(和 Zelle 的 graphics.py)
For purposes of starting a new hand in your blackHyman game, I'd guess turtle.reset()
or turtle.resetscreen()
are your best bet.
为了在您的二十一点游戏中开始新手,我猜turtle.reset()
或者turtle.resetscreen()
是您最好的选择。