在 Tkinter 中使用 pygame 功能 [英] Using pygame features in Tkinter

查看:117
本文介绍了在 Tkinter 中使用 pygame 功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我在 Tkinter 中制作的 GUI 中使用 pygame(精灵图形)的一些功能.我知道 OcempGUI,但我更愿意坚持使用 Tkinter,只需使用 pygame 中的一些模块.很相似,但不太一样.这可能吗?什么是潜在问题(事件循环)?

I'd like to use some features from pygame (sprite graphics) in my GUI I made in Tkinter. I know of OcempGUI, but I'd prefer to stick to Tkinter, just use some modules from pygame. This is similar but not quite the same. Is this possible at all? What are potential problems (event loop)?

推荐答案

这适用于 Linux.如果幸运的话,它可能也适用于其他操作系统.

This works on Linux. If you're lucky, it might work on other operating systems as well.

import Tkinter as tk
import os

w, h = 500, 200

# Add a couple widgets. We're going to put pygame in `embed`.
root = tk.Tk()
embed = tk.Frame(root, width=w, height=h)
embed.pack()
text = tk.Button(root, text='Blah.')
text.pack()

# Tell pygame's SDL window which window ID to use    
os.environ['SDL_WINDOWID'] = str(embed.winfo_id())

# The wxPython wiki says you might need the following line on Windows
# (http://wiki.wxpython.org/IntegratingPyGame).
#os.environ['SDL_VIDEODRIVER'] = 'windib'

# Show the window so it's assigned an ID.
root.update()

# Usual pygame initialization
import pygame as pg
pg.display.init()
screen = pg.display.set_mode((w,h))

pos = 0
while 1:
    # Do some pygame stuff
    screen.fill(pg.Color(0,0,0))
    pos = (pos + 1) % screen.get_width()
    pg.draw.circle(screen, pg.Color(255,255,255), (pos,100), 30)

    # Update the pygame display
    pg.display.flip()

    # Update the Tk display
    root.update()

这篇关于在 Tkinter 中使用 pygame 功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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