gpt4 book ai didi

python - 同时运行 2 个 python 窗口

转载 作者:行者123 更新时间:2023-12-05 01:29:46 27 4
gpt4 key购买 nike

我正在创建一个游戏应用程序,它有一个菜单窗口,然后是一个单独的游戏窗口。我正在尝试让 Tkinter 窗口充当游戏的菜单和 pygame 窗口,但是如果游戏正在运行,我无法使用菜单(甚至无法关闭它)。如果我尝试,我会收到一条 fatal error 消息:

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 部分:

游戏.py

import pygame

def main():
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()
print("pygame done")

def terminate():
if pygame.get_init():
pygame.event.post(pygame.event.Event(pygame.QUIT))

请注意,我添加了 terminate() 函数来停止 pygame 循环。

菜单.py

from tkinter import *
import game

WIN = Tk()
WIN.geometry("200x200")

def f():
import threading
# execute the pygame stuff in a thread
threading.Thread(target=game.main).start()

button = Button(WIN, text="button", command=f)
button.pack()

WIN.mainloop()
game.terminate() # terminate pygame loop

关于python - 同时运行 2 个 python 窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67526713/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com