【问题标题】:Using pygame features in Tkinter在 Tkinter 中使用 pygame 功能
【发布时间】:2011-12-21 02:02:22
【问题描述】:

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

【问题讨论】:

    标签: user-interface tkinter pygame


    【解决方案1】:

    这适用于 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()
    

    【讨论】:

    • 刚刚在 windows 上试过这个,它似乎工作正常(没有你注释掉的代码行)
    猜你喜欢
    • 1970-01-01
    • 2018-04-30
    • 2021-02-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-12
    • 2021-07-18
    相关资源
    最近更新 更多