Mundo em Python

Valor Total de um Valor Parcial

from tkinter import *

root = Tk()
root.geometry("700x420")
root.resizable(0, 0)
root.config(bg="#103030")
root.title("Valor Total de um Valor Parcial") def formatar_milhar(valor):
return f"{valor:,.2f}".replace(",", " ") def formatar_grande(valor):
if valor >= 1_000_000_000:
return f"{valor/1_000_000_000:.2f} mil milhões"
elif valor >= 1_000_000:
return f"{valor/1_000_000:.2f} milhões"
elif valor >= 1_000:
return f"{valor/1_000:.2f} mil"
else:
return f"{valor:.2f}"


titulo = Label(
root,
text="Valor Total de um Valor Parcial",
font=("Arial", 26, "bold"),
bg="#103030",
fg="#49e3e3"
)
titulo.place(relx=0.08, rely=0.05) Label(root,text="Valor parcial:",
font=("Arial",20,"bold"),
bg="#103030",fg="#49e3e3").place(relx=0.2,rely=0.28) Label(root,text="Valor da Percentagem:",
font=("Arial",20,"bold"),
bg="#103030",fg="#49e3e3").place(relx=0.03,rely=0.42) Valor_parcial = StringVar()
Entry(root,textvariable=Valor_parcial,
font=("Arial",12,"bold"),
bg="white",fg="blue",
justify="center"
).place(relx=0.5,rely=0.3,relwidth=0.43) Valor_Percentagem = StringVar()
Entry(root,textvariable=Valor_Percentagem,
font=("Arial",12,"bold"),
bg="white",fg="blue",
justify="center"
).place(relx=0.5,rely=0.44,relwidth=0.43) def limpar():
Valor_parcial.set("")
Valor_Percentagem.set("")
resultado_texto.config(text="") def calcular():
try:
parcial = float(Valor_parcial.get().replace(",", "."))
percentagem = float(Valor_Percentagem.get().replace(",", ".")) if percentagem == 0:
resultado_texto.config(text="Percentagem não pode ser zero.")
return

total = parcial / (percentagem / 100) parcial_txt = formatar_grande(parcial)
total_txt = formatar_grande(total) explicacao = (
f"Se {parcial_txt} correspondem a {percentagem}%,\n"
f"o total (100%) é {total_txt}.\n\n"
) resultado_texto.config(text=explicacao) except ValueError:
resultado_texto.config(
text="Por favor, insira valores numéricos válidos."
)
Button(root,text="Calcular",
bd=2,bg='#107db2',fg='white',
font=('verdana',12,'bold'),
command=calcular
).place(relx=0.1,rely=0.62,relwidth=0.25,relheight=0.1) Button(root,text="Limpar",
bd=2,bg='#107db2',fg='white',
font=('verdana',12,'bold'),
command=limpar
).place(relx=0.4,rely=0.62,relwidth=0.25,relheight=0.1) Button(root,text="Sair",
bd=2,bg='#107db2',fg='white',
font=('verdana',12,'bold'),
command=root.destroy
).place(relx=0.7,rely=0.62,relwidth=0.25,relheight=0.1) resultado_texto = Label(
root,
text="",
font=("Arial",11,"bold"),
bg="#cfe2f3",
justify="left",
anchor="w"
)
resultado_texto.place(relx=0.05,rely=0.78,relwidth=0.9,relheight=0.2)
root.mainloop()
0