-1

Olá, estou criando um código simples para treinar tanto python como pysimplegui, e gostaria de verificar se o usuário deixou ou não o campo vazio antes de clicar em salvar, mas olhando a documentação e exemplos da biblioteca, não encontrei nada que remetesse a isso. vou deixar o código logo abaixo para que possam entender...

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

2 Respostas 2

-1

No PySimpleGUI ainda não temos uma solução viável através do Framework, mas você pode usar condições para fazer isso um exemplo base e que pode ser nem melhorado seria usar da seguinte forma:

Laço de atualização da Janela

while True:

Definição de eventos e valores da Janela

event, value = window.read()

Definição da Key -NOME- para uma variável de uso

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

Condição da variável para aprovação ser for igual a vazio, retorna Falso ou um Popup

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

Caso True continua o processo da Janela

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

Código Completo

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
-3

Opa, sou dev jr e sei que é antigo mas basicamente existem duas opçoes muitos simples

    if variavel =='':
        inputEstaVazio = True
    

ou

    if len(variavel) > 0:
        inputEstaPreenchido = True
    else:
        inputEstaVazio = True

em pySimpleGUI apenas mude variavel para:

    values['variavel'] 

que vai funcionar do mesmo jeito

Você deve fazer log-in para responder a esta pergunta.

Esta não é a resposta que você está procurando? Pesquise outras perguntas com a tag .