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”.
JavaScript
x
18
18
1
import PySimpleGUI as sg
2
3
layout = [[sg.Text("Click OK to start the unlock process using Face Verification")],
4
[sg.Button("OK")]]
5
6
# Create the window
7
window = sg.Window("Demo", layout)
8
9
# Create an event loop
10
while True:
11
event, values = window.read()
12
# End program if user closes window or
13
# presses the OK button
14
if event == "OK" or event == sg.WIN_CLOSED:
15
break
16
17
window.close()
18
Advertisement
Answer
It is defined in source code of PySimpleGUI. You can change it to generate an event “WIN_CLOSE”, like this
JavaScript
1
18
18
1
import PySimpleGUI as sg
2
3
layout = [[sg.Text("Click OK to start the unlock process using Face Verification")], [sg.Button("OK")]]
4
window = sg.Window("Title", layout, finalize=True)
5
6
window.TKroot.protocol("WM_DESTROY_WINDOW", lambda:window.write_event_value("WIN_CLOSE", ()))
7
window.TKroot.protocol("WM_DELETE_WINDOW", lambda:window.write_event_value("WIN_CLOSE", ()))
8
9
while True:
10
event, values = window.read()
11
print(event)
12
if event in ("OK", sg.WIN_CLOSED):
13
break
14
elif event == "WIN_CLOSE":
15
print("Close Button 'X' clicked !")
16
17
window.close()
18
JavaScript
1
8
1
WIN_CLOSE
2
Close Button 'X' clicked !
3
WIN_CLOSE
4
Close Button 'X' clicked !
5
WIN_CLOSE
6
Close Button 'X' clicked !
7
OK
8