Mundo em Python

Índice Cintura-Altura

Índice Cintura-Altura
from tkinter import *

root = Tk()
root.geometry("700x400")
root.resizable(0, 0)
root.config(bg="#103030")
root.title("Índice Cintura-Altura")

# Título
titulo = Label(
text="Índice Cintura-Altura",
font=("Arial", 28, "bold"),
bg="#103030",
fg="#49e3e3"
)
titulo.place(relx=0.25, rely=0.05)

# Labels
texto_sub1 = Label(
text="Perímetro da cintura (cm):",
font=("Arial", 18, "bold"),
bg="#103030",
fg="#49e3e3"
)
texto_sub1.place(relx=0.08, rely=0.3)

texto_sub2 = Label(
text="Altura (cm):",
font=("Arial", 18, "bold"),
bg="#103030",
fg="#49e3e3"
)
texto_sub2.place(relx=0.32, rely=0.45)

# Variáveis
perimetro_cintura = StringVar()
altura = StringVar()

# Entradas
perimetro_entry = Entry(
textvariable=perimetro_cintura,
font=("Arial", 12, "bold"),
bg="white",
fg="blue",
justify='center'
)
perimetro_entry.place(relx=0.55, rely=0.31, relwidth=0.35)
perimetro_entry.focus()

altura_entry = Entry(
textvariable=altura,
font=("Arial", 12, "bold"),
bg="white",
fg="blue",
justify='center'
)
altura_entry.place(relx=0.55, rely=0.46, relwidth=0.35)


# Função limpar
def limpar():
altura_entry.delete(0, END)
perimetro_entry.delete(0, END)
resultado_texto.config(text="", fg="black")


# Função calcular
def app():
try:
h = float(altura.get())
p = float(perimetro_cintura.get())

if h <= 0 or p <= 0:
resultado_texto.config(
text="Altura e perímetro devem ser maiores que 0.",
fg="black"
)
return

ica = p / h

if ica < 0.5:
mensagem = f"ICA = {ica:.2f} | Risco baixo (saudável)"
cor = "green"

elif ica < 0.6:
mensagem = f"ICA = {ica:.2f} | Risco aumentado"
cor = "orange"

else:
mensagem = f"ICA = {ica:.2f} | Risco elevado"
cor = "red"

resultado_texto.config(text=mensagem, fg=cor)

except ValueError:
resultado_texto.config(
text="Valor introduzido inválido.",
fg="black"
)


# 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)

# 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