Mundo em Python

Calcular Rentabilidade

from tkinter import *

# Janela principal
root = Tk()
root.geometry("700x400")
root.resizable(0, 0)
root.config(bg="#103030")
root.title("Calcular Rentabilidade") # Título
titulo = Label(
text="Calcular Rentabilidade",
font=("Arial", 28, "bold"),
bg="#103030",
fg="#49e3e3"
)
titulo.place(relx=0.2, rely=0.05) # Labels dos valores
texto_sub1 = Label(
text="Valor Inicial:",
font=("Arial", 18, "bold"),
bg="#103030",
fg="#49e3e3"
)
texto_sub1.place(relx=0.2, rely=0.25) texto_sub2 = Label(
text="Valor Final:",
font=("Arial", 18, "bold"),
bg="#103030",
fg="#49e3e3"
)
texto_sub2.place(relx=0.2, rely=0.42) # Entradas
Valor_Inicial = StringVar()
Valor_Inicial_entrada = Entry(
textvariable=Valor_Inicial,
font=("Arial", 12, "bold"),
bg="white",
fg="blue",
justify='center'
)
Valor_Inicial_entrada.place(relx=0.55, rely=0.26, relwidth=0.35)
Valor_Inicial_entrada.focus() Valor_Final = StringVar()
Valor_Final_entrada = Entry(
textvariable=Valor_Final,
font=("Arial", 12, "bold"),
bg="white",
fg="blue",
justify='center'
)
Valor_Final_entrada.place(relx=0.55, rely=0.43, relwidth=0.35) # Função para limpar entradas e resultado
def limpar():
Valor_Final_entrada.delete(0, END)
Valor_Inicial_entrada.delete(0, END)
resultado_texto.config(text="") # Função para calcular rentabilidade
def app():
try:
final = float(Valor_Final.get())
inicial = float(Valor_Inicial.get()) if inicial == 0:
resultado_texto.config(text="O valor inicial não pode ser zero.")
return

rentabilidade = ((final - inicial) / inicial) * 100
resultado_texto.config(text=f"Rentabilidade: {rentabilidade:.2f}%") except ValueError:
resultado_texto.config(text="Por favor, insira valores numéricos válidos.") # Botões
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) 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) 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) # Label para exibir resultado
resultado_texto = Label(
text="",
font=("Arial", 14, "bold"),
bg="#103030",
fg="#49e3e3",
anchor="center"
)
resultado_texto.place(relx=0.05, rely=0.8, relwidth=0.9, relheight=0.15) root.mainloop()
0