【发布时间】:2020-04-13 06:17:58
【问题描述】:
我正在用 Python 3.7 编写一个程序,该程序在用户单击屏幕的位置绘制花朵,但我不知道如何检测用户是否在函数仍在执行时按下 X 按钮,然后停止所有进程并关闭窗口。
我寻找解决方案并尝试使用this 回答有关使用winfo_toplevel 的问题,但我也无法做到这一点。
我的代码如下所示:
import turtle, random
from sys import exit
window = turtle.Screen()
window.setup(1000,500)
#my functions to draw go here, but aren't included since the question is about closing the program
def stop():
turtle.bye()
root.destroy()
exit()
window.onclick(chooseFlower)
window.listen()
canvas = window.getcanvas()
root = canvas.winfo_toplevel()
root.protocol("WM_DELETE_WINDOW", stop)
while not root.protocol("WM_DELETE_WINDOW"):
turtle.mainloop()
我收到了一堆错误:
tkinter.TclError: can't invoke "destroy" command: application has been destroyed
然后
raise Terminator
turtle.Terminator
然后
_tkinter.TclError: invalid command name ".!canvas"
我还能尝试什么?
【问题讨论】:
标签: python python-3.7 turtle-graphics