Mundo em Python

Função de Aniversário (usando Tkinter)

from tkinter import *
root = Tk()
root.geometry("500x250")
root.resizable(0, 0)
root.config(bg="#2a4b80")
root.title("Feliz a aniversariante") titulo = Label(text="Feliz a Aniversariante",
font=("Arial", "33", "bold"),bg="#2a4b80",fg="#e4e7ed")
titulo.place(relx=0.05, rely=0.05)
texto_sub1 = Label(text="Nome:",
font=("Arial", "15", "bold"),bg="#2a4b80",fg="#e4e7ed")
texto_sub1.place(relx=0.2, rely=0.3) nome = StringVar()
nome_entrada = Entry(textvariable=nome,
font=("Arial", "12", "bold"),
bg="white", fg="blue", justify='center')
nome_entrada.place(relx=0.4, rely=0.3, relwidth=0.55)
def limpar():
nome_entrada.delete(0, END)
resultado.set("")
def app():
n =nome.get()
mensagem = f"Feliz Aniversário, {nome}!"
resultado.set(mensagem) but1 = Button(text="Mostrar", bd=2, bg='#107db2', fg='white',
font=('verdana', 12, 'bold'), command=app)
but1.place(relx=0.05, rely=0.5, relwidth=0.25, relheight=0.13)
but_limpar = Button(text="Limpar", bd=2, bg='#107db2', fg='white',
font=('verdana', 12, 'bold'), command=limpar)
but_limpar.place(relx=0.35, rely=0.5, relwidth=0.25, relheight=0.13) but_sair = Button(text="Sair", bd=2, bg='#107db2', fg='white',
font=('verdana', 12, 'bold'), command=root.destroy)
but_sair.place(relx=0.65, rely=0.5, relwidth=0.25, relheight=0.13)
resultado = StringVar()
resultado_texto = Label(textvariable=resultado,
font=("Arial", 12, "bold"), bg="#cfe2f3")
resultado_texto.place(relx=0.05, rely=0.75, relwidth=0.9,relheight=0.15)
root.mainloop()

# Outra Versão


from tkinter import *


root = Tk()
root.geometry("500x500")
root.resizable(0, 0)
root.config(bg="#103030")
root.title("Função Aniversário") titulo = Label(text="Função Aniversário",
font=("Arial", "38", "bold"), bg="#103030", fg="#49e3e3")
titulo.place(relx=0.05, rely=0.05) sub1 = Label(text="Qual é o seu nome? ", font=("Arial", "18", "bold"),
bg="#103030", fg="#49e3e3")
sub1.place(relx=0.05, rely=0.25) nome_aniversariante = StringVar()
nome_aniversariante_entrada = Entry(textvariable=nome_aniversariante,
font=("Arial", "12", "bold"),
bg="white", fg="blue", justify='center')
nome_aniversariante_entrada.place(relx=0.53, rely=0.26, relwidth=0.45)
nome_aniversariante_entrada.focus() sub2 = Label(text="Quantos anos você está fazendo hoje? ", font=("Arial", "18", "bold"), bg="#103030", fg="#49e3e3")
sub2.place(relx=0.05, rely=0.35)
idade = StringVar()
idade_entrada = Entry(textvariable=idade, font=("Arial", "12", "bold"),
bg="white", fg="blue", justify='center')
idade_entrada.place(relx=0.4, rely=0.45, relwidth=0.25)
idade_entrada.focus()
def limpar():
idade_entrada.delete(0,END)
nome_aniversariante_entrada.delete(0,END)
resultado_texto.config(text="") def app():
try:
n = nome_aniversariante.get()
i = int(idade.get()) if i <= 0:
mensagem = f"Parabéns, {n}!\nEspero que este seja o primeiro de muitos\n" \
f" anos maravilhosos pela frente!"
elif i == 1:
mensagem = f"Parabéns, {n}!\nFeliz primeiro aniversário!\n" \
f" Que este seja o início de uma jornada incrível!"
elif i < 13:
mensagem = f"Parabéns, {n}!\nFeliz {i}º aniversário!\n" \
f" Que você tenha um dia cheio de alegria e diversão!"
elif i < 18:
mensagem = f"Parabéns, {n}!\nFeliz {i}º aniversário!\n" \
f" Que este seja um ano emocionante repleto de descobertas!"
elif i < 50:
mensagem = f"Parabéns, {n}!\nFeliz {i}º aniversário!\n" \
f" Que este novo ciclo traga muitas realizações\n" \
f" e felicidades!"
else:
mensagem = f"Parabéns, {n}!\nFeliz {i}º aniversário!" \
f" Que você continue celebrando\n" \
f" a vida com a mesma energia e vitalidade!"

resultado_texto.config(text=mensagem)
except ValueError:
resultado_texto.config(text="Erro de digitação!!") but1 = Button(text="Mostrar", bd=2, bg='#107db2', fg='white',
font=('verdana', 12, 'bold'), command=app)
but1.place(relx=0.1, rely=0.55, 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.55, 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.55, relwidth=0.25, relheight=0.1) resultado_texto = Label(font=("Arial", 12, "bold"), bg="#cfe2f3")
resultado_texto.place(relx=0.05, rely=0.7, relwidth=0.9, relheight=0.25)
root.mainloop()
0