Mundo em Python

Fórmula do Amor (usando Tkinter)

from tkinter import *
root = Tk()
root.geometry("500x600")
root.resizable(0, 0)
root.config(bg="#103030")
root.title("Fórmula do Amor") titulo = Label(text="Fórmula do Amor",
font=("Arial", "28", "bold"), bg="#103030", fg="#49e3e3")
titulo.place(relx=0.18, rely=0.05) # Labels
texto_sub1 = Label(text="Respeito (0-10) :", font=("Arial", "18", "bold"), bg="#103030", fg="#49e3e3")
texto_sub1.place(relx=0.15, rely=0.15) texto_sub2 = Label(text="Comunicação (0-10) :", font=("Arial", "18", "bold"), bg="#103030", fg="#49e3e3")
texto_sub2.place(relx=0.05, rely=0.25) texto_sub3 = Label(text="Confiança (0-10) :", font=("Arial", "18", "bold"), bg="#103030", fg="#49e3e3")
texto_sub3.place(relx=0.13, rely=0.35) texto_sub4 = Label(text="Empatia (0-10) :", font=("Arial", "18", "bold"), bg="#103030", fg="#49e3e3")
texto_sub4.place(relx=0.16, rely=0.45) texto_sub5 = Label(text="Cumplicidade (0-10) :", font=("Arial", "18", "bold"), bg="#103030", fg="#49e3e3")
texto_sub5.place(relx=0.03, rely=0.55) texto_sub6 = Label(text="Tempo (em anos) :", font=("Arial", "18", "bold"), bg="#103030", fg="#49e3e3")
texto_sub6.place(relx=0.08, rely=0.65) # Entradas
Respeito = StringVar()
Respeito_entrada = Entry(textvariable=Respeito, font=("Arial", "12", "bold"),
bg="white", fg="blue", justify='center')
Respeito_entrada.place(relx=0.55, rely=0.16, relwidth=0.35) Comunicação = StringVar()
Comunicação_entrada = Entry(textvariable=Comunicação, font=("Arial", "12", "bold"),
bg="white", fg="blue", justify='center')
Comunicação_entrada.place(relx=0.55, rely=0.26, relwidth=0.35) Confiança = StringVar()
Confiança_entrada = Entry(textvariable=Confiança, font=("Arial", "12", "bold"),
bg="white", fg="blue", justify='center')
Confiança_entrada.place(relx=0.55, rely=0.36, relwidth=0.35) Empatia = StringVar()
Empatia_entrada = Entry(textvariable=Empatia, font=("Arial", "12", "bold"),
bg="white", fg="blue", justify='center')
Empatia_entrada.place(relx=0.55, rely=0.46, relwidth=0.35) Cumplicidade = StringVar()
Cumplicidade_entrada = Entry(textvariable=Cumplicidade, font=("Arial", "12", "bold"),
bg="white", fg="blue", justify='center')
Cumplicidade_entrada.place(relx=0.55, rely=0.56, relwidth=0.35) Tempo = StringVar()
Tempo_entrada = Entry(textvariable=Tempo, font=("Arial", "12", "bold"),
bg="white", fg="blue", justify='center')
Tempo_entrada.place(relx=0.55, rely=0.66, relwidth=0.35) # Funções
def limpar():
Respeito_entrada.delete(0, END)
Comunicação_entrada.delete(0, END)
Confiança_entrada.delete(0, END)
Empatia_entrada.delete(0, END)
Cumplicidade_entrada.delete(0, END)
Tempo_entrada.delete(0, END)
resultado_texto.config(text="") # Limpa o texto do resultado


def app():
try:
# Coleta de dados
respeito = int(Respeito.get())
comunicacao = int(Comunicação.get())
confianca = int(Confiança.get())
empatia = int(Empatia.get())
cumplicidade = int(Cumplicidade.get())
tempo = float(Tempo.get()) # Validação
if not all(0 <= f <= 10 for f in [respeito, comunicacao, confianca, empatia, cumplicidade]):
raise ValueError("Os valores dos fatores devem estar entre 0 e 10.")
if tempo < 0:
raise ValueError("O tempo deve ser positivo.") # Cálculo do amor
nivel_de_amor = (respeito + comunicacao + confianca + empatia + cumplicidade) * tempo # Exibição do resultado
resultado_texto.config(text=f"Nível de Amor:\n{nivel_de_amor:.2f}")
except ValueError as e:
resultado_texto.config(text=f"Erro: {str(e)}")
# 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.75, 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.75, 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.75, relwidth=0.25, relheight=0.1) resultado_texto = Label(text="", font=("Arial", 12, "bold"), bg="#cfe2f3", justify="center")
resultado_texto.place(relx=0.05, rely=0.87, relwidth=0.9, relheight=0.1)
root.mainloop()
0