Error closing a tab in Pysimplegui

Asked

Viewed 55 times

-2

I’m doing a test program with Pysimplegui, but basically it asks for a user, if it’s correct it opens a new tab with the phrase: "Success, you’ve logged into your account!" , and when it was wrong open a new tab with a phrase "Incorrect password. Try again". I was programming when I needed to break the loop while when the person closes the program. I even managed to do this in the part where asks the user, but when I was doing the incorrect password part, I realized that python was not recognizing the command if click == sg.WINDOW_CLOSED:. So I modified the code to put one input to see if he appeared, and if he appeared the command would be recognized. But the result is the same as the beginning: Aqui está a mensagem de erro

Here’s the code (I know I wouldn’t close with it, but I just wanted it to recognize the input first)

from PySimpleGUI import PySimpleGUI as sg
from time import sleep
sg.theme("LightGreen4")
x = [
        [sg.Text('Usuário'), sg.Input(key='usuario')],
        [sg.Button('Entrar', key = 'entrar')]
]
janela = sg.Window('lalalala', x)
s = 's'
t = 't'
while s == 's':
    while t == 't':
        click, valores = janela.read()
        if click == sg.WINDOW_CLOSED:
            s = 't'
            click = 'closed'
            break
        elif click == 'entrar':
            if valores['usuario'] == '004848':
                layout = [
                    [sg.Text('Sucesso! Você entrou na sua conta!')]
                ]
                break
            else:
                layout = [
                    [sg.Text('Senha incorreta. Tente novamente.')]
                ]
                break
    if valores['usuario'] == '004848':
        janela3 = sg.Window('sucesso!', layout)
        while True:
            click = janela3.read()
            pass
    elif click == 'closed':
        sg.Window.close(janela)
    else:
        janela3 = sg.Window('senha incorreta', layout)
        while True:
            del(click)
            click = janela3.read()
            if click == sg.WINDOW_CLOSED:
                input('s')
                sleep(100)

I’m using Windows 10 PRO, 64bit. Please, if you have an error in the question write in the comments for me to modify it. Thank you!

1 answer

-2


Try this: (Obs. it is not recommended to use Sleep in a graphical interface, it makes the code wait a little impossible for the interface to be updated and may crash)

from PySimpleGUI import PySimpleGUI as sg

def tela1(layout, name_window):
    janela = sg.Window(name_window, layout=layout, finalize=True)
    while True:
        event, values = janela.read()
        if event == sg.WINDOW_CLOSED:
            break

sg.theme("LightGreen4")
layout = [[sg.Text('Usuário'), sg.Input(key='usuario')],
    [sg.Button('Entrar', key = 'entrar')]]
janela = sg.Window('Window-PySimpleGUI', layout=layout, finalize=True)

while True:
    event, values = janela.read()
    if event == sg.WINDOW_CLOSED:
        break
    elif event == 'entrar':
        if values['usuario'] == '004848':
            janela.hide()
            layout1 = [[sg.Text('Sucesso! Você entrou na sua conta!')]]
            tela1(layout1, 'sucesso!')
            janela.un_hide()
        else:
            janela.hide()
            layout2 = [[sg.Text('Senha incorreta. Tente novamente.')]]
            tela1(layout2, 'senha incorreta')
            janela.un_hide()
  • Why the -2?? I only organized the code

  • Now I can’t test yet, but because you used the.Hide() window and the finalize = True?

  • When you open another window, it is good to "hide" one, until the other will be unanswered (you cannot touch it). finilize is used to prevent some errors that are described in the documentation: https://pysimplegui.readthedocs.io/en/latest/

  • thanks, you solved my problem, but when I wanted to use Sleep, what I would do?

  • It depends on the use... In Pysimplegui you can use the hide to hide the window and then forward you can use the team. In general nn is used a lot in these cases, it is for a very specific use

Browser other questions tagged

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