Python 为什么进程仍在后台运行?
我制作了一个脚本,将pygame窗口嵌入到tkinter窗口中,但当我尝试退出时,它总是给我以下错误: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 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()