Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/338.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 为什么进程仍在后台运行?_Python_Tkinter_Pygame - Fatal编程技术网

Python 为什么进程仍在后台运行?

Python 为什么进程仍在后台运行?,python,tkinter,pygame,Python,Tkinter,Pygame,我制作了一个脚本,将pygame窗口嵌入到tkinter窗口中,但当我尝试退出时,它总是给我以下错误: pygame 1.9.6 Hello from the pygame community. https://www.pygame.org/contribute.html Traceback (most recent call last): File "C:\Users\Luca\Desktop\prova.py", line 39, in <module> root.update(

我制作了一个脚本,将pygame窗口嵌入到tkinter窗口中,但当我尝试退出时,它总是给我以下错误:

pygame 1.9.6
Hello from the pygame community. https://www.pygame.org/contribute.html
Traceback (most recent call last):
File "C:\Users\Luca\Desktop\prova.py", line 39, in <module>
root.update()
File "C:\Users\Luca\AppData\Local\Programs\Python\Python38-32\lib\tkinter\__init__.py", line 1305, in update
self.tk.call('update')
_tkinter.TclError: can't invoke "update" command: application has been destroyed

我也尝试过使用sys.exit(),但脚本仍在后台运行

您可以用函数替换while循环,并在之后使用
定期执行它:

def run():
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            return
    update()
    root.after(100, run)

run()
root.mainloop()
pygame.quit()

“仍在运行”:在
run=False
break
之后添加一个
break
将只中断for循环,而不是while循环。因此,仍将执行
root.update()
def run():
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            return
    update()
    root.after(100, run)

run()
root.mainloop()
pygame.quit()