Mundo em Python

Velocidade (nós)

from tkinter import *

# Initialize the Tkinter window
root = Tk()
root.geometry("600x400")
root.resizable(0, 0)
root.config(bg="#103030")
root.title("Velocidade (nós)") # Title label
titulo = Label(text="Velocidade (nós)", font=("Arial", "28", "bold"), bg="#103030", fg="#49e3e3")
titulo.place(relx=0.25, rely=0.05) # Distance label and input
texto_sub1 = Label(text="Distância (milhas naúticas) :", font=("Arial", "18", "bold"), bg="#103030", fg="#49e3e3")
texto_sub1.place(relx=0.05, rely=0.27) 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.63, rely=0.28, relwidth=0.28) # Time label and input
texto_sub2 = Label(text="Tempo (horas) :", font=("Arial", "18", "bold"), bg="#103030", fg="#49e3e3")
texto_sub2.place(relx=0.28, rely=0.45) Tempo = StringVar()
Tempo_entrada = Entry(textvariable=Tempo, font=("Arial", "12", "bold"), bg="white", fg="blue", justify='center')
Tempo_entrada.place(relx=0.63, rely=0.45, relwidth=0.28) # Function to clear the input fields
def limpar():
Tempo_entrada.delete(0, END)
Distância_entrada.delete(0, END) # Function to calculate the speed
def app():
try:
d = float(Distância.get()) # Get the distance
t = float(Tempo.get()) # Get the time
Velocidade_nos = d / t # Calculate speed in knots
resultado_texto.config(text=f"Velocidade do navio: {round(Velocidade_nos, 3)} nós")
except ValueError:
resultado_texto.config(text="Valor digitado é inválido.") # Handle invalid input

# Calculate button
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) # Clear button
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) # Exit button
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) # Result label
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) # Run the Tkinter loop
root.mainloop()
0