我正在使用Python 3.5编写一个包含海龟绘图的简单程序,遇到了问题:在海龟绘图完成后,用户必须手动关闭窗口。
有没有办法在海龟绘图完成后程序自动关闭窗口?
我正在使用Python 3.5编写一个包含海龟绘图的简单程序,遇到了问题:在海龟绘图完成后,用户必须手动关闭窗口。
有没有办法在海龟绘图完成后程序自动关闭窗口?
turtle.bye()
, 也称为 turtle.Screen().bye()
,用于关闭海龟绘图窗口。
通常情况下,如果缺少 turtle.mainloop()
或其变体之一,则窗口将被关闭,因为程序将退出并关闭所有内容。除非脚本在禁用 turtle.mainloop()
和其变体的 Python IDLE -n 中运行,否则 turtle.mainloop()
应该是海龟绘图程序中执行的最后一个语句。
turtle.Screen().mainloop()
和 turtle.done()
是 turtle.mainloop()
的变体。
turtle.exitonclick()
,也称为 turtle.Screen().exitonclick()
,将屏幕点击事件绑定到执行 turtle.bye()
然后调用 turtle.mainloop()
。
tkinter.mainloop()
。import turtle
import tkinter as TK
t = turtle.Pen()
for x in range(100):
t.forward(x)
t.left(90)
TK.mainloop()
mainloop()
会保持窗口打开,而不是关闭它。将 mainloop()
留在文件末尾之外将允许窗口关闭。 - cdlaneexitonclick()
或者done()
来关闭窗口。