from tkinter import *
from tkinter import messagebox
from PIL import Image, ImageDraw, ImageFont
import random
import string
from datetime import datetime
import re
import os
# Função para gerar código aleatório
def gerar_codigo():
return ''.join(random.choices(string.ascii_uppercase + string.digits, k=10))
# Criação da interface gráfica
root = Tk()
root.geometry("700x400")
root.resizable(0, 0)
root.config(bg="#103030")
root.title("Vale Cupom para o Dia da Mãe")
# Título
titulo = Label(text="Vale Cupom para o Dia da Mãe",
font=("Arial", 28, "bold"), bg="#103030", fg="#49e3e3")
titulo.place(relx=0.1, rely=0.05)
# Campo de entrada para o nome
texto_sub1 = Label(text="Nome da Mãe:",
font=("Arial", 18, "bold"), bg="#103030", fg="#49e3e3")
texto_sub1.place(relx=0.2, rely=0.32)
nome_mae = StringVar()
nome_mae_entrada = Entry(textvariable=nome_mae,
font=("Arial", 12, "bold"),
bg="white", fg="blue", justify='center')
nome_mae_entrada.place(relx=0.48, rely=0.33, relwidth=0.35)
# Função para limpar campo
def limpar():
nome_mae_entrada.delete(0, END)
# Função para criar a imagem do cupom
def criar_cupom_imagem():
nome = nome_mae.get().strip()
if not nome:
messagebox.showerror("Erro", "Por favor, insira o nome da mãe.")
return
try:
img = Image.new('RGB', (500, 300), color=(255, 255, 255))
except Exception as e:
messagebox.showerror("Erro com PIL", f"Erro ao criar imagem: {e}")
return
draw = ImageDraw.Draw(img)
# Tenta carregar a fonte Arial, ou usa padrão
try:
fonte = ImageFont.truetype("arial.ttf", 20)
except IOError:
fonte = ImageFont.load_default()
codigo = gerar_codigo()
data_atual = datetime.now().strftime("%d/%m/%Y")
draw.text((20, 20), "Vale-Cupom Especial de Dia da Mãe", font=fonte, fill=(0, 0, 0))
draw.text((20, 60), "Este cupom é válido para uma actividade especial\n ou presente surpresa!", font=fonte, fill=(0, 0, 0))
draw.text((20, 100), f"Presenteado: {nome}", font=fonte, fill=(0, 0, 0))
draw.text((20, 140), f"Código do cupom: {codigo}", font=fonte, fill=(0, 0, 0))
draw.text((20, 180), f"Data de emissão: {data_atual}", font=fonte, fill=(0, 0, 0))
draw.text((20, 220), "Aproveite com muito amor e carinho!", font=fonte, fill=(0, 0, 0))
# Sanitiza o nome para o nome do arquivo
nome_arquivo = re.sub(r'[\\/*?:"<>|]', "_", nome)
caminho_arquivo = f"cupom_{nome_arquivo}.png"
try:
img.save(caminho_arquivo)
os.startfile(caminho_arquivo) # Apenas no Windows
except Exception as e:
messagebox.showinfo("Cupom Criado", f"Cupom salvo como: {caminho_arquivo}\nErro ao abrir imagem: {e}")
# Botões
but1 = Button(text="Fazer", bd=2, bg='#107db2', fg='white',
font=('verdana', 12, 'bold'), command=criar_cupom_imagem)
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)
root.mainloop()