How to check if an Input Text is "Empty" or not in pySimpleGUI?

Asked

Viewed 110 times

-1

Hello, I’m creating a simple code to train both python and pysimplegui, and I’d like to check whether or not the user left the field empty before clicking save, but looking at the library’s documentation and examples, I couldn’t find anything that referred to it. I’ll drop the code right down so you can understand...

import sqlite3 #biblioteca para banco de dados
import PySimpleGUI as sg #biblioteca para interface (GUI)
##################################################

# conectando...
conn = sqlite3.connect('clientes.db')
# definindo um cursor
cursor = conn.cursor()

# criando a tabela (schema)
cursor.execute("""CREATE TABLE IF NOT EXISTS clientes (
    id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
    nome TEXT NOT NULL,
    idade INTEGER,
    cpf VARCHAR(11) NOT NULL,
    email TEXT,
    fone TEXT,
    cidade TEXT,
    uf VARCHAR(2));""")

##################################################

# criando a janela (GUI)
sg.theme('Tan Blue')   # Definindo um tema para a janela
# Desingn da janela.
layout = [  [sg.Text('Nome:', size=(6, 1)), sg.InputText(size=(30,1), key='-NOME-')],
            [sg.Text('Idade:', size=(6, 1)), sg.InputText(size=(30,1), key='-IDADE-')],
            [sg.Text('CPF:', size=(6, 1)), sg.InputText(size=(30,1), key='-CPF-')],
            [sg.Text('e-mail:', size=(6, 1)), sg.InputText(size=(30,1), key='-EMAIL-')],
            [sg.Text('Fone:', size=(6, 1)), sg.InputText(size=(30,1), key='-FONE-')],
            [sg.Text('Cidade:', size=(6, 1)), sg.InputText(size=(30,1), key='-CIDADE-')],
            [sg.Text('UF:', size=(6, 1)), sg.InputText(size=(30,1), key='-UF-')],
            [ sg.Button('Cadastrar', button_color = 'black on orange', font = ['Comics', 12]), 
            sg.Button('Sair', button_color = 'black on red', font = ['Comics', 12]),
            sg.Button('Ver Registros', button_color = 'black on green', font = ['Comics', 12]) ] ]

# Criando a janela
window1 = sg.Window('Cadastro de Clientes', layout)
window2_active = False

# Event Loop to process "events" and get the "values" of the inputs
while True:
    event1, values1 = window1.read()
    # if user closes window or clicks exit
    if event1 == sg.WIN_CLOSED or event1 == 'Sair':
        break
    
    if event1 == 'Cadastrar':
        try:
            # inserindo dados na tabela
            cursor.execute("""INSERT INTO clientes (nome, idade, cpf, email, fone, cidade, uf)
            VALUES (?,?,?,?,?,?,?)""", (values1['-NOME-'], values1['-IDADE-'], values1['-CPF-'], 
            values1['-EMAIL-'], values1['-FONE-'], values1['-CIDADE-'], values1['-UF-']))
            conn.commit() #NUNCA ESQUECER ESSE COMANDO
            sg.popup('Cliente cadastrado com sucesso!')
            window1['-NOME-'].update('')
            window1['-IDADE-'].update('')
            window1['-CPF-'].update('')
            window1['-EMAIL-'].update('')
            window1['-FONE-'].update('')
            window1['-CIDADE-'].update('')
            window1['-UF-'].update('')
        except E as Argument:
            print(f'Erro ao gravar os dados:\n{E}')

    if event1 == 'Ver Registros' and not window2_active:
        window2_active = True
        window1.Hide()
        
        # SEMPRE CRIAR LAYOUT NOVO, NUNCA REUTILIZAR
        layout2 = [ [sg.Text('TIPO DE FILTRO', font=('Helvetica', 14), justification='left')],
                    [sg.Radio('Nome', 'loss', default=True, size=(5, 1), font = ['Comics', 14]), 
                    sg.Radio('CPF', 'loss', size=(5, 1), font = ['Comics', 14])],
                    [sg.Text('Busca:', size=(5, 1), font = ['Comics', 13]), sg.InputText(size=(40,1), key='-BUSCA-', font = ['Comics', 13]), sg.Button('Filtrar', button_color = 'black on light blue', font = ['Comics', 12])],
                    [sg.MLine(key='-ML1-'+sg.WRITE_ONLY_KEY, size=(80,15), font='Any 13')],
                    [sg.Button('Sair', button_color = 'black on red', font = ['Comics', 12])] ]
                    
        window2 = sg.Window('Ver/Filtrar Registros', layout2, finalize=True)
        
        #Preenchendo na primeira execução
        window2['-ML1-'+sg.WRITE_ONLY_KEY].print('\nOS DADOS SALVOS NO BANCO DE DADOS SÃO:\n', text_color='yellow', background_color='black')
        cursor.execute("""SELECT * FROM clientes;""")
        for i, linha in enumerate(cursor.fetchall()):
            if i % 2 == 0:
                window2['-ML1-'+sg.WRITE_ONLY_KEY].print(linha, text_color='black', background_color='light yellow')
            else:
                window2['-ML1-'+sg.WRITE_ONLY_KEY].print(linha, text_color='black', background_color='light blue')
        
        #Laço da segunda janela
        tipoBusca = True
        while True:
            event2, values2 = window2.read()
            if event2 == sg.WIN_CLOSED or event2 == 'Sair':
                window2.Close()
                window2_active = False
                window1.UnHide()
                break

            if event2 == 'Nome':
                tipoBusca = True
            if event2 == 'CPF':
                tipoBusca = False

            if event2 == 'Filtrar' and tipoBusca:    # if something is highlighted in the list
                #window2.FindElement('-ML1-').Update('') #Não consigo limpar o Multiline
                cursor.execute(f"""SELECT * FROM clientes WHERE nome LIKE '{values2['-BUSCA-']}%';""")
                for i, linha in enumerate(cursor.fetchall()):
                    if i % 2 == 0:
                        window2['-ML1-'+sg.WRITE_ONLY_KEY].print(linha, text_color='black', background_color='light yellow')
                    else:
                        window2['-ML1-'+sg.WRITE_ONLY_KEY].print(linha, text_color='black', background_color='light blue')
            
            if event2 == 'Filtrar' and not tipoBusca:    # if something is highlighted in the list
                #window2.FindElement('-ML1-').Update('') #Não consigo limpar o Multiline
                cursor.execute(f"""SELECT * FROM clientes WHERE cpf LIKE '{values2['-BUSCA-']}%';""")
                for i, linha in enumerate(cursor.fetchall()):
                    if i % 2 == 0:
                        window2['-ML1-'+sg.WRITE_ONLY_KEY].print(linha, text_color='black', background_color='light yellow')
                    else:
                        window2['-ML1-'+sg.WRITE_ONLY_KEY].print(linha, text_color='black', background_color='light blue')
conn.close() # desconectando do banco de dados...
window1.close()
##################################################

1 answer

0

Hello @Elvis, all right ? In Pysimplegui we do not yet have a workable solution through the Framework, but you can use conditions to do this a base example and that can not be improved would use as follows:

#Window update loop while True:

definition of events and values in the Window

event, value = window.read()

Key definition -NAME- for a usage variable

NOME = value['-NOME-']
print(NOME)

Variable condition for approval is equal to empty, returns False or a Popup

if NOME == '':
    print('False')
    sg.popup('PopUp','Vazio Campo NOME')

True case continues the Window process

elif NOME != '' :
    print('True')

Complete Code

import PySimpleGUI as sg

layout = [[sg.Text('Nome:', size=(6, 1)), sg.Input(size=(30, 1), key='-NOME-')],
          [sg.Button('Ver Registros', button_color='black on green')]]

window = sg.Window('Cadastro de Clientes', layout)

while True:
    event, value = window.read()

    NOME = value['-NOME-']
    print(NOME)

    if NOME == '':
        print('False')
        sg.popup('PopUp','Vazio Campo NOME')
    elif NOME != '' :
        print('True')

    if event == sg.WIN_CLOSED :
        break

Browser other questions tagged

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