您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

将Pygame窗口嵌入到Tkinter或WxPython框架中

将Pygame窗口嵌入到Tkinter或WxPython框架中

根据此SO问题和公认的答案,最简单的方法是使用SDL并条机。

代码是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()

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

# [...]
import platform
if platform.system == "Windows":
    os.environ['SDL_VIDEODRIVER'] = 'windib'
# [...]
python 2022/1/1 18:30:09 有327人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶