from tkinter import *
import math
root = Tk()
root.geometry("500x500")
root.resizable(0, 0)
root.config(bg="#103030")
root.title("Fórmula de Haversine ")
titulo = Label(text="Fórmula de Haversine",
font=("Arial", "28", "bold"), bg="#103030", fg="#49e3e3")
titulo.place(relx=0.1, rely=0.05)
texto_sub1 = Label(text="Latitude",
font=("Arial", "18", "bold"), bg="#103030", fg="#49e3e3")
texto_sub1.place(relx=0.35, rely=0.2)
texto_sub2 = Label(text="Longitude",
font=("Arial", "18", "bold"), bg="#103030", fg="#49e3e3")
texto_sub2.place(relx=0.7, rely=0.2)
texto_sub3 = Label(text="Lugar 1",
font=("Arial", "18", "bold"), bg="#103030", fg="#49e3e3")
texto_sub3.place(relx=0.05, rely=0.35)
texto_sub4 = Label(text="Lugar 2",
font=("Arial", "18", "bold"), bg="#103030", fg="#49e3e3")
texto_sub4.place(relx=0.05, rely=0.5)
lat1 = StringVar()
lat1_entrada = Entry(textvariable=lat1,
font=("Arial", "12", "bold"),
bg="white", fg="blue", justify='center')
lat1_entrada.place(relx=0.35, rely=0.35, relwidth=0.2)
lat2 = StringVar()
lat2_entrada = Entry(textvariable=lat2,
font=("Arial", "12", "bold"),
bg="white", fg="blue", justify='center')
lat2_entrada.place(relx=0.35, rely=0.5, relwidth=0.2)
lon1 = StringVar()
lon1_entrada = Entry(textvariable=lon1,
font=("Arial", "12", "bold"),
bg="white", fg="blue", justify='center')
lon1_entrada.place(relx=0.73, rely=0.35, relwidth=0.2)
lon2 = StringVar()
lon2_entrada = Entry(textvariable=lon2,
font=("Arial", "12", "bold"),
bg="white", fg="blue", justify='center')
lon2_entrada.place(relx=0.73, rely=0.5, relwidth=0.2)
def limpar():
lon2_entrada.delete(0, END)
lon1_entrada.delete(0, END)
lat2_entrada.delete(0, END)
lat1_entrada.delete(0, END)
resultado_texto.config(text="")
def app():
try:
Longitude1 = float(lon1.get()) # lon1 é o campo de entrada para Longitude1
Longitude2 = float(lon2.get()) # lon2 é o campo de entrada para Longitude2
Latitude1 = float(lat1.get()) # lat1 é o campo de entrada para Latitude1
Latitude2 = float(lat2.get()) # lat2 é o campo de entrada para Latitude2
R = 3959 # Raio da Terra em milhas
lat1_rad = math.radians(Latitude1)
lon1_rad = math.radians(Longitude1)
lat2_rad = math.radians(Latitude2)
lon2_rad = math.radians(Longitude2)
dlat = lat2_rad - lat1_rad
dlon = lon2_rad - lon1_rad
a = math.sin(dlat / 2) ** 2 + math.cos(lat1_rad) * math.cos(lat2_rad) * math.sin(dlon / 2) ** 2
c = 2 * math.atan2(math.sqrt(a), math.sqrt(1 - a))
distance = R * c
mensagem = f"A distância é {distance:.2f} milhas"
resultado_texto.config(text=mensagem)
except ValueError:
resultado_texto.config(text="Digitou um valor inválido!")
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()