What's the best way to add a GUI to a Pygame application?
Are there any good GUIs that support Pygame surfaces as a widget within the application?
If this isn't possible or practical, what GUI toolkit has the best graphics component? I'm looking to keep the speedy renderin开发者_如何学Cg made possible by a SDL wrapper.
Dont use wxPython, its very hard to get to work well with Pygame, as described over at the GUI section of the Pygame wiki.
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.
Instead, opt for any of the libraries listed at the bottom of that page. I use pgu myself.
Take a look at Albow, PGU or Ocemp.
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.
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.)
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:
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.)
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.
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.
Only two of the five are actively maintained/developed: PGU and sgc, and sgc is new and incomplete.
That makes PGU the clear leader.
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.
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/
Here you have an explanation on how to integrate and make interact wxPython
and Pygame
.
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 work has been done with SDL as well.
精彩评论