from datetime import datetime
from tkinter import *
from tkinter import ttk, messagebox
root = Tk()
root.geometry("600x450")
root.resizable(0, 0)
root.config(bg="#103030")
root.title("Teste de idade futura")
titulo = Label(root, text="Teste de idade futura",
font=("Arial", "28", "bold"), bg="#103030", fg="#49e3e3")
titulo.place(relx=0.15, rely=0.05)
texto_sub1 = Label(root, text="Nome :", font=("Arial", "18", "bold"), bg="#103030", fg="#49e3e3")
texto_sub1.place(relx=0.32, rely=0.2)
texto_sub2 = Label(root, text="Quantos anos tem? :", font=("Arial", "18", "bold"), bg="#103030", fg="#49e3e3")
texto_sub2.place(relx=0.1, rely=0.35)
texto_sub3 = Label(root, text="Ano quer saber sua idade? :", font=("Arial", "18", "bold"), bg="#103030", fg="#49e3e3")
texto_sub3.place(relx=0.05, rely=0.5)
Nome = StringVar()
Nome_entrada = Entry(root, textvariable=Nome, font=("Arial", "12", "bold"), bg="white", fg="blue", justify='center')
Nome_entrada.place(relx=0.55, rely=0.21, relwidth=0.35)
Nome_entrada.focus()
idade = StringVar()
idade_entrada = Entry(root, textvariable=idade, font=("Arial", "12", "bold"), bg="white", fg="blue", justify='center')
idade_entrada.place(relx=0.55, rely=0.36, relwidth=0.35)
ano = StringVar()
ano_entrada = Entry(root, textvariable=ano, font=("Arial", "12", "bold"), bg="white", fg="blue", justify='center')
ano_entrada.place(relx=0.58, rely=0.51, relwidth=0.35)
def limpar():
ano_entrada.delete(0, END)
idade_entrada.delete(0, END)
Nome_entrada.delete(0, END)
resultado_texto.config(text="")
def app(event=None):
try:
if not Nome.get() or not idade.get() or not ano.get():
raise ValueError("Todos os campos devem ser preenchidos!")
i = int(idade.get())
a = int(ano.get())
n = Nome.get()
ano_atual = datetime.now().year
idade_futura = i + (a - ano_atual)
if idade_futura < 0:
mensagem = f"{n},\n em {a} você ainda não havia nascido!"
elif a > ano_atual:
mensagem = f"{n},\n em {a} você terá {idade_futura} anos."
elif a == ano_atual:
mensagem = f"{n},\n este ano você tem {i} anos."
else:
mensagem = f"{n},\n em {a} você tinha {idade_futura} anos."
resultado_texto.config(text=mensagem, fg="green")
except ValueError as e:
resultado_texto.config(text=str(e), fg="red")
but1 = Button(root, text="Mostrar", 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(root, 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(root, 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(root, text="", font=("Arial", 12, "bold"), bg="#cfe2f3", wraplength=500)
resultado_texto.place(relx=0.05, rely=0.8, relwidth=0.9, relheight=0.15)
root.bind('<Return>', app)
root.mainloop()
Comentários
Enviar um comentário