如何重新打开pysimplegui窗口?

问题描述

我有一些使用pygame创建按钮的代码。按下此按钮后,将打开pysimplegui窗口。关闭此pysimplegui窗口后,将无法再次将其重新打开。我想知道是否有某种方法可以重新打开窗户。

pygame按钮的代码

def button(msg,x,y,w,h,ic,ac,action =None):
    if x + y > mouse[0] > x and y + h > mouse[1] > y:
        pygame.draw.rect(gamedisplay,(x,h))
        
        if click[0] == 1 and action!= None:
            
            if action == "play":
                asdc(window)
                
                
            if action == "quit":
                pygame.quit()
                quit()
            
    else:
        pygame.draw.rect(gamedisplay,h))


    smallText = pygame.font.SysFont("comicsansms",20)
    textSurf,textRect = text_objects(msg,smallText)
    textRect.center = ((round(x+(w/2))),(round(y + (h/2))))
    gamedisplay.blit(textSurf,textRect)

pysimplegui按钮的代码

def asdc(window):
    while True:
        event,values = window.Read()
        if event == sg.WIN_CLOSED: 
            break
        
        if event == 'Enter':                            
            event,values = window.Read()    
            AllyBan1 = values["AllyBan1"]
            AllyBan2 = values["AllyBan2"]
            AllyBan3 = values["AllyBan3"]
            AllyBan4 = values["AllyBan4"]
            AllyBan5 = values["AllyBan5"] 
            
                break
                
    window.close()

代码的运行方式:

while True:
    gamedisplay.fill(white)

    mouse = pygame.mouse.get_pos()
    click = pygame.mouse.get_pressed()

    button("Enter",100,150,50,darkgreen,green,"play")
    button("Exit",450,darkred,red,"quit")

    for event in pygame.event.get() :
        if event.type == pygame.QUIT :
            pygame.quit()

            quit() 
    pygame.display.update()   

预先感谢

解决方法

您只能使用一次布局和窗口对象。关闭窗口后,您需要创建一个新窗口。

尝试以下代码:

def createwindow():
    layout = [[sg.Text("Hello from PySimpleGUI")],[sg.Button("OK")]]
    return sg.Window("Demo",layout)

def button(msg,x,y,w,h,ic,ac,action =None):
    if x + y > mouse[0] > x and y + h > mouse[1] > y:
        pygame.draw.rect(gameDisplay,(x,h))
        
        if click[0] == 1 and action!= None:
            
            if action == "play":
                window = createwindow()  # must create each time
                asdc(window)
                
            if action == "quit":
                pygame.quit()
                quit()
    else:
        pygame.draw.rect(gameDisplay,h))

    smallText = pygame.font.SysFont("comicsansms",20)
    textSurf,textRect = text_objects(msg,smallText)
    textRect.center = ((round(x+(w/2))),(round(y + (h/2))))
    gameDisplay.blit(textSurf,textRect)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...