将 Pygame 窗口嵌入到 Tkinter 或 WxPython 框架中 [英] Embedding a Pygame window into a Tkinter or WxPython frame

查看:68
本文介绍了将 Pygame 窗口嵌入到 Tkinter 或 WxPython 框架中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我和一个朋友正在 pygame 中制作游戏.我们希望将 pygame 窗口嵌入到 tkinter 或 WxPython 框架中,以便我们可以包含 WX 或 Tkinter 支持的文本输入、按钮和下拉菜单.我已经在互联网上搜索了答案,但我发现的都是问同样问题的人,这些都没有得到很好的回答.

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.

实现嵌入 tkinter 或 WX 框架的 pygame 显示的最佳方法是什么?(TKinter 更可取)

What would be the best way implement a pygame display embedded into a tkinter or WX frame? (TKinter is preferable)

可以将这些功能与 pygame 显示一起包含的任何其他方式也可以使用.

Any other way in which these features can be included alongside a pygame display would also work.

推荐答案

根据 this SO question和公认的答案,最简单的方法是使用SDL绘图框架.

According to this SO question and the accepted answer, the simplest way to do this would be to use an SDL drawing frame.

此代码是 SO 用户 Alex Sallons 的作品.

This code is the work of SO user 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()      

此代码是跨平台的,只要在非 Windows 系统上省略 windb SDL_VIDEODRIVER 行即可.我建议

This code is cross-platform, as long as the windb SDL_VIDEODRIVER line is omitted on non Windows systems. I would suggest

# [...]
import platform
if platform.system == "Windows":
    os.environ['SDL_VIDEODRIVER'] = 'windib'
# [...]

这篇关于将 Pygame 窗口嵌入到 Tkinter 或 WxPython 框架中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆