我正在创建一个游戏应用程序,它有一个菜单窗口和一个独立的游戏窗口。我试图让Tkinter窗口充当菜单,而pygame窗口则用于游戏。但是,如果游戏正在运行,我无法使用菜单(甚至无法关闭它)。如果我尝试关闭,就会收到致命错误消息:
Fatal Python error: PyEval_RestoreThread: the function must be called with the GIL held, but the GIL is released (the current Python thread state is NULL)
Python runtime state: initialized
Current thread 0x00002afc (most recent call first):
File "c:\Users\James\Documents\6th form\comp sci\NEA- A Level\code\gametst.py", line 12 in main
File "c:\Users\James\Documents\6th form\comp sci\NEA- A Level\code\Menu.py", line 15 in chess
File "C:\Users\James\AppData\Local\Programs\Python\Python39\lib\tkinter\__init__.py", line 1884 in __call__
File "C:\Users\James\AppData\Local\Programs\Python\Python39\lib\tkinter\__init__.py", line 1421 in mainloop
File "c:\Users\James\Documents\6th form\comp sci\NEA- A Level\code\Menu.py", line 22 in <module>
这是最小的可重现代码:
菜单:
from tkinter import *
import game
WIN = Tk()
WIN.geometry("200x200")
def f():
game.main()
button = Button(WIN, text="button", command=f)
button.pack()
WIN.mainloop()
游戏:
def main():
import pygame
pygame.init()
WIN = pygame.display.set_mode((200, 200))
run = 1
while run:
pygame.display.update()
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = 0
pygame.quit()
有没有方法可以让我同时使用菜单和游戏?谢谢任何帮助。
如果您知道更好的将游戏导入菜单的方法,那就太好了,但这并不是特别困扰我。
pygame
部分也应该足够。 - Matiissmain()
,就不会有菜单冻结的问题。在我看来,main()
的while
循环正在阻塞WIN.mainloop()
。实际上,这可能不太明显,但他可以尝试使用after()
循环来运行pygame,对吧? - Matiisswhile
循环阻塞了tkinter
,这也无法解释错误信息。这就是为什么我问是否在没有tkinter
的情况下运行main()
会产生相同的错误。此外,从回溯中可以看出,在一个c
模块(很可能是pygame
)中出现了问题,并且它是从main()
调用的。 - TheLizzard