Files
ide-cadastro/main.py
ovosimpatico 23362988bc Add functionality to remove child and associated picture by ID.
Handle file not found exception when retrieving child picture.
Update interface language to Portuguese.
Fix minor layout issues and button text.
2024-02-01 16:55:02 -03:00

130 lines
4.3 KiB
Python

import PySimpleGUI as sg
import time
import shutil
from libs import *
def login():
sg.theme('DarkAmber')
layout = [
[sg.Text("Nome de usuário")],
[sg.Input(key='-USERNAME-')],
[sg.Text("Senha")],
[sg.Input(key='-PASSWORD-', password_char='*')],
[sg.Text(size=(40,1), key='-TEXT-', text_color="red")],
[sg.Button('Login'), sg.Button('Sair')]
]
window = sg.Window('Login', layout)
while True:
event, values = window.read()
if event == sg.WINDOW_CLOSED or event == 'Sair':
break
if event == 'Login':
if validate_login(values['-USERNAME-'], values['-PASSWORD-']):
window.close()
return True
else:
window['-TEXT-'].update('Nome de usuário ou senha incorretos!')
window.refresh()
time.sleep(2)
window['-TEXT-'].update('')
window.close()
return False
##########################################################################################
def main_menu():
sg.theme('BlueMono')
info_col = [
[sg.T('ID: '), sg.T('', key='-ID-')],
[sg.T('Nome: '), sg.T('', key='-NAME-')],
[sg.T('Nascimento: '), sg.T('', key='-BIRTH-')],
[sg.Image(data=get_pic(0), key='-IMAGE-')]
]
layout = [
[sg.Listbox(values=[row[1] for row in get_children()], size=(30, 17), key='-CHILDREN-', enable_events=True), sg.Col(info_col)],
[sg.Column([[sg.Button('Adicionar'), sg.Button('Remover', disabled=True)]], justification='center')]
]
window = sg.Window('IDE Cadastro', layout, finalize=True)
window.set_min_size((500,350))
while True:
event, values = window.read()
if event == sg.WIN_CLOSED:
break
if event == '-CHILDREN-':
selected = values['-CHILDREN-']
id, name, birth = get_child_by_name(selected[0])
window['Remover'].update(disabled=False)
window['-NAME-'].update(name)
window['-BIRTH-'].update(birth)
window['-ID-'].update(id)
window['-IMAGE-'].update(get_pic(id))
if event == 'Adicionar':
add_layout = [
[sg.T('Nome')],
[sg.In(key='-NAME-')],
[sg.T('Nascimento')],
[sg.In(key='-BIRTH-')],
[sg.T('ID')],
[sg.In(key='-ID-')],
[sg.T('Foto')],
[sg.In(), sg.FileBrowse(key='-PICTURE-', button_text="Navegar")],
[sg.Button(button_text="OK")]
]
add_window = sg.Window('Adicionar Criança', add_layout)
add_event, add_values = add_window.read()
if add_event == 'OK':
new_filename = f"assets/{add_values['-ID-']}.jpg"
picture_path = add_values['-PICTURE-']
shutil.copy(picture_path, new_filename)
add_child(int(add_values['-ID-']), add_values['-NAME-'], add_values['-BIRTH-'])
window['-CHILDREN-'].update(values=[row[1] for row in get_children()])
add_window.close()
if event == 'Remover':
selected = values['-CHILDREN-'][0]
child_id = get_child_by_name(selected)[0]
id_layout = [[sg.T(f'Você tem certeza que deseja remover {selected}?')],
[sg.T(f'Essa ação é PERMANENTE e IRREVERSÍVEL!', text_color="red")],
[sg.Button(button_text="OK")]]
id_window = sg.Window('Remover Criança', id_layout, element_justification='c')
id_event, id_values = id_window.read()
if id_event == 'OK':
remove_child_by_id(child_id)
removepicbyid(child_id)
window['-CHILDREN-'].update(values=[row[1] for row in get_children()])
id_window.close()
window.close()
###############################################
def main():
if login():
print("Login sucessful")
main_menu()
else:
print("Login unsucessful")
main()
# main_menu()