python - 同时运行2个python窗口
问题描述
我正在创建一个游戏应用程序,它有一个菜单窗口,然后是一个单独的游戏窗口。我正在尝试让 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
在线程中运行该部分:
游戏.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
推荐阅读
- java - 当值为 nil 时将命名空间添加到 xml
- r - 根据条件用另一个数据框替换数据框列 - R
- javascript - 更改跨度文本值 JavaScript/jQuery
- haskell - Haskall Monad 任务
- sql - sqlite 允许 char(p) 输入超过长度 p
- javascript - 为单元测试生成测试数据 - JEST
- c++ - 用于处理多个模板类的 lambda 函数的“未定义引用”
- javascript - 可刷的水平条形图不起作用
- macos - Docker 容器不在 macOS 上运行
- django - 如何在 docker 启动时自动重建 Django 中 Elasticsearch 的搜索索引?