Mundo em Python

Converter Milhas Náuticas para Nós ou Vice-Versa

# Milhas vs nós
from tkinter import *
from tkinter import ttk class AppConverter:
def __init__(self):
self.root = Tk()
self.janela()
self.frames_da_janela()
self.widgets_frame1()
self.menus()
self.root.mainloop() def janela(self):
self.root.title("Converter Milhas Náuticas para Nós ou Vice-Versa")
self.root.configure(background='#1e3743')
self.root.geometry("500x300")
self.root.resizable(False, False) def frames_da_janela(self):
self.frame_1 = Frame(self.root, bd=4, bg='#dfe3ee',
highlightbackground='#759fe6', highlightthickness=2)
self.frame_1.place(relx=0.02, rely=0.02, relwidth=0.96, relheight=0.96) def widgets_frame1(self):
self.abas = ttk.Notebook(self.frame_1)
self.milhas_nauticas_nos = Frame(self.abas, background="#dfe3ee")
self.nos_milhas_nauticas = Frame(self.abas, background="#dfe3ee") self.abas.add(self.milhas_nauticas_nos, text="Milhas Náuticas para Nós")
self.abas.add(self.nos_milhas_nauticas, text="Nós para Milhas Náuticas")
self.abas.place(relx=0, rely=0, relwidth=0.98, relheight=0.98) # Milhas Náuticas para Nós
self.milhas_nauticas = DoubleVar()
self.tempo1 = DoubleVar()
self.resultado_nos = StringVar() Label(self.milhas_nauticas_nos, text="Milhas Náuticas:", bg='#dfe3ee', fg='#107db2',
font=("Arial", 14, "bold")).place(relx=0.15, rely=0.05)
Entry(self.milhas_nauticas_nos, textvariable=self.milhas_nauticas, justify='center',
font=("Arial", 14)).place(relx=0.55, rely=0.05, relwidth=0.35) Label(self.milhas_nauticas_nos, text="Tempo (horas):", bg='#dfe3ee', fg='#107db2',
font=("Arial", 14, "bold")).place(relx=0.15, rely=0.25)
Entry(self.milhas_nauticas_nos, textvariable=self.tempo1, justify='center',
font=("Arial", 14)).place(relx=0.55, rely=0.25, relwidth=0.35) Button(self.milhas_nauticas_nos, text="Calcular", bd=2, bg='#107db2', fg='white',
font=('verdana', 12, 'bold'), command=self.calcular_nos).\
place(relx=0.3, rely=0.45, relwidth=0.45, relheight=0.2) Label(self.milhas_nauticas_nos, text="Velocidade (Nós):", bg='#dfe3ee', fg='#107db2',
font=("Arial", 14, "bold")).place(relx=0.05, rely=0.73)
Label(self.milhas_nauticas_nos, textvariable=self.resultado_nos,
font=("Arial", 14, "bold")).place(relx=0.55, rely=0.73, relwidth=0.3) # Nós para Milhas Náuticas
self.nos = DoubleVar()
self.tempo2 = DoubleVar()
self.resultado_milhas = StringVar() Label(self.nos_milhas_nauticas, text="Nós:", bg='#dfe3ee', fg='#107db2',
font=("Arial", 14, "bold")).place(relx=0.15, rely=0.05)
Entry(self.nos_milhas_nauticas, textvariable=self.nos, justify='center',
font=("Arial", 14)).place(relx=0.55, rely=0.05, relwidth=0.35) Label(self.nos_milhas_nauticas, text="Tempo (horas):", bg='#dfe3ee', fg='#107db2',
font=("Arial", 14, "bold")).place(relx=0.15, rely=0.25)
Entry(self.nos_milhas_nauticas, textvariable=self.tempo2, justify='center',
font=("Arial", 14)).place(relx=0.55, rely=0.25, relwidth=0.35) Button(self.nos_milhas_nauticas, text="Calcular", bd=2, bg='#107db2', fg='white',
font=('verdana', 12, 'bold'),
command=self.calcular_milhas).place(relx=0.3, rely=0.45, relwidth=0.45, relheight=0.2) Label(self.nos_milhas_nauticas, text="Milhas Náuticas:", bg='#dfe3ee', fg='#107db2',
font=("Arial", 14, "bold")).place(relx=0.05, rely=0.73)
Label(self.nos_milhas_nauticas, textvariable=self.resultado_milhas,
font=("Arial", 14, "bold")).place(relx=0.55, rely=0.73, relwidth=0.3) def calcular_nos(self):
try:
distancia = self.milhas_nauticas.get()
tempo = self.tempo1.get()
if tempo == 0:
self.resultado_nos.set("Erro (tempo = 0)")
else:
velocidade = round(distancia / tempo, 2)
self.resultado_nos.set(f"{velocidade} nós")
except Exception as e:
self.resultado_nos.set("Erro") def calcular_milhas(self):
try:
nos = self.nos.get()
tempo = self.tempo2.get()
distancia = round(nos * tempo, 2)
self.resultado_milhas.set(f"{distancia} milhas")
except Exception as e:
self.resultado_milhas.set("Erro") def quit(self):
self.root.destroy() def menus(self):
menubar = Menu(self.root)
self.root.config(menu=menubar)
filemenu = Menu(menubar, tearoff=0)
menubar.add_cascade(label="Opções", menu=filemenu)
filemenu.add_command(label="Sair", command=self.quit) AppConverter()
0