python 将 GUI 添加到 Pygame 应用程序的最佳方法是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2142912/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 23:53:24  来源:igfitidea点击:

What's the best way to add a GUI to a Pygame application?

pythonuser-interfacegraphicspygame

提问by Bibendum

Are there any good GUIs that support Pygame surfaces as a widget within the application?

是否有任何好的 GUI 支持 Pygame 表面作为应用程序中的小部件?

If this isn't possible or practical, what GUI toolkit has the best graphics component? I'm looking to keep the speedy rendering made possible by a SDL wrapper.

如果这不可能或不实用,哪个 GUI 工具包具有最好的图形组件?我希望通过 SDL 包装器保持快速渲染。

采纳答案by Kevin Shea

Hereyou have an explanation on how to integrate and make interact wxPythonand Pygame.

在这里,你对如何整合并互动的解释wxPythonPygame

Best GUI toolkit in my opinions is wxPython(a binding for wxWidgets) which has GUI widgets for practically everything including an OpenGL widget and some workhas been done with SDL as well.

在我看来,最好的 GUI 工具包是wxPython(wxWidgets 的绑定),它具有适用于几乎所有东西的 GUI 小部件,包括 OpenGL 小部件,并且还使用 SDL 完成了一些工作

回答by Mizipzor

Dont use wxPython, its very hard to get to work well with Pygame, as described over at the GUI sectionof the Pygame wiki.

不要使用 wxPython,它很难与 Pygame 一起工作,如Pygame wiki的GUI 部分所述。

First of all, pygame relies on the SDL, which means that it can only have one window at a time. Thus, trying to implement multiple Gtk, Qt, ... application instances that use pygame, is an impossibility. The second problematic reason is that those toolkits use their own main loop, which possibly forces you to pipe their events to your pygame instance and vice versa. And to mention some other points in short: Drawing the toolkit elements on the pygame window is impossible and the SDL/pygame fullscreen mode will be problematic.

首先,pygame 依赖于 SDL,这意味着它一次只能有一个窗口。因此,尝试实现多个使用 pygame 的 Gtk、Qt、...应用程序实例是不可能的。第二个有问题的原因是这些工具包使用它们自己的主循环,这可能会迫使您将它们的事件通过管道传输到您的 pygame 实例,反之亦然。简而言之,其他几点:在 pygame 窗口上绘制工具包元素是不可能的,SDL/pygame 全屏模式将有问题。

Instead, opt for any of the libraries listed at the bottom of that page. I use pgumyself.

相反,选择该页面底部列出的任何库。我自己用pgu

回答by Brandon

Take a look at Albow, PGUor Ocemp.

看看AlbowPGUOcemp

回答by Dave Burton

Albow and Ocemp are not being actively maintained. PGU is now being maintained by Peter Rogers, and Victor Kam has converted most of it to Python 3.

Albow 和 Ocemp 没有得到积极维护。PGU 现在由 Peter Rogers 维护,Victor Kam 已将其大部分转换为 Python 3。

There are 5 pygame GUI toolkits which I've tried to get running under Python 3: Albow, GooeyPy, PGU, pqGUI, and sgc. (I didn't get GooeyPy to work with Python 3, but the others did.)

我尝试在 Python 3 下运行 5 个 pygame GUI 工具包:Albow、GooeyPy、PGU、pqGUI 和 sgc。(我没有让 GooeyPy 与 Python 3 一起工作,但其他人做到了。)

I ran each of them through a simple Lines-Of-Code counter http://code.activestate.com/recipes/527746-line-of-code-counter/to gauge their sizes:

我通过一个简单的 Lines-Of-Code 计数器 http://code.activestate.com/recipes/527746-line-of-code-counter/来测量它们的大小:

These are the results:

这些是结果:

Albow code        min=2810, max=4551    (max = 162% of min)
Albow\demo        min= 453, max= 649

GooeyPy\gooeypy   min=2034, max=3941    (max = 194% of min)
GooeyPy\examples  min= 178, max= 351

pgu\pgu           min=2910, max=7047    (max = 242% of min)
incl. pgu\pgu\gui min=1678, max=4638    (max = 276% of min)
pgu\examples      min= 822, max=2527

pqGUI.py          min=1586, max=1834    (max = 116% of min)
Example.py        min= 178, max= 225

sgc (incomplete)  min= 889, max=1243    (max = 140% of min)

(I wanted to also try poutine, by Shandy Brown, but I couldn't find it.)

(我也想尝尝 Shandy Brown 的 poutine,但我找不到。)

In each case, the "min" number is more representative of the "size" of the toolkit, since it doesn't count whitespace and comments.

在每种情况下,“最小”数字更能代表工具包的“大小”,因为它不计算空格和注释。

When the "max" number is very close to the min number, it means that there's not much whitespace and not many comments in the code. pqGUI exemplifies that, because it has almost no comments at all, which is too bad, because (IMO) it makes the nicest-looking widgets.

当“最大”数字非常接近最小数字时,这意味着代码中没有太多空格和注释。pqGUI 举例说明了这一点,因为它几乎没有任何注释,这太糟糕了,因为(IMO)它制作了最漂亮的小部件。

I liked pqGUI because I like the look of the widgets it makes, but it is unsupported and undocumented, and I can't find the author.

我喜欢 pqGUI,因为我喜欢它制作的小部件的外观,但它不受支持和无证,而且我找不到作者。

Only two of the five are actively maintained/developed: PGU and sgc, and sgc is new and incomplete.

五个中只有两个被积极维护/开发:PGU 和 sgc,而 sgc 是新的和不完整的。

That makes PGU the clear leader.

这使 PGU 成为明确的领导者。

Dave

戴夫



P.S. (10/23/2012) -- I've created a GUI toolkit of my own for PyGame. It is widget-based, and uses pygame events for communication, so that it can easily be dropped into an existing pygame program, without taking over the event loop. It supports forms, buttons, windows, modal & non-modal message boxes & dialog boxes, vertical menus, text-entry boxes, and sliders (scroll bars). It smoothly handles overlapping controls, and forms-within forms.

PS (10/23/2012) -- 我已经为 PyGame 创建了我自己的 GUI 工具包。它是基于小部件的,并使用 pygame 事件进行通信,因此可以轻松地将其放入现有的 pygame 程序中,而无需接管事件循环。它支持表单、按钮、窗口、模态和非模态消息框和对话框、垂直菜单、文本输入框和滑块(滚动条)。它可以平滑地处理重叠控件和表单中的表单。

However, it's not really complete: it lacks some controls you're likely to want, like file-open dialogs and tables. The controls that do exist look nice, but features like title-bars & scroll bars are fixed numbers of pixels in width, rather than resizeable. The code is well-commented, and there's a demo app with usage examples, but there's no proper how-to-use documentation. And it's pre-beta, so everything is subject to change.

但是,它并不真正完整:它缺少一些您可能需要的控件,例如文件打开对话框和表格。确实存在的控件看起来不错,但标题栏和滚动条等功能的宽度是固定的像素数,而不是可调整大小。代码得到了很好的评论,并且有一个带有使用示例的演示应用程序,但没有正确的使用方法文档。而且它是预测试版,所以一切都可能发生变化。

If, in spite of those limitations, someone wants to try it out, then contact me by email. Ask about "DavesGUI." My email address is here: http://www.burtonsys.com/email/

尽管有这些限制,如果有人想尝试一下,请通过电子邮件与我联系。询问“DavesGUI”。我的电子邮件地址在这里:http: //www.burtonsys.com/email/