Mundo em Python

Tempo limite, de entrega de encomenda

from tkinter import *
from datetime import datetime # Função para limpar os campos de entrada e o texto do resultado
def limpar():
Hora_entrada.delete(0, END)
Dia_entrada.delete(0, END)
resultado_texto.config(text="") # Função principal para calcular o tempo restante até a entrega
def app():
d = Dia.get()
h = Hora.get()
try:
entrega_str = d + " " + h
entrega = datetime.strptime(entrega_str, "%Y-%m-%d %H:%M")
agora = datetime.now()
if entrega < agora:
resultado_texto.config(text="A data e hora de entrega são anteriores ao horário atual.", fg="red")
else:
restante = entrega - agora
dias = restante.days
horas, resto = divmod(restante.seconds, 3600)
minutos, segundos = divmod(resto, 60)
resultado_texto.config(
text=f"Tempo restante: {dias} dias, {horas} horas, {minutos} minutos, {segundos} segundos.", fg="green")
except ValueError:
resultado_texto.config(text="Formato de data ou hora inválido. Use AAAA-MM-DD e hh:mm.", fg="red")
finally:
# Chama a função app novamente após 1000 milissegundos (1 segundo) para atualizar a contagem
root.after(1000, app)
# Configuração da interface gráfica
root = Tk()
root.geometry("600x400")
root.resizable(0, 0)
root.config(bg="#103030")
root.title("Tempo limite") # Título
titulo = Label(root, text="Tempo limite", font=("Arial", 40, "bold"), bg="#103030", fg="#49e3e3")
titulo.place(relx=0.23, rely=0.05) # Labels para as entradas
texto_sub1 = Label(root, text="Dia de Entrega (AAAA-MM-DD):", font=("Arial", 15, "bold"), bg="#103030", fg="#49e3e3")
texto_sub1.place(relx=0.1, rely=0.32) texto_sub2 = Label(root, text="Hora de Entrega (hh:mm):", font=("Arial", 15, "bold"), bg="#103030", fg="#49e3e3")
texto_sub2.place(relx=0.18, rely=0.5) # Entradas para data e hora
Dia = StringVar()
Dia_entrada = Entry(root, textvariable=Dia, font=("Arial", 12, "bold"), bg="white", fg="blue", justify='center')
Dia_entrada.place(relx=0.63, rely=0.33, relwidth=0.3)
Dia_entrada.focus() Hora = StringVar()
Hora_entrada = Entry(root, textvariable=Hora, font=("Arial", 12, "bold"), bg="white", fg="blue", justify='center')
Hora_entrada.place(relx=0.63, rely=0.5, relwidth=0.3) # Botões
but1 = Button(root, text="Calcular", bd=2, bg='#107db2', fg='white', font=('verdana', 12, 'bold'), command=app)
but1.place(relx=0.1, rely=0.65, relwidth=0.25, relheight=0.1) but_limpar = Button(root, text="Limpar", bd=2, bg='#107db2', fg='white', font=('verdana', 12, 'bold'), command=limpar)
but_limpar.place(relx=0.4, rely=0.65, relwidth=0.25, relheight=0.1) but_sair = Button(root, text="Sair", bd=2, bg='#107db2', fg='white', font=('verdana', 12, 'bold'), command=root.destroy)
but_sair.place(relx=0.7, rely=0.65, relwidth=0.25, relheight=0.1) # Texto do resultado
resultado_texto = Label(root, text="", font=("Arial", 12, "bold"), bg="#cfe2f3")
resultado_texto.place(relx=0.05, rely=0.8, relwidth=0.9, relheight=0.15) # Executa a interface gráfica
root.mainloop()
0