2010-07-21 1496 views
3

我在用pygame編寫的遊戲中添加了一些gui元素(帶有按鈕的對話框)。我環顧四周尋找一個體面的GUI工具包,並以pgu結束。無論如何,我試圖讓它彈出一個對話框,它確實(有點),但它不關閉。在pygame中製作彈出窗口與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() 

行爲我看到:一個窗口,對話框的全屏版本中打開。我沒有做任何事情會關閉它,雖然右鍵點擊會打印我的控制檯上的「打開」,然後左擊紅色的小圓圈將打印出「關閉」。它看起來像對話框正在使用整個背景表面,而不是一個較小的只爲自己。

我希望看到的行爲:出現一個大的黑屏(稍後我會畫它),當我右擊它時,會打開一個小窗口。當我離開關閉按鈕時,窗口消失。

我懷疑它與我沒有使用桌面這一事實有關,但我不希望整個遊戲住在gui中。

現在,爲了明確,問題是:如何修改我的代碼以從我看到的行爲中獲得我想要看到的行爲?如果有人知道最近維護的東西比pgu更好,我願意使用不同的gui庫。

回答

3

如果有人想要這樣做,我發現了一些可行的方法:創建一個空容器並在其上調用app.init()

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