Mundo em Python

Memorial de Falecimento

from tkinter import *
from tkinter import messagebox
from datetime import datetime
import random def calcular_idade(nascimento, falecimento):
anos = falecimento.year - nascimento.year
if (falecimento.month, falecimento.day) < (nascimento.month, nascimento.day):
anos -= 1
return anos def limpar():
Data_felecimento_entrada.delete(0, END)
Data_nascimento_entrada.delete(0, END)
Nome_Completo_entrada.delete(0, END)
resultado_texto.delete("1.0", END) def app():
nome = Nome_Completo.get().strip()
dn_input = Data_nascimento.get().strip()
df_input = Data_felecimento.get().strip() if not nome or not dn_input or not df_input:
messagebox.showerror("Erro", "Todos os campos devem ser preenchidos.")
return

try:
data_nascimento = datetime.strptime(dn_input, "%d/%m/%Y")
data_falecimento = datetime.strptime(df_input, "%d/%m/%Y")
except ValueError:
messagebox.showerror(
"Erro",
"As datas devem ser reais e no formato DD/MM/AAAA.\nExemplo válido: 29/02/2020"
)
return

hoje = datetime.now() if data_falecimento < data_nascimento:
messagebox.showerror("Erro", "A data de falecimento não pode ser anterior à de nascimento.")
return
if data_falecimento > hoje:
messagebox.showerror("Erro", "A data de falecimento não pode ser no futuro.")
return

idade = calcular_idade(data_nascimento, data_falecimento)
if idade > 150:
messagebox.showerror("Erro", "Idade superior a 150 anos. Verifique as datas.")
return

epitafios = [
"Descansou em paz, mas viverá para sempre na nossa memória.",
"Partiu, mas deixou um amor que nunca desaparecerá.",
"A sua luz jamais se apagará nos nossos corações.",
"Aqueles que amamos nunca morrem; apenas partem antes de nós.",
"Fez do mundo um lugar melhor — agora repousa em paz.",
"O tempo leva a presença, mas nunca o carinho que deixou."
] mensagem = random.choice(epitafios) resultado = f"""
╔══════════════════════════════════════╗
EM MEMÓRIA DE
{nome}
╟──────────────────────────────────────╢
* Nascimento: {data_nascimento.strftime('%d/%m/%Y')}
† Falecimento: {data_falecimento.strftime('%d/%m/%Y')}
Idade no momento da morte: {idade} anos
╟──────────────────────────────────────╢
"{mensagem}"
╚══════════════════════════════════════╝
"""
messagebox.showinfo("Em Memória", resultado)
resultado_texto.delete("1.0", END)
resultado_texto.insert(END, resultado)
root = Tk()
root.geometry("700x600")
root.resizable(0, 0)
root.config(bg="#103030")
root.title("Memorial de Falecimento") titulo = Label(text="Memorial de Falecimento",
font=("Arial", 28, "bold"), bg="#103030", fg="#49e3e3")
titulo.place(relx=0.15, rely=0.05) texto_sub1 = Label(text="Nome Completo:",
font=("Arial", 18, "bold"), bg="#103030", fg="#49e3e3")
texto_sub1.place(relx=0.15, rely=0.2) Nome_Completo = StringVar()
Nome_Completo_entrada = Entry(textvariable=Nome_Completo,
font=("Arial", 12, "bold"),
bg="white", fg="blue", justify='center')
Nome_Completo_entrada.place(relx=0.45, rely=0.21, relwidth=0.48)
Nome_Completo_entrada.focus()
texto_sub2 = Label(text="Data de Nascimento (DD/MM/AAAA):",
font=("Arial", 18, "bold"), bg="#103030", fg="#49e3e3")
texto_sub2.place(relx=0.05, rely=0.3) Data_nascimento = StringVar()
Data_nascimento_entrada = Entry(textvariable=Data_nascimento,
font=("Arial", 12, "bold"),
bg="white", fg="blue", justify='center')
Data_nascimento_entrada.place(relx=0.67, rely=0.31, relwidth=0.25)
texto_sub3 = Label(text="Data de Falecimento (DD/MM/AAAA):",
font=("Arial", 18, "bold"), bg="#103030", fg="#49e3e3")
texto_sub3.place(relx=0.05, rely=0.4) Data_felecimento = StringVar()
Data_felecimento_entrada = Entry(textvariable=Data_felecimento,
font=("Arial", 12, "bold"),
bg="white", fg="blue", justify='center')
Data_felecimento_entrada.place(relx=0.67, rely=0.41, relwidth=0.25) # Botões
but1 = Button(text="Mostrar", bd=2, bg='#107db2', fg='white',
font=('verdana', 12, 'bold'), command=app)
but1.place(relx=0.1, rely=0.5, relwidth=0.25, relheight=0.1) but_limpar = Button(text="Limpar", bd=2, bg='#107db2', fg='white',
font=('verdana', 12, 'bold'), command=limpar)
but_limpar.place(relx=0.4, rely=0.5, relwidth=0.25, relheight=0.1) but_sair = Button(text="Sair", bd=2, bg='#107db2', fg='white',
font=('verdana', 12, 'bold'), command=root.destroy)
but_sair.place(relx=0.7, rely=0.5, relwidth=0.25, relheight=0.1)
resultado_texto = Text(font=("Arial", 12, "bold"), bg="#cfe2f3")
resultado_texto.place(relx=0.05, rely=0.65, relwidth=0.9, relheight=0.28) root.mainloop()
0