Mundo em Python

Calcular hora de sono

from tkinter import *
from datetime import datetime, timedelta


def limpar():
foste_dormir.set("")
horas_acordaste.set("")
resultado_texto.config(text="", fg="#103030")
foste_dormir_entrada.focus()


def app():
try:
formato = "%H:%M"

# Obter os valores das StringVars usando o .get()
dormir_str = foste_dormir.get()
acordar_str = horas_acordaste.get()

# Converter as strings para objetos de data/hora
t_dormir = datetime.strptime(dormir_str, formato)
t_acordar = datetime.strptime(acordar_str, formato)

if t_acordar <= t_dormir:
t_acordar += timedelta(days=1)

# Calcular a diferença de tempo
diferenca = t_acordar - t_dormir
horas_totais = diferenca.total_seconds() / 3600

# Separar em horas e minutos inteiros
horas = int(horas_totais)
minutos = int((horas_totais - horas) * 60)

# Construir a mensagem para mostrar na interface gráfica
mensagem = f"Dormiste um total de {horas} horas e {minutos} minutos.\n\n"

# Feedback básico com base nas recomendações gerais
if horas < 7:
mensagem += "Dica: Dormiste menos de 7 horas. Tenta descansar mais um pouco!"
elif horas > 9:
mensagem += "Dica: Dormiste mais de 9 horas. Rotinas irregulares podem fatigar."
else:
mensagem += "Parabéns! A duração do teu sono está dentro da faixa recomendada (7 a 9h)."

# Atualizar a label de resultado na janela
resultado_texto.config(text=mensagem, fg="#103030")

except ValueError:
resultado_texto.config(
text="❌ Erro: Formato de hora inválido. Usa o formato HH:MM (ex: 22:30).",
fg="#900C3F"
)


root = Tk()
root.geometry("700x450")
root.resizable(0, 0)
root.config(bg="#103030")
root.title("CALCULADORA DE HORAS DE SONO")

# Título
titulo = Label(
root,
text="CALCULADORA DE HORAS DE SONO",
font=("Arial", 22, "bold"),
bg="#103030",
fg="#49e3e3"
)
titulo.place(relx=0.1, rely=0.05)

Label(
root,
text="A que horas foste dormir? (Formato HH:MM, ex: 23:30):",
font=("Arial", 12, "bold"),
bg="#103030",
fg="#49e3e3"
).place(relx=0.15, rely=0.15)

Label(
root,
text="A que horas acordaste? (Formato HH:MM, ex: 07:15):",
font=("Arial", 12, "bold"),
bg="#103030",
fg="#49e3e3"
).place(relx=0.15, rely=0.35)

foste_dormir = StringVar()
foste_dormir_entrada = Entry(
root,
textvariable=foste_dormir,
font=("Arial", 14, "bold"),
justify='center'
)
foste_dormir_entrada.place(relx=0.35, rely=0.23, relwidth=0.35)
foste_dormir_entrada.focus()

horas_acordaste = StringVar()
horas_acordaste_entrada = Entry(
root,
textvariable=horas_acordaste,
font=("Arial", 14, "bold"),
justify='center'
)
horas_acordaste_entrada.place(relx=0.35, rely=0.45, relwidth=0.35)

root.bind("<Return>", lambda event: app())

Button(
root, text="Mostrar", bg='#107db2', fg='white',
font=('Verdana', 12, 'bold'), command=app
).place(relx=0.1, rely=0.58, relwidth=0.25, relheight=0.08)

Button(
root, text="Limpar", bg='#107db2', fg='white',
font=('Verdana', 12, 'bold'), command=limpar
).place(relx=0.4, rely=0.58, relwidth=0.25, relheight=0.08)

Button(
root, text="Sair", bg='#107db2', fg='white',
font=('Verdana', 12, 'bold'), command=root.destroy
).place(relx=0.7, rely=0.58, relwidth=0.25, relheight=0.08)

resultado_texto = Label(
root, text="", font=("Arial", 11, "bold"), bg="#cfe2f3", fg="#103030", wraplength=650, justify="left"
)
resultado_texto.place(relx=0.05, rely=0.70, relwidth=0.9, relheight=0.25)

root.mainloop()
0