How to reopen a pysimplegui window?
Question:
I have some code that uses pygame to create a button. When this button is pressed, a pysimplegui window is opened. When this pysimplegui window is closed it cannot be reopened again. I was wondering if there was some way of reoping the window.
Code for pygame button:
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, ac,(x,y,w,h))
if click[0] == 1 and action!= None:
if action == "play":
asdc(window)
if action == "quit":
pygame.quit()
quit()
else:
pygame.draw.rect(gameDisplay, ic,(x,y,w,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)
Code for pysimplegui button:
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()
How the code is run:
while True:
gameDisplay.fill(white)
mouse = pygame.mouse.get_pos()
click = pygame.mouse.get_pressed()
button("Enter",100,150,100,50,darkgreen,green,"play")
button("Exit",450,150,100,50,darkred,red,"quit")
for event in pygame.event.get() :
if event.type == pygame.QUIT :
pygame.quit()
quit()
pygame.display.update()
Thanks in advance
Answers:
You can only use the layout and window objects once. After you close the window, you need to create a new one.
Try this code:
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, ac,(x,y,w,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, ic,(x,y,w,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)
You can minimize (hide) your window without closing and make it available to open again.
Example:
#Open window
old_windows()
# Hide old_windows()
window.hide()
#reopen old_windows()
window.unhide()
#open new window
window.hide()
new_window()
I have some code that uses pygame to create a button. When this button is pressed, a pysimplegui window is opened. When this pysimplegui window is closed it cannot be reopened again. I was wondering if there was some way of reoping the window.
Code for pygame button:
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, ac,(x,y,w,h))
if click[0] == 1 and action!= None:
if action == "play":
asdc(window)
if action == "quit":
pygame.quit()
quit()
else:
pygame.draw.rect(gameDisplay, ic,(x,y,w,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)
Code for pysimplegui button:
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()
How the code is run:
while True:
gameDisplay.fill(white)
mouse = pygame.mouse.get_pos()
click = pygame.mouse.get_pressed()
button("Enter",100,150,100,50,darkgreen,green,"play")
button("Exit",450,150,100,50,darkred,red,"quit")
for event in pygame.event.get() :
if event.type == pygame.QUIT :
pygame.quit()
quit()
pygame.display.update()
Thanks in advance
You can only use the layout and window objects once. After you close the window, you need to create a new one.
Try this code:
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, ac,(x,y,w,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, ic,(x,y,w,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)
You can minimize (hide) your window without closing and make it available to open again.
Example:
#Open window
old_windows()
# Hide old_windows()
window.hide()
#reopen old_windows()
window.unhide()
#open new window
window.hide()
new_window()