Estimar hora de chegada

from tkinter import *
from datetime import datetime, timedelta


def calcular_tempo(distancia, velocidade):
if velocidade <= 0:
return "Velocidade deve ser maior que zero"
tempo = distancia / velocidade
return tempo


def estimar_hora_chegada(hora_saida, tempo_viagem):
hora_saida_dt = datetime.strptime(hora_saida, '%H:%M')
hora_chegada_dt = hora_saida_dt + timedelta(hours=tempo_viagem)
return hora_chegada_dt.strftime('%H:%M')


def limpar():
Velocidade_entrada.delete(0, END)
Horas_entrada.delete(0, END)
Distância_entrada.delete(0, END)
resultado.set("")


def app():
try:
distancia = float(Distância.get())
hora_saida = Horas.get()
velocidade = float(Velocidade.get())

if velocidade <= 0:
resultado.set("Velocidade deve ser maior que zero")
return

tempo_viagem = calcular_tempo(distancia, velocidade)

if isinstance(tempo_viagem, str):
resultado.set(tempo_viagem)
else:
hora_chegada = estimar_hora_chegada(hora_saida, tempo_viagem)
horas = int(tempo_viagem)
minutos = int((tempo_viagem - horas) * 60)
resultado.set(f"Tempo de viagem: {horas} horas e {minutos} minutos.\nHora de chegada: {hora_chegada}")
except ValueError:
resultado.set("Erro de digitação")


root = Tk()
root.geometry("600x500")
root.resizable(0, 0)
root.config(bg="#103030")
root.title("Estimar hora de chegada")

titulo = Label(text="Estimar hora de chegada",
font=("Arial", "33 ", "bold"), bg="#103030", fg="#49e3e3")
titulo.place(relx=0.08, rely=0.05)

texto_sub1 = Label(text="Distância (km):",
font=("Arial", "18", "bold"), bg="#103030", fg="#49e3e3")
texto_sub1.place(relx=0.25, rely=0.25)

texto_sub2 = Label(text="Horas (formato HH:MM):",
font=("Arial", "18", "bold"), bg="#103030", fg="#49e3e3")
texto_sub2.place(relx=0.05, rely=0.38)

texto_sub3 = Label(text="Velocidade média (em km/h):",
font=("Arial", "18", "bold"), bg="#103030", fg="#49e3e3")
texto_sub3.place(relx=0.05, rely=0.53)

Distância = StringVar()
Distância_entrada = Entry(textvariable=Distância,
font=("Arial", "12", "bold"),
bg="white", fg="blue", justify='center')
Distância_entrada.place(relx=0.65, rely=0.26, relwidth=0.3)
Distância_entrada.focus()

Horas = StringVar()
Horas_entrada = Entry(textvariable=Horas,
font=("Arial", "12", "bold"),
bg="white", fg="blue", justify='center')
Horas_entrada.place(relx=0.65, rely=0.38, relwidth=0.3)

Velocidade = StringVar()
Velocidade_entrada = Entry(textvariable=Velocidade,
font=("Arial", "12", "bold"),
bg="white", fg="blue", justify='center')
Velocidade_entrada.place(relx=0.65, rely=0.53, relwidth=0.3)

but1 = Button(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(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(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)

resultado = StringVar()
resultado_texto = Label(textvariable=resultado,
font=("Arial", 12, "bold"), bg="#cfe2f3")
resultado_texto.place(relx=0.05, rely=0.8, relwidth=0.9, relheight=0.15)

root.mainloop()

Comentários

Mensagens populares deste blogue

Criar Cartões de Visita

12 signos egípcios

Calcular a percentagem de ocupação