Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/355.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/flutter/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 海龟:当我点击X按钮时,如何停止所有进程并关闭窗口?_Python_Python 3.7_Turtle Graphics - Fatal编程技术网

Python 海龟:当我点击X按钮时,如何停止所有进程并关闭窗口?

Python 海龟:当我点击X按钮时,如何停止所有进程并关闭窗口?,python,python-3.7,turtle-graphics,Python,Python 3.7,Turtle Graphics,我正在用Python3.7编写一个程序,在用户单击屏幕时绘制花朵,但我不知道如何检测用户是否在函数仍在执行时按下X按钮,然后停止所有进程并关闭窗口 我寻找解决方案,并尝试使用有关使用winfo_toplevel的答案,但我也无法实现 我的代码如下所示: import turtle, random from sys import exit window = turtle.Screen() window.setup(1000,500) #my functions to draw go here,

我正在用Python3.7编写一个程序,在用户单击屏幕时绘制花朵,但我不知道如何检测用户是否在函数仍在执行时按下X按钮,然后停止所有进程并关闭窗口

我寻找解决方案,并尝试使用有关使用
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:无法调用“销毁”命令:应用程序已销毁
然后

升高终止符
乌龟,终结者
然后

\u tkinter.TclError:无效的命令名“!canvas”

我还可以尝试什么?

在单击X按钮之前,您的代码正在破坏窗口。while循环条件错误。我认为你的代码应该不用这个。此外,turtle.bye()将为您关闭窗口,不会出现任何错误。这样做不需要destroy函数。请尝试按照下面的说明更改代码。它应该会起作用

       def stop():
           turtle.bye()
并在

          root.protocol("WM_DELETE_WINDOW", stop)
          # while root.protocol("WM_DELETE_WINDOW"):
          turtle.mainloop()

谢谢,这仍然给了我一个乌龟。终结者错误如果我关闭程序时,它仍然在绘图,我如何使它停止?