Mundo em Python

Tempo de Viagem

from tkinter import *
root = Tk()
root.geometry("500x400")
root.resizable(0, 0)
root.config(bg="#3c58e6")
root.title("Tempo de Viagem")
titulo = Label(text="Tempo de Viagem",
font=("Arial", "35", "bold"),bg="#3c58e6",fg="#d9ead3")
titulo.place(relx=0.1, rely=0.05)
texto_sub1 = Label(text="Distância Percorrida (em km):",
font=("Arial", "15", "bold"),bg="#3c58e6",fg="#d9ead3")
texto_sub1.place(relx=0.05, rely=0.35) texto_sub2 = Label(text="Velocidade Média (em km/h):",
font=("Arial", "15", "bold"),bg="#3c58e6",fg="#d9ead3")
texto_sub2.place(relx=0.06, rely=0.5) Distância = DoubleVar()
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.35, relwidth=0.3) velocidade = DoubleVar()
velocidade_entrada = Entry(textvariable=velocidade,
font=("Arial", "12", "bold"),
bg="white", fg="blue", justify='center')
velocidade_entrada.place(relx=0.65, rely=0.5, relwidth=0.3)
def calcular_tempo_viagem(distancia, velocidade_media):
if velocidade_media <= 0:
return "A velocidade média deve ser maior que zero."

else:
tempo_em_horas = distancia / velocidade_media
return f"O tempo estimado de viagem é de {tempo_em_horas:.2f} horas."

def limpar():
velocidade_entrada.delete(0, END)
Distância_entrada.delete(0, END)
resultado.set("")
def app():
v = velocidade.get()
d = Distância.get()
result = calcular_tempo_viagem(d, v)
resultado.set(result)
but1 = Button(text="Calcular", bd=2, bg='#107db2', fg='white',
font=('verdana', 12, 'bold'), command=app)
but1.place(relx=0.05, 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.35, 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.65, 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()

# Outra Versão


from tkinter import *
root = Tk()
root.geometry("500x400")
root.resizable(0, 0)
root.config(bg="#0c7275")
root.title("Tempo de Viagem") titulo = Label(text="Tempo de Viagem",
font=("Arial", "40", "bold"),bg="#0c7275",fg="#90d2d4")
titulo.place(relx=0.04, rely=0.05)
texto_sub1 = Label(text="Distância da Viagem(km):",
font=("Arial", "18", "bold"),bg="#0c7275",fg="#90d2d4")
texto_sub1.place(relx=0.05, rely=0.3) texto_sub2 = Label(text="Velocidade Média(km/h):",
font=("Arial", "18", "bold"),bg="#0c7275",fg="#90d2d4")
texto_sub2.place(relx=0.05, rely=0.45) Distância_Viagem = DoubleVar()
Distância_Viagem_entrada = Entry(textvariable=Distância_Viagem,
font=("Arial", "12", "bold"),
bg="white", fg="blue", justify='center')
Distância_Viagem_entrada.place(relx=0.67, rely=0.3, relwidth=0.25)
Velocidade_Média = DoubleVar()
Velocidade_Média_entrada = Entry(textvariable=Velocidade_Média,
font=("Arial", "12", "bold"),
bg="white", fg="blue", justify='center')
Velocidade_Média_entrada.place(relx=0.67, rely=0.45, relwidth=0.25)
def limpar():
Velocidade_Média_entrada.delete(0, END)
Distância_Viagem_entrada.delete(0, END)
resultado.set("")
def app():
velocidade_media = Velocidade_Média.get()
distancia=Distância_Viagem.get()
tempo_horas = distancia / velocidade_media
horas = int(tempo_horas)
minutos = int((tempo_horas - horas) * 60)
mensagem = f"Tempo estimado de viagem: {horas} horas e {minutos} minutos."
resultado.set(mensagem) but1 = Button(text="Verificar", bd=2, bg='#107db2', fg='white',
font=('verdana', 12, 'bold'), command=app)
but1.place(relx=0.05, 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.35, 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.65, 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()
0