将 Pygame 窗口嵌入到 Tkinter 或 WxPython 框架中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23319059/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Embedding a Pygame window into a Tkinter or WxPython frame
提问by AHuman
A friend and I are making a game in pygame. We would like to have a pygame window embedded into a tkinter or WxPython frame, so that we can include text input, buttons, and dropdown menus that are supported by WX or Tkinter. I have scoured the internet for an answer, but all I have found are people asking the same question, none of these have been well answered.
我和一个朋友正在 pygame 中制作游戏。我们希望将 pygame 窗口嵌入到 tkinter 或 WxPython 框架中,以便我们可以包含 WX 或 Tkinter 支持的文本输入、按钮和下拉菜单。我已经在互联网上搜索了答案,但我发现的都是问同样问题的人,这些都没有得到很好的回答。
What would be the best way implement a pygame display embedded into a tkinter or WX frame? (TKinter is preferable)
实现嵌入到 tkinter 或 WX 框架中的 pygame 显示的最佳方法是什么?(TKinter 更可取)
Any other way in which these features can be included alongside a pygame display would also work.
可以将这些功能与 pygame 显示一起包含的任何其他方式也可以使用。
采纳答案by PythonNut
According to thisSO question and the accepted answer, the simplest way to do this would be to use an SDL drawing frame.
根据thisSO question和公认的答案,最简单的方法是使用SDL绘图框架。
This code is the work of SO user Alex Sallons.
此代码是 SO 用户Alex Sallons 的作品。
import pygame
import Tkinter as tk
from Tkinter import *
import os
root = tk.Tk()
embed = tk.Frame(root, width = 500, height = 500) #creates embed frame for pygame window
embed.grid(columnspan = (600), rowspan = 500) # Adds grid
embed.pack(side = LEFT) #packs window to the left
buttonwin = tk.Frame(root, width = 75, height = 500)
buttonwin.pack(side = LEFT)
os.environ['SDL_WINDOWID'] = str(embed.winfo_id())
os.environ['SDL_VIDEODRIVER'] = 'windib'
screen = pygame.display.set_mode((500,500))
screen.fill(pygame.Color(255,255,255))
pygame.display.init()
pygame.display.update()
def draw():
pygame.draw.circle(screen, (0,0,0), (250,250), 125)
pygame.display.update()
button1 = Button(buttonwin,text = 'Draw', command=draw)
button1.pack(side=LEFT)
root.update()
while True:
pygame.display.update()
root.update()
This code is cross-platform, as long as the windb
SDL_VIDEODRIVER line is omitted on non Windows systems. I would suggest
此代码是跨平台的,只要windb
在非 Windows 系统上省略 SDL_VIDEODRIVER 行即可。我会建议
# [...]
import platform
if platform.system == "Windows":
os.environ['SDL_VIDEODRIVER'] = 'windib'
# [...]
回答by PythonNut
Here are some links.
这里有一些链接。
- For embedding in WxPython An Article on
pygame.org
- For Embedding in WxPython An Article on the
WxPython wiki
- For embedding in Tkinter see this SO question
- 对于嵌入 WxPython的文章
pygame.org
- 关于嵌入 WxPython 的一篇文章
WxPython wiki
- 要嵌入 Tkinter,请参阅此 SO 问题
Basically, there are many approaches.
基本上,有很多方法。
- On Linux, you can easily
embed
any application in a frame inside another. Simple. - Direct Pygame output to a WkPython Canvas
- 在 Linux 上,您可以轻松地将
embed
任何应用程序放在另一个框架内。简单的。 - 将 Pygame 输出直接输出到 WkPython Canvas
Some research will provide the relevant code.
一些研究将提供相关代码。
回答by trevorKirkby
According to the tracebacks, the program crashes due to TclErrors. These are caused by attempting to access the same file, socket, or similar resource in two different threads at the same time. In this case, I believe it is a conflict of screen resources within threads. However, this is not, in fact, due to an internal issue that arises with two gui programs that are meant to function autonomously. The errors are a product of a separate thread calling root.update() when it doesn't need to because the main thread has taken over. This is stopped simply by making the thread call root.update() only when the main thread is not doing so.
根据回溯,程序因 TclErrors 而崩溃。这些是由于尝试在两个不同的线程中同时访问相同的文件、套接字或类似资源造成的。在这种情况下,我认为这是线程内屏幕资源的冲突。然而,这实际上并不是由于两个旨在自主运行的 gui 程序出现的内部问题。这些错误是由于主线程已接管而不需要时调用 root.update() 的单独线程的产物。仅当主线程未执行此操作时,才通过使线程调用 root.update() 来停止此操作。