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("Conversor de ha ↔ km²")
self.root.configure(background='#1e3743')
self.root.geometry("500x400")
self.root.resizable(0, 0)
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.hectares_para_km = Frame(self.abas, bg="#dfe3ee")
self.km_para_hectares = Frame(self.abas, bg="#dfe3ee")
self.abas.add(self.hectares_para_km, text="ha → km²")
self.abas.add(self.km_para_hectares, text="km² → ha")
self.abas.place(relx=0, rely=0, relwidth=0.98, relheight=0.98)
# ----------- ABA 1: ha → km² -----------
self.hectares = StringVar()
Label(self.hectares_para_km,
text="Hectares (ha)",
bg='#dfe3ee', fg='#107db2',
font=("Arial", 15, "bold")
).place(relx=0.05, rely=0.1)
Entry(self.hectares_para_km,
textvariable=self.hectares,
justify='center',
font=("Arial", 15, "bold")
).place(relx=0.45, rely=0.1, relwidth=0.45)
Button(self.hectares_para_km,
text="Calcular",
bd=2, bg='#107db2', fg='white',
font=('verdana', 12, 'bold'),
command=self.butaoclick1
).place(relx=0.3, rely=0.3, relwidth=0.45, relheight=0.15)
self.resultado_km = StringVar()
Label(self.hectares_para_km,
text="Quilómetros quadrados (km²)",
bg='#dfe3ee', fg='#107db2',
font=("Arial", 15, "bold")
).place(relx=0.1, rely=0.55)
Label(self.hectares_para_km,
textvariable=self.resultado_km,
font=("Arial", 15, "bold"),
bg='#dfe3ee'
).place(relx=0.05, rely=0.7, relwidth=0.9)
# ----------- ABA 2: km² → ha -----------
self.km2 = StringVar()
Label(self.km_para_hectares,
text="Quilómetros quadrados (km²)",
bg='#dfe3ee', fg='#107db2',
font=("Arial", 15, "bold")
).place(relx=0.15, rely=0.1)
Entry(self.km_para_hectares,
textvariable=self.km2,
justify='center',
font=("Arial", 15, "bold")
).place(relx=0.25, rely=0.25, relwidth=0.45)
Button(self.km_para_hectares,
text="Calcular",
bd=2, bg='#107db2', fg='white',
font=('verdana', 12, 'bold'),
command=self.butaoclick2
).place(relx=0.25, rely=0.4, relwidth=0.45, relheight=0.15)
self.resultado_ha = StringVar()
Label(self.km_para_hectares,
text="Hectares (ha)",
bg='#dfe3ee', fg='#107db2',
font=("Arial", 15, "bold")
).place(relx=0.3, rely=0.55)
Label(self.km_para_hectares,
textvariable=self.resultado_ha,
font=("Arial", 15, "bold"),
bg='#dfe3ee'
).place(relx=0.05, rely=0.7, relwidth=0.9)
# ----------- FUNÇÕES DOS BOTÕES -----------
def butaoclick1(self):
try:
ha = float(self.hectares.get())
km2 = ha / 100 # 1 ha = 0.01 km²
mensagem = f"{round(km2, 3)}"
except ValueError:
mensagem = "Valor inválido."
self.resultado_km.set(mensagem)
def butaoclick2(self):
try:
km2 = float(self.km2.get())
ha = km2 * 100 # 1 km² = 100 ha
mensagem = f"{round(ha, 3)}"
except ValueError:
mensagem = "Valor inválido."
self.resultado_ha.set(mensagem)
# ----------- MENU -----------
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()