我正在尝试将一些gui元素(带有按钮的对话框)添加到我用pygame编写的游戏中。我四处寻找一个像样的gui工具包,最后选择了pgu。无论如何,我试图让它弹出一个对话框,它确实(某种程度上),但它没有关闭。
下面是我的代码的一个简化版本,它只显示了我所关心的行为:
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()我所看到的行为是:一个窗口打开,其中包含一个全屏幕版本的对话框。我不会做任何事情来关闭它,尽管右击会在我的控制台上打印“打开”,左键单击红色小圆圈会让它打印“关闭”。看起来对话框使用的是整个背景表面,而不是仅用于其自身的较小的背景表面。
我希望看到的行为是:出现一个大黑屏(稍后我将在上面绘制),当我右键单击它时,一个小窗口就会打开。当我用鼠标左键单击close按钮时,窗口将消失。
我怀疑这与我没有使用桌面的事实有关,但我不希望整个游戏都生活在gui中。
现在,只是明确地说,问题是:我如何修改我的代码,以从我看到的行为得到我想要看到的行为?如果有人知道比pgu更新维护的东西,我愿意使用不同的gui库。
发布于 2010-08-22 22:24:39
如果其他人想要这样做,我发现了一些有用的方法:创建一个空的容器并在其上调用app.init()。
empty = gui.Container(width=WIDTH, height=HEIGHT)
gui.init(empty)发布于 2019-05-29 02:12:53
我尝试了一些类似于nmicahaels的答案,以在我的pygame应用程序中实现一个独立的对话框,但我一直收到typeerror:
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()https://stackoverflow.com/questions/3302973
复制相似问题