强制覆盖 PySimpleGUI 窗口

问题描述

我只是希望如果有人尝试使用关闭按钮而不是单击“确定”来关闭 GUI 窗口,该窗口会重新出现...简而言之,他们无法在不单击“确定”的情况下关闭此窗口或访问任何其他窗口.

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()

解决方法

它在 PySimpleGUI 的源代码中定义。 您可以更改它以生成一个事件“WIN_CLOSE”,就像这样

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",()))

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