Python 使用pgu在pygame中创建弹出窗口

Python 使用pgu在pygame中创建弹出窗口,python,pygame,pgu,Python,Pygame,Pgu,我正在尝试向我用pygame编写的游戏中添加一些gui元素(带有按钮的对话框)。我四处寻找一个像样的gui工具包,最后得到了一个。不管怎样,我正试图让它弹出一个对话框,它确实(有点),但它并没有关闭 下面是我的代码的简化版本,它只显示了我关心的行为: import pygame, sys from pgu import gui screen = None WIDTH = 640 HEIGHT = 480 def init_pygame(): global screen py

我正在尝试向我用pygame编写的游戏中添加一些gui元素(带有按钮的对话框)。我四处寻找一个像样的gui工具包,最后得到了一个。不管怎样,我正试图让它弹出一个对话框,它确实(有点),但它并没有关闭

下面是我的代码的简化版本,它只显示了我关心的行为:


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()
我看到的行为是:打开一个窗口,显示一个全屏版本的对话框。我所做的一切都不会关闭它,尽管右键单击将在我的控制台上打印“打开”,左键单击红色小圆圈将打印“关闭”。看起来对话框使用的是整个背景表面,而不是仅用于自身的较小背景表面

我想看到的行为是:一个大黑屏出现(我稍后会在上面画),当我右键点击它时,一个小窗口打开。当我左键单击关闭按钮时,窗口消失了

我怀疑这与我没有使用桌面有关,但我不希望整个游戏都在gui中运行


现在,明确地说,问题是:如何修改代码,使其从我看到的行为变成我希望看到的行为?如果有人知道比pgu更新维护的东西,我愿意使用不同的gui库。

如果有人想这样做,我发现了一些有用的方法:创建一个空容器并调用
app.init()

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

如果其他任何人想这样做,我发现了一些有效的方法:创建一个空容器并调用
app.init()

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

我曾尝试过类似于Nmichaels的答案的方法,在我的pygame应用程序中实现了一个独立的对话框,但我一直遇到一个类型错误:

pygame_pgu-0.21-py3.6.egg\pgu\gui\surface.py”,第10行,地下 r=pygame.Rect(r)TypeError:参数必须是Rect样式的对象

r
正在传递一个
None

删除对话框
height
参数为我解决了这个问题

import pygame, sys
from pgu import gui

# original author: user nmicahaels https://stackoverflow.com/questions/3302973/making-popup-windows-in-pygame-with-pgu

WIDTH = 640
HEIGHT = 480


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


class SimpleDialog(gui.Dialog):
    def __init__(self):
        title = gui.Label("Spam")
        main = gui.Container(width=20, height=20)
        # passing the 'height' parameter resulting in a typerror when paint was called
        super(SimpleDialog, self).__init__(title, main, width=40)  # , height=40)

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


def run():
    black = (0, 0, 0)
    screen = init_pygame()  # type: pygame.Surface
    refresh = pygame.display.update
    app = gui.App()

    dialog = SimpleDialog()
    # app.init(dialog)

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

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


if __name__ == '__main__':
    run()

我曾尝试过类似于Nmichaels的答案的方法,在我的pygame应用程序中实现了一个独立的对话框,但我一直遇到一个类型错误:

pygame_pgu-0.21-py3.6.egg\pgu\gui\surface.py”,第10行,地下 r=pygame.Rect(r)TypeError:参数必须是Rect样式的对象

r
正在传递一个
None

删除对话框
height
参数为我解决了这个问题。这是经过修改的代码

import pygame, sys
from pgu import gui

# original author: user nmicahaels https://stackoverflow.com/questions/3302973/making-popup-windows-in-pygame-with-pgu

WIDTH = 640
HEIGHT = 480


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


class SimpleDialog(gui.Dialog):
    def __init__(self):
        title = gui.Label("Spam")
        main = gui.Container(width=20, height=20)
        # passing the 'height' parameter resulting in a typerror when paint was called
        super(SimpleDialog, self).__init__(title, main, width=40)  # , height=40)

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


def run():
    black = (0, 0, 0)
    screen = init_pygame()  # type: pygame.Surface
    refresh = pygame.display.update
    app = gui.App()

    dialog = SimpleDialog()
    # app.init(dialog)

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

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


if __name__ == '__main__':
    run()