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 Barril ↔ Litros")
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.Barril_para_Litros = Frame(self.abas, bg="#dfe3ee")
self.Litros_para_Barril = Frame(self.abas, bg="#dfe3ee")
self.abas.add(self.Barril_para_Litros, text="Barril → Litros")
self.abas.add(self.Litros_para_Barril, text="Litros → Barril")
self.abas.place(relx=0, rely=0, relwidth=0.98, relheight=0.98)
# ----------- ABA 1: Barril → Litros -----------
self.Barril = StringVar()
Label(self.Barril_para_Litros,
text="Número de Barris",
bg='#dfe3ee', fg='#107db2',
font=("Arial", 15, "bold")
).place(relx=0.05, rely=0.1)
Entry(self.Barril_para_Litros,
textvariable=self.Barril,
justify='center',
font=("Arial", 15, "bold")
).place(relx=0.45, rely=0.1, relwidth=0.45)
Button(self.Barril_para_Litros,
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_litros = StringVar()
Label(self.Barril_para_Litros,
text="Resultado em Litros",
bg='#dfe3ee', fg='#107db2',
font=("Arial", 15, "bold")
).place(relx=0.2, rely=0.55)
Label(self.Barril_para_Litros,
textvariable=self.resultado_litros,
font=("Arial", 15, "bold"),
bg='#dfe3ee'
).place(relx=0.05, rely=0.7, relwidth=0.9)
# ----------- ABA 2: Litros → Barril -----------
self.litros = StringVar()
Label(self.Litros_para_Barril,
text="Número de Litros",
bg='#dfe3ee', fg='#107db2',
font=("Arial", 15, "bold")
).place(relx=0.05, rely=0.1)
Entry(self.Litros_para_Barril,
textvariable=self.litros,
justify='center',
font=("Arial", 15, "bold")
).place(relx=0.45, rely=0.1, relwidth=0.45)
Button(self.Litros_para_Barril,
text="Calcular",
bd=2, bg='#107db2', fg='white',
font=('verdana', 12, 'bold'),
command=self.butaoclick2
).place(relx=0.3, rely=0.3, relwidth=0.45, relheight=0.15)
self.resultado_barris = StringVar()
Label(self.Litros_para_Barril,
text="Resultado em Barris",
bg='#dfe3ee', fg='#107db2',
font=("Arial", 15, "bold")
).place(relx=0.2, rely=0.55)
Label(self.Litros_para_Barril,
textvariable=self.resultado_barris,
font=("Arial", 15, "bold"),
bg='#dfe3ee'
).place(relx=0.05, rely=0.7, relwidth=0.9)
# ----------- FUNÇÕES -----------
def butaoclick1(self):
try:
barris = float(self.Barril.get())
litros = barris * 159
mensagem = f"{litros:.2f} litros"
except ValueError:
mensagem = "Valor inválido."
self.resultado_litros.set(mensagem)
def butaoclick2(self):
try:
litros = float(self.litros.get())
barris = litros / 159
mensagem = f"{barris:.4f} barris"
except ValueError:
mensagem = "Valor inválido."
self.resultado_barris.set(mensagem)
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()