Mundo em Python

Fórmula de Heron

from tkinter import *
import math
root = Tk()
root.geometry("400x500")
root.resizable(0, 0)
root.config(bg="#103030")
root.title("Fórmula de Heron ")
titulo = Label(text="Fórmula de Heron",
font=("Arial", "28", "bold"), bg="#103030", fg="#49e3e3")
titulo.place(relx=0.08, rely=0.05) texto_sub1 = Label(text="A : ",
font=("Arial", "18", "bold"), bg="#103030", fg="#49e3e3")
texto_sub1.place(relx=0.35, rely=0.23)
texto_sub2 = Label(text="B : ",
font=("Arial", "18", "bold"), bg="#103030", fg="#49e3e3")
texto_sub2.place(relx=0.35, rely=0.35) texto_sub3 = Label(text="C : ",
font=("Arial", "18", "bold"), bg="#103030", fg="#49e3e3")
texto_sub3.place(relx=0.35, rely=0.48) A = StringVar()
A_entrada = Entry(textvariable=A,
font=("Arial", "12", "bold"),
bg="white", fg="blue", justify='center')
A_entrada.place(relx=0.55, rely=0.24, relwidth=0.25)
A_entrada.focus() B = StringVar()
B_entrada = Entry(textvariable=B,
font=("Arial", "12", "bold"),
bg="white", fg="blue", justify='center')
B_entrada.place(relx=0.55, rely=0.36, relwidth=0.25) C = StringVar()
C_entrada = Entry(textvariable=C,
font=("Arial", "12", "bold"),
bg="white", fg="blue", justify='center')
C_entrada.place(relx=0.55, rely=0.49, relwidth=0.25) def limpar():
C_entrada.delete(0, END)
B_entrada.delete(0, END)
A_entrada.delete(0, END)
resultado_texto.config(text="")
def app():
try:
a = float(A.get())
b = float(B.get())
c = float(C.get()) # Check if the inputs can form a triangle
if a <= 0 or b <= 0 or c <= 0:
resultado_texto.config(text="Os valores devem ser positivos!")
return
if a + b <= c or a + c <= b or b + c <= a:
resultado_texto.config(text="Os lados não formam um triângulo válido!")
return

s = (a + b + c) / 2
area = math.sqrt(s * (s - a) * (s - b) * (s - c))
mensagem = f"A área do triângulo é: {round(area, 2)}"
resultado_texto.config(text=mensagem) except ValueError:
resultado_texto.config(text="Valor digitado inválido!")
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_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