Using pygame features in Tkinter

前端 未结 1 1991
孤城傲影
孤城傲影 2020-12-06 22:03

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

相关标签:
1条回答
  • 2020-12-06 22:34

    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()
    
    0 讨论(0)
提交回复
热议问题