Mundo em Python

Calculador de Rota Básica

from tkinter import *

root = Tk()
root.geometry("800x400")
root.resizable(0, 0)
root.config(bg="#103030")
root.title("Calculador de Rota Básica")
titulo = Label(
text="Calculador de Rota Básica",
font=("Arial", 28, "bold"),
bg="#103030",
fg="#49e3e3"
)
titulo.place(relx=0.15, rely=0.05)
texto_sub1 = Label(
text="Distância Viagem (em milhas náuticas):",
font=("Arial", 18, "bold"),
bg="#103030",
fg="#49e3e3"
)
texto_sub1.place(relx=0.05, rely=0.25) texto_sub2 = Label(
text="Velocidade da embarcação (em nós):",
font=("Arial", 18, "bold"),
bg="#103030",
fg="#49e3e3"
)
texto_sub2.place(relx=0.05, rely=0.45)
Distância_Viagem = StringVar()
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.65, rely=0.26, relwidth=0.28)
Distância_Viagem_entrada.focus() Velocidade_embarcação = StringVar()
Velocidade_embarcação_entrada = Entry(
textvariable=Velocidade_embarcação,
font=("Arial", 12, "bold"),
bg="white",
fg="blue",
justify='center'
)
Velocidade_embarcação_entrada.place(relx=0.65, rely=0.46, relwidth=0.28) # Funções
def limpar():
Velocidade_embarcação_entrada.delete(0, END)
Distância_Viagem_entrada.delete(0, END)
resultado_texto.config(text="") def app():
try:
distancia = float(Distância_Viagem.get())
velocidade = float(Velocidade_embarcação.get()) # CORRIGIDO

if distancia <= 0 or velocidade <= 0:
mensagem = "Erro: A distância e a velocidade devem ser maiores que zero."
else:
tempo_horas = distancia / velocidade
horas = int(tempo_horas)
minutos = int((tempo_horas - horas) * 60)
mensagem = f"Tempo estimado: {horas}h {minutos}min"

resultado_texto.config(text=mensagem) except ValueError:
resultado_texto.config(text="Por favor, insira valores numéricos válidos.")
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_texto = Label(
text="",
font=("Arial", 12, "bold"),
bg="#cfe2f3"
)
resultado_texto.place(relx=0.05, rely=0.8, relwidth=0.9, relheight=0.15) root.mainloop()
0