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.
This commit is contained in:
ovosimpatico
2024-02-01 16:55:02 -03:00
parent 03adb0128d
commit 23362988bc
5 changed files with 49 additions and 18 deletions

BIN
0.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 54 KiB

BIN
69.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 31 KiB

10
db.json
View File

@@ -9,6 +9,16 @@
"birth": "24/07/2008",
"id": 2,
"name": "Chris"
},
{
"birth": "11/22/33",
"id": 69,
"name": "jose"
},
{
"birth": "15/01/1995",
"id": 12349876,
"name": "Limas"
}
],
"password": "pass",

15
libs.py
View File

@@ -86,8 +86,19 @@ def add_child(child_id, name, birth):
write_json('children', data['children'])
def get_pic(id):
image = Image.open(f"assets/{id}.jpg")
try:
image = Image.open(f"assets/{id}.jpg")
except FileNotFoundError:
image = Image.open(f"assets/0.jpg")
image.thumbnail((200, 200))
bio = io.BytesIO()
image.save(bio, format="PNG")
return bio.getvalue()
return bio.getvalue()
def removepicbyid(id):
path = f"assets/{id}.jpg"
if os.path.exists(path):
os.remove(path)
return True
else:
return False

42
main.py
View File

@@ -1,5 +1,7 @@
import PySimpleGUI as sg
import time
import shutil
from libs import *
def login():
@@ -41,19 +43,19 @@ def main_menu():
sg.theme('BlueMono')
info_col = [
[sg.T('ID: '), sg.T('', key='-ID-')],
[sg.T('Name: '), sg.T('', key='-NAME-')],
[sg.T('Birth: '), sg.T('', key='-BIRTH-')],
[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('Add'), sg.Button('Remove')]], justification='center')]
[sg.Column([[sg.Button('Adicionar'), sg.Button('Remover', disabled=True)]], justification='center')]
]
window = sg.Window('Children App', layout, finalize=True)
window = sg.Window('IDE Cadastro', layout, finalize=True)
window.set_min_size((500,350))
while True:
@@ -63,44 +65,52 @@ def main_menu():
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 == 'Add':
if event == 'Adicionar':
add_layout = [
[sg.T('Name')],
[sg.T('Nome')],
[sg.In(key='-NAME-')],
[sg.T('Birth')],
[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('Add Child', add_layout)
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 == 'Remove':
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('Remove Child', id_layout, element_justification='c')
id_event, id_values = id_window.read()
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(get_child_by_name(selected)[0])
remove_child_by_id(child_id)
removepicbyid(child_id)
window['-CHILDREN-'].update(values=[row[1] for row in get_children()])
id_window.close()
@@ -114,6 +124,6 @@ def main():
main_menu()
else:
print("Login unsucessful")
# main()
main_menu()
main()
# main_menu()