What gui toolkit should I use with Pygame? [closed

2019-02-06 12:27发布

I'm making a game that needs to be able to pop up gui elements within a pygame surface. This question isn't what I want because wxPython goes around the SDL surface, not inside it. So far I've only seen ocemp, pgu, and GooeyPy in this problem space.

  • Ocemp is huge and crufty looking. It mentions Python 2.3 and the newest file I found in a quick browse of the cvs repo was 2 years old.
  • I couldn't get GooeyPy to work (though I didn't try very hard; eggs and Debian are not friends) and v0.2 was last updated in February of 2009.
  • I've been working with pgu, but it appears to be unmaintained (last update 11/2009 and original maintainer abandoned it) and it's starting to show its age. It uses old style classes and throws string exceptions.

So my question to you, SO, is this: What gui toolkit should I use for making pretty clickable buttons pop up in your pygame applications? Are there any in active development?

Edit, September 2011

It looks like PGU is still being maintained. The last commits are from 4 days ago.

6条回答
相关推荐>>
2楼-- · 2019-02-06 13:05

Despite this being old, you really don't have to have a gui library for it, you just use sprites. For labels you would take a sprite and give it the font to render and such. For buttons it inherits the label and adds a active state to detect changes and cause something else to happen. And a scroller works the same as a button and just changes a label or something elses value

查看更多
趁早两清
3楼-- · 2019-02-06 13:07

See also the answers to: What's the best way to add a GUI to a Pygame application?

In particular, if someone would like to use DavesGUI, my pre-beta GUI toolkit for PyGame, send me an email. There's a link to my email address at the end of my reply, there.

查看更多
戒情不戒烟
4楼-- · 2019-02-06 13:09

I created a function that you can use inside of pygame without importing any extra libraries.

def gui(x1, y1, x2, y2, button):
    mouseX, mouseY = pygame.mouse.get_pos()
    if mouseX >= x1 and mouseX <= x2 and mouseY >= y1 and mouseY <= y2:
        if button:
            return True
        else:
            return False
    else:
        return False

You call upon the function like so(I am using a menu as an example).

topCornerX = #X coordinate of top right corner
topCornerY = #Y coordinate of top right corner
bottomCornerX = #X coordinate of bottom left corner
bottomCornerY = #Y coordinate of bottom left corner

while menu:
    for event in pygame.event.get():
        mouseLeft, mouseMiddle, mouseRight = pygame.mouse.get_pressed()
        if gui(topCornerX, topCornerY, bottomCornerX, bottomCornerY, '''mouseLeft, mouseMiddle, or mouseRight''')
            menu = False

Then you would continue on with your output.

This works by taking the coordinates of a rectangle and comparing your mouse postion to the inside of the rectangle and is True if they click the correct button and are inside the rectangle. Hope this helps.

查看更多
叛逆
5楼-- · 2019-02-06 13:11

I agree with milleja46 on not using a gui for somethings,

I found 2 simple menu popup programs here instead:

sophisticated menu: http://code.google.com/p/simple-pygame-menu/

simple popup window: How to specify what actually happens when Yes/No is clicked with ctypes MessageBoxW?

查看更多
狗以群分
6楼-- · 2019-02-06 13:12

I'm not aware of any pygame gui stuff, but it shouldn't be terribly hard to roll your own (and hey, maybe make it open source!)

If you're just doing a few simple buttons you could use GIMP or Photoshop or something else to make two (or three) images - an up, down and possible hover button, then you'd write your own event loop/handler that would do something like this:

  1. Get the mouse position
  2. Is the mouse over any of the buttons (and no buttons are pressed)?
  3. Display hover image
  4. Is the mouse over a button and a mouse button is clicked?
  5. Fire event associated with that button
  6. See 1

That's a bit simplified, but it should at least give you a starting point (if no one else has any pygame GUI libraries)

查看更多
祖国的老花朵
7楼-- · 2019-02-06 13:17
登录 后发表回答