在Tkinter中使用pygame功能(Using pygame features in Tkinter)

编程入门 行业动态 更新时间:2024-10-25 13:25:50
在Tkinter中使用pygame功能(Using pygame features in Tkinter)

我想在我用Tkinter制作的GUI中使用pygame(sprite graphics)中的一些功能。 我知道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。 如果幸运的话,它也可能适用于其他操作系统。

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()

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()

更多推荐

本文发布于:2023-08-06 18:43:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1454626.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:功能   Tkinter   pygame   features

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!