Skip to content
Advertisement

Force overlay a PySimpleGUI window

I just want that if someone tries to close the GUI window using close button instead of clicking “OK”, the window reappears… In simple words, they cannot close this one or access any other window without clicking “OK”.

import PySimpleGUI as sg

layout = [[sg.Text("Click OK to start the unlock process using Face Verification")], 
          [sg.Button("OK")]]

# Create the window
window = sg.Window("Demo", layout)

# Create an event loop
while True:
    event, values = window.read()
    # End program if user closes window or
    # presses the OK button
    if event == "OK" or event == sg.WIN_CLOSED:
        break

window.close()

Advertisement

Answer

It is defined in source code of PySimpleGUI. You can change it to generate an event “WIN_CLOSE”, like this

import PySimpleGUI as sg

layout = [[sg.Text("Click OK to start the unlock process using Face Verification")], [sg.Button("OK")]]
window = sg.Window("Title", layout, finalize=True)

window.TKroot.protocol("WM_DESTROY_WINDOW", lambda:window.write_event_value("WIN_CLOSE", ()))
window.TKroot.protocol("WM_DELETE_WINDOW",  lambda:window.write_event_value("WIN_CLOSE", ()))

while True:
    event, values = window.read()
    print(event)
    if event in ("OK", sg.WIN_CLOSED):
        break
    elif event == "WIN_CLOSE":
        print("Close Button 'X' clicked !")

window.close()
WIN_CLOSE
Close Button 'X' clicked !
WIN_CLOSE
Close Button 'X' clicked !
WIN_CLOSE
Close Button 'X' clicked !
OK
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement