How do I make a window not close? Pysimplegui

Asked

Viewed 17 times

1

I need to disable window options made in Pysimplegui and allow closing only if you hit the password in a text box.

my code:

sg.theme('DarkRed')
layout = [
    [sg.Text('Senha'), sg.Input(key='senha', password_char='*')],
    [sg.Button('Liberar')]
]


janela = sg.Window('', layout)

while True:
    eventos, valores = janela.read()
    if eventos == sg.WINDOW_CLOSED:
        Mbox('Titulo', 'Você não pode fechar', 0)
        continue     
    if eventos == 'Liberar':
        if valores ['senha'] == '123456':
            Mbox('Titulo', 'Liberado', 0)
            break
        
        else:
            Mbox('Titulo', 'Senha errada', 0)

Below the sg.WINDOW_CLOSED I’ve tried to put a continue or a pass but it didn’t work.

Some way to disable the function, hide or make the program open every time it is closed?

1 answer

1

To do this, you can add the method enable_close_attempted_event=True in its variable janela, example:

janela = sg.Window('', layout, enable_close_attempted_event=True)

The feature will return a window closing attempt confirmation event instead of closing it, displaying a custom message and the window will remain active. Then add the event check in the window closing attempt:

if eventos in (sg.WINDOW_CLOSED, sg.WINDOW_CLOSE_ATTEMPTED_EVENT):
   Mbox('Titulo', 'Você não pode fechar', 0)

Don’t forget to finish your code with janela.close() not to generate any error when finishing the repeat structure and closing the window.

More about the Window Close Confirmation method Here

  • Thank you!! Gave straight, I will read more about this method

Browser other questions tagged

You are not signed in. Login or sign up in order to post.