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("Calculador de Porcentagens")
self.root.configure(background='#1e3743')
self.root.geometry("700x400")
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.valor_percentual_numero = Frame(self.abas)
self.valor_total_valor_percentual = Frame(self.abas)
self.porcentagem_numero_relacao_outro = Frame(self.abas)
self.valor_percentual_numero.configure(background="#dfe3ee")
self.valor_total_valor_percentual.configure(background="#dfe3ee")
self.porcentagem_numero_relacao_outro.configure(background="#dfe3ee")
self.abas.add(self.valor_percentual_numero, text="Valor Percentual de um Número")
self.abas.add(self.valor_total_valor_percentual, text="Valor Total dado um Valor Percentual")
self.abas.add(self.porcentagem_numero_relacao_outro, text="Porcentagem de um Número em Relação a Outro")
self.abas.place(relx=0, rely=0, relwidth=0.98, relheight=0.98)
# Primeira Aba
self.percentagem1 = StringVar()
self.lb_percentagem1 = Label(self.valor_percentual_numero,
text="Porcentagem (%):",
bg='#dfe3ee', fg='#107db2', font=("Arial", "15", "bold"))
self.lb_percentagem1.place(relx=0.2, rely=0.05)
self.percentagem1_entry = Entry(self.valor_percentual_numero,
textvariable=self.percentagem1,
justify='center', font=("Arial", "15", "bold"))
self.percentagem1_entry.place(relx=0.55, rely=0.05, relwidth=0.25)
self.valor1 = StringVar()
self.lb_valor1 = Label(self.valor_percentual_numero,
text="Valor (€):",
bg='#dfe3ee', fg='#107db2', font=("Arial", "15", "bold"))
self.lb_valor1.place(relx=0.2, rely=0.25)
self.valor1_entry = Entry(self.valor_percentual_numero,
textvariable=self.valor1,
justify='center', font=("Arial", "15", "bold"))
self.valor1_entry.place(relx=0.55, rely=0.25, relwidth=0.25)
self.bt_calcular1 = Button(self.valor_percentual_numero, text="Calcular",
bd=2, bg='#107db2', fg='white',
font=('verdana', 12, 'bold'),
command=self.butaoclick1)
self.bt_calcular1.place(relx=0.3, rely=0.45, relwidth=0.45, relheight=0.15)
self.resultado_valor_final = StringVar()
self.resultado1 = Label(self.valor_percentual_numero, textvariable=self.resultado_valor_final,
font=("Arial", "13", "bold"))
self.resultado1.place(relx=0.5, rely=0.71, relwidth=0.45)
self.lb_resultado1 = Label(self.valor_percentual_numero,
text="Valor percentual de um número", font=("Arial", "15", "bold"),
bg='#dfe3ee', fg='#107db2')
self.lb_resultado1.place(relx=0.04, rely=0.7)
# Segunda Aba
self.porcentagem2 = StringVar()
self.lb_porcentagem2 = Label(self.valor_total_valor_percentual,
text="Porcentagem (%):",
bg='#dfe3ee', fg='#107db2', font=("Arial", "15", "bold"))
self.lb_porcentagem2.place(relx=0.15, rely=0.05)
self.porcentagem2_entry = Entry(self.valor_total_valor_percentual,
textvariable=self.porcentagem2,
justify='center', font=("Arial", "15", "bold"))
self.porcentagem2_entry.place(relx=0.45, rely=0.05, relwidth=0.25)
self.valor_parcial2 = StringVar()
self.lb_valor_parcial2 = Label(self.valor_total_valor_percentual,
text="Valor Parcial:",
bg='#dfe3ee', fg='#107db2', font=("Arial", "15", "bold"))
self.lb_valor_parcial2.place(relx=0.15, rely=0.25)
self.valor_parcial2_entry = Entry(self.valor_total_valor_percentual,
textvariable=self.valor_parcial2,
justify='center', font=("Arial", "15", "bold"))
self.valor_parcial2_entry.place(relx=0.45, rely=0.25, relwidth=0.25)
self.bt_calcular2 = Button(self.valor_total_valor_percentual, text="Calcular", bd=2, bg='#107db2',
fg='white',
font=('verdana', 12, 'bold'),
command=self.butaoclick2)
self.bt_calcular2.place(relx=0.3, rely=0.38, relwidth=0.4, relheight=0.15)
self.resultado_valor_parcial = StringVar()
self.resultado2 = Label(self.valor_total_valor_percentual,
textvariable=self.resultado_valor_parcial, font=("Arial", "13", "bold"))
self.resultado2.place(relx=0.5, rely=0.8, relwidth=0.4)
self.lb_resultado2 = Label(self.valor_total_valor_percentual,
text="Resultado", bg='#dfe3ee',
fg='#107db2', font=("Arial", "13", "bold"))
self.lb_resultado2.place(relx=0.05, rely=0.8)
# Terceira Aba
self.valor3 = StringVar()
self.lb_valor3 = Label(self.porcentagem_numero_relacao_outro,
text="Valor Parcial:",
bg='#dfe3ee', fg='#107db2', font=("Arial", "15", "bold"))
self.lb_valor3.place(relx=0.2, rely=0.05)
self.valor3_entry = Entry(self.porcentagem_numero_relacao_outro,
textvariable=self.valor3,
justify='center', font=("Arial", "15", "bold"))
self.valor3_entry.place(relx=0.55, rely=0.05, relwidth=0.25)
self.valor4 = StringVar()
self.lb_valor4 = Label(self.porcentagem_numero_relacao_outro,
text="Valor Total:",
bg='#dfe3ee', fg='#107db2', font=("Arial", "15", "bold"))
self.lb_valor4.place(relx=0.2, rely=0.25)
self.valor4_entry = Entry(self.porcentagem_numero_relacao_outro,
textvariable=self.valor4,
justify='center', font=("Arial", "15", "bold"))
self.valor4_entry.place(relx=0.55, rely=0.25, relwidth=0.25)
self.bt_calcular3 = Button(self.porcentagem_numero_relacao_outro, text="Calcular", bd=2, bg='#107db2',
fg='white',
font=('verdana', 12, 'bold'),
command=self.butaoclick3)
self.bt_calcular3.place(relx=0.3, rely=0.45, relwidth=0.4, relheight=0.15)
self.resultado_relacao = StringVar()
self.resultado3 = Label(self.porcentagem_numero_relacao_outro,
textvariable=self.resultado_relacao, font=("Arial", "13", "bold"))
self.resultado3.place(relx=0.5, rely=0.8, relwidth=0.4)
self.lb_resultado3 = Label(self.porcentagem_numero_relacao_outro,
text="Resultado", bg='#dfe3ee',
fg='#107db2', font=("Arial", "13", "bold"))
self.lb_resultado3.place(relx=0.05, rely=0.8)
def butaoclick1(self):
try:
per1 = float(self.percentagem1.get())
val1 = float(self.valor1.get())
resultado1 = (per1 / 100) * val1
mensagem = f"{per1}% de {val1} é {resultado1:.2f}."
except ValueError:
mensagem = "Valor inválido."
self.resultado_valor_final.set(mensagem)
def butaoclick2(self):
try:
per2 = float(self.porcentagem2.get())
valparcial = float(self.valor_parcial2.get())
total = (valparcial * 100) / per2
mensagem = f"Se {valparcial} é {per2}%, o valor total é {total:.2f}."
except ValueError:
mensagem = "Valor inválido."
self.resultado_valor_parcial.set(mensagem)
def butaoclick3(self):
try:
val3 = float(self.valor3.get())
val4 = float(self.valor4.get())
resultado3 = (val3 / val4) * 100
mensagem = f"{val3} é {resultado3:.2f}% de {val4}."
except ValueError:
mensagem = "Valor inválido."
self.resultado_relacao.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()