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:
10
db.json
10
db.json
@@ -9,6 +9,16 @@
|
|||||||
"birth": "24/07/2008",
|
"birth": "24/07/2008",
|
||||||
"id": 2,
|
"id": 2,
|
||||||
"name": "Chris"
|
"name": "Chris"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"birth": "11/22/33",
|
||||||
|
"id": 69,
|
||||||
|
"name": "jose"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"birth": "15/01/1995",
|
||||||
|
"id": 12349876,
|
||||||
|
"name": "Limas"
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"password": "pass",
|
"password": "pass",
|
||||||
|
|||||||
15
libs.py
15
libs.py
@@ -86,8 +86,19 @@ def add_child(child_id, name, birth):
|
|||||||
write_json('children', data['children'])
|
write_json('children', data['children'])
|
||||||
|
|
||||||
def get_pic(id):
|
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))
|
image.thumbnail((200, 200))
|
||||||
bio = io.BytesIO()
|
bio = io.BytesIO()
|
||||||
image.save(bio, format="PNG")
|
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
42
main.py
@@ -1,5 +1,7 @@
|
|||||||
import PySimpleGUI as sg
|
import PySimpleGUI as sg
|
||||||
import time
|
import time
|
||||||
|
import shutil
|
||||||
|
|
||||||
from libs import *
|
from libs import *
|
||||||
|
|
||||||
def login():
|
def login():
|
||||||
@@ -41,19 +43,19 @@ def main_menu():
|
|||||||
sg.theme('BlueMono')
|
sg.theme('BlueMono')
|
||||||
info_col = [
|
info_col = [
|
||||||
[sg.T('ID: '), sg.T('', key='-ID-')],
|
[sg.T('ID: '), sg.T('', key='-ID-')],
|
||||||
[sg.T('Name: '), sg.T('', key='-NAME-')],
|
[sg.T('Nome: '), sg.T('', key='-NAME-')],
|
||||||
[sg.T('Birth: '), sg.T('', key='-BIRTH-')],
|
[sg.T('Nascimento: '), sg.T('', key='-BIRTH-')],
|
||||||
[sg.Image(data=get_pic(0), key='-IMAGE-')]
|
[sg.Image(data=get_pic(0), key='-IMAGE-')]
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
layout = [
|
layout = [
|
||||||
[sg.Listbox(values=[row[1] for row in get_children()], size=(30, 17), key='-CHILDREN-', enable_events=True), sg.Col(info_col)],
|
[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))
|
window.set_min_size((500,350))
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
@@ -63,44 +65,52 @@ def main_menu():
|
|||||||
if event == '-CHILDREN-':
|
if event == '-CHILDREN-':
|
||||||
selected = values['-CHILDREN-']
|
selected = values['-CHILDREN-']
|
||||||
id, name, birth = get_child_by_name(selected[0])
|
id, name, birth = get_child_by_name(selected[0])
|
||||||
|
|
||||||
|
window['Remover'].update(disabled=False)
|
||||||
window['-NAME-'].update(name)
|
window['-NAME-'].update(name)
|
||||||
window['-BIRTH-'].update(birth)
|
window['-BIRTH-'].update(birth)
|
||||||
window['-ID-'].update(id)
|
window['-ID-'].update(id)
|
||||||
window['-IMAGE-'].update(get_pic(id))
|
window['-IMAGE-'].update(get_pic(id))
|
||||||
|
|
||||||
if event == 'Add':
|
if event == 'Adicionar':
|
||||||
add_layout = [
|
add_layout = [
|
||||||
[sg.T('Name')],
|
[sg.T('Nome')],
|
||||||
[sg.In(key='-NAME-')],
|
[sg.In(key='-NAME-')],
|
||||||
[sg.T('Birth')],
|
[sg.T('Nascimento')],
|
||||||
[sg.In(key='-BIRTH-')],
|
[sg.In(key='-BIRTH-')],
|
||||||
[sg.T('ID')],
|
[sg.T('ID')],
|
||||||
[sg.In(key='-ID-')],
|
[sg.In(key='-ID-')],
|
||||||
|
[sg.T('Foto')],
|
||||||
|
[sg.In(), sg.FileBrowse(key='-PICTURE-', button_text="Navegar")],
|
||||||
[sg.Button(button_text="OK")]
|
[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()
|
add_event, add_values = add_window.read()
|
||||||
|
|
||||||
if add_event == 'OK':
|
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-'])
|
add_child(int(add_values['-ID-']), add_values['-NAME-'], add_values['-BIRTH-'])
|
||||||
window['-CHILDREN-'].update(values=[row[1] for row in get_children()])
|
window['-CHILDREN-'].update(values=[row[1] for row in get_children()])
|
||||||
add_window.close()
|
add_window.close()
|
||||||
|
|
||||||
if event == 'Remove':
|
if event == 'Remover':
|
||||||
|
|
||||||
selected = values['-CHILDREN-'][0]
|
selected = values['-CHILDREN-'][0]
|
||||||
|
child_id = get_child_by_name(selected)[0]
|
||||||
|
|
||||||
id_layout = [[sg.T(f'Você tem certeza que deseja remover {selected}?')],
|
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.T(f'Essa ação é PERMANENTE e IRREVERSÍVEL!', text_color="red")],
|
||||||
[sg.Button(button_text="OK")]]
|
[sg.Button(button_text="OK")]]
|
||||||
|
|
||||||
id_window = sg.Window('Remove Child', id_layout, element_justification='c')
|
id_window = sg.Window('Remover Criança', id_layout, element_justification='c')
|
||||||
id_event, id_values = id_window.read()
|
id_event, id_values = id_window.read()
|
||||||
|
|
||||||
if id_event == 'OK':
|
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()])
|
window['-CHILDREN-'].update(values=[row[1] for row in get_children()])
|
||||||
|
|
||||||
id_window.close()
|
id_window.close()
|
||||||
@@ -114,6 +124,6 @@ def main():
|
|||||||
main_menu()
|
main_menu()
|
||||||
else:
|
else:
|
||||||
print("Login unsucessful")
|
print("Login unsucessful")
|
||||||
# main()
|
main()
|
||||||
main_menu()
|
# main_menu()
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user