Mundo em Python

Calcular o valor por m²

from tkinter import *

root = Tk()
root.geometry("650x400")
root.resizable(0, 0)
root.config(bg="#103030")
root.title("Calcular o valor por m²") # Título
titulo = Label(
text="Calcular o valor por m²",
font=("Arial", 28, "bold"),
bg="#103030",
fg="#49e3e3"
)
titulo.place(relx=0.18, rely=0.05) # Texto
texto_sub1 = Label(
text="Total de m² :",
font=("Arial", 18, "bold"),
bg="#103030",
fg="#49e3e3"
)
texto_sub1.place(relx=0.26, rely=0.32) texto_sub2 = Label(
text="Preço Total da habitação :",
font=("Arial", 18, "bold"),
bg="#103030",
fg="#49e3e3"
)
texto_sub2.place(relx=0.05, rely=0.5) # Variáveis
Total_m = StringVar()
Total_habitacao = StringVar() # Entrada m²
Total_m_entrada = Entry(
textvariable=Total_m,
font=("Arial", 12, "bold"),
bg="white",
fg="blue",
justify='center'
)
Total_m_entrada.place(relx=0.57, rely=0.33, relwidth=0.35)
Total_m_entrada.focus() # Entrada preço habitação
Total_habitacao_entrada = Entry(
textvariable=Total_habitacao,
font=("Arial", 12, "bold"),
bg="white",
fg="blue",
justify='center'
)
Total_habitacao_entrada.place(relx=0.57, rely=0.51, relwidth=0.35) # Função limpar
def limpar():
Total_habitacao_entrada.delete(0, END)
Total_m_entrada.delete(0, END)
resultado_texto.config(text="") # Função calcular
def app():
try:
custo_habitacao = float(Total_habitacao.get())
total_m = float(Total_m.get()) if total_m == 0:
resultado_texto.config(text="Os m² não podem ser 0.")
return

por_metro_quadrado = custo_habitacao / total_m
mensagem = f"A habitação por m²: {round(por_metro_quadrado,2)} €"

resultado_texto.config(text=mensagem) except ValueError:
resultado_texto.config(text="Por favor, insira valores numéricos válidos.") # Botão calcular
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) # Botão limpar
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) # Botão sair
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) # Resultado
resultado_texto = Label(
text="",
font=("Arial", 12, "bold"),
bg="#cfe2f3"
)
resultado_texto.place(relx=0.05, rely=0.8, relwidth=0.9, relheight=0.15) root.mainloop()
0