在Tkinter中使用pygame功能

sig*_*980 3 user-interface pygame tkinter

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

Sno*_*all 10

这适用于Linux.如果你很幸运,它也可以在其他操作系统上运行.

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()
Run Code Online (Sandbox Code Playgroud)

  • 刚刚在Windows上尝试过,它似乎可以正常工作(没有您注释掉的代码行) (2认同)