使用PGU在Pygame中制作弹出窗口 [英] Making popup windows in pygame with pgu

查看:1167
本文介绍了使用PGU在Pygame中制作弹出窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将一些gui元素(带有按钮的对话框)添加到我用pygame编写的游戏中.我四处寻找一个不错的gui工具包,最后得到了 pgu .无论如何,我试图让它弹出一个对话框,它确实(有点),但是它没有关闭.

I'm trying to add some gui elements (dialog boxes with buttons) to a game I'm writing with pygame. I looked around for a decent gui toolkit and ended up with pgu. Anyway, I'm trying to get it to pop up a dialog box, which it does (sort of), but it isn't closing.

这是我的代码的简化版本,仅显示我关注的行为:

Here's a simplified version of my code that just shows the behavior I care about:


import pygame, sys
from pgu import gui

screen = None
WIDTH = 640
HEIGHT = 480

def init_pygame():
    global screen
    pygame.display.init()
    screen = pygame.display.set_mode((WIDTH, HEIGHT), pygame.DOUBLEBUF)
    pygame.display.set_caption('Testing PGU')

class SimpleDialog(gui.Dialog):
    def __init__(self):
        title = gui.Label("Spam")
        main = gui.Container(width=20, height=20)
        # I patched PGU to use new style classes.
        super(SimpleDialog, self).__init__(title, main, width=40, height=40)

    def close(self, *args, **kwargs):
        print "closing"
        return super(SimpleDialog, self).close(*args, **kwargs)

def run():
    init_pygame()
    app = gui.App()

    dialog = SimpleDialog()
    app.init(dialog)

    app.paint(screen)
    pygame.display.flip()
    while True:
        app.paint(screen)
        pygame.display.flip()
        for event in pygame.event.get():
            if event.type == pygame.MOUSEBUTTONDOWN:
                if event.button == 3: # right mouse button
                    print "opening"
                    dialog.open()
                else:
                    app.event(event)
            elif event.type == pygame.QUIT:
                sys.exit()
            else:
                app.event(event)

if __name__=='__main__':
    run()

我看到的行为:随即打开一个窗口,其中包含对话框的全屏版本.我什么都不会关闭它,尽管右键单击将在控制台上打印"opening",而单击红色小圆圈将使其打印"closes".对话框似乎使用了整个背景表面,而不是仅仅使用一个较小的背景表面.

The behavior I'm seeing: A window opens with a full screen version of the dialog box. Nothing I do will close it, though right clicking will print "opening" on my console and left clicking the little red circle will get it to print "closing". It looks like the dialog box is using the whole background surface instead of a smaller one just for itself.

我想看到的行为:出现一个大黑屏(稍后我将在其上进行绘制),当我右键单击它时,将打开一个小窗口.当我左键单击关闭按钮时,窗口消失了.

The behavior I'd like to see: A big black screen appears (I'll draw on it later) and when I right click on it, a little window opens up. When I left click the close button, the window goes away.

我怀疑这与我没有使用台式机有关,但是我不希望整个游戏都生活在gui中.

I suspect it has something to do with the fact that I'm not using a Desktop, but I don't want the whole game to live inside a gui.

现在,为了明确起见,我要问的问题是:如何修改我的代码,以从我看到的行为转变为我希望看到的行为?如果有人知道最近维护的内容比pgu更新,我愿意使用其他gui库.

Now, just to be explicit, the question: How do I modify my code to get from the behavior I'm seeing to the behavior I'd like to see? I'm open to using a different gui library if someone knows of something more recently maintained than pgu.

推荐答案

万一有其他人想这样做,我发现了一些可行的方法:创建一个空容器并在其上调用app.init().

In case anyone else ever wants to do this, I found something that works: Create an empty container and call app.init() on it.

empty = gui.Container(width=WIDTH, height=HEIGHT)
gui.init(empty)

这篇关于使用PGU在Pygame中制作弹出窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆