import customtkinter as ctk
from tkinter import ttk, messagebox
from PIL import Image, ImageTk
from usuarios import UsuariosDB
class Registro:
def __init__(self, ventana, rol,volver_login=None):
[Link] = ventana
[Link] = rol
[Link] = UsuariosDB()
self.volver_login = volver_login
[Link]("Registro de usuario")
[Link]("450x620+350+10")
[Link](False, False)
[Link]("imagenes/[Link]")
[Link]([Link], text=f"REGISTRO DE {[Link]()}",
font=("Bradley Hand ITC", 18, "bold")).pack(pady=10)
img = [Link]("imagenes/[Link]")
[Link] = [Link](light_image=img, dark_image=img, size=(140, 55))
[Link](ventana, image=[Link], text="").pack(pady=5)
frame = [Link](ventana)
[Link](padx=20, pady=10, fill="x")
[Link] = self._campo(frame, "Usuario:", 0)
[Link] = self._campo(frame, "Nombre:", 1)
[Link] = self._campo(frame, "Apellidos:", 2)
[Link](frame, text="Sexo:", font=("Bradley Hand ITC", 18,
"bold")).grid(row=3, column=0, sticky="w", pady=6)
[Link] = [Link](frame, values=["Masculino", "Femenino"],
state="readonly", width=22)
[Link](row=3, column=1, pady=6)
[Link] = self._campo(frame, "Correo:", 4)
[Link] = self._campo(frame, "Contraseña:", 5, True)
[Link] = self._campo(frame, "Repetir contraseña:", 6, True)
frame_p = [Link](ventana)
frame_p.pack(padx=20, pady=10, fill="x")
[Link] = [Link](frame_p, values=[
"¿Nombre de tu primera mascota?",
"¿Lugar dónde fuiste al colegio?",
"¿En qué ciudad naciste?",
"¿Cómo se llama tu equipo favorito?"
], state="readonly", width=38)
[Link](0)
[Link](pady=5)
[Link] = [Link](frame_p, width=280)
[Link](pady=5)
# Iconos y botones
self.icon_registrar = [Link]([Link]("imagenes/[Link]"), size=(18,
18))
self.icon_limpiar = [Link]([Link]("imagenes/[Link]"),
size=(18, 18))
self.icon_cerrar = [Link]([Link]("imagenes/[Link]"),
size=(18, 18))
botones = [Link](ventana)
[Link](pady=15)
[Link](botones, text="REGISTRAR", image=self.icon_registrar,
fg_color="#4CAF50", hover_color="#45A049",
command=self.registrar_usuario).grid(row=2, column=0, padx=6)
[Link](botones, text="LIMPIAR", image=self.icon_limpiar,
fg_color="#2196F3", hover_color="#1976D2",
command=[Link]).grid(row=2, column=1, padx=6)
[Link](botones, text="CERRAR", image=self.icon_cerrar,
fg_color="#F44336", hover_color="#D32F2F",
command=lambda: self._cerrar_registro()).grid(row=2,
column=2, padx=2)
# ================= CAMPOS =================
def _campo(self, frame, texto, fila, oculto=False):
[Link](frame, text=texto, font=("Bradley Hand ITC", 18,
"bold")).grid(row=fila, column=0, sticky="w", pady=6)
entry = [Link](frame, width=180, show="*" if oculto else "")
[Link](row=fila, column=1, pady=6)
return entry
# ================= VALIDAR FORMULARIO =================
def validar_formulario(self):
campos = [[Link], [Link], [Link],
[Link], [Link], [Link], [Link],
[Link]]
if not all([[Link]() if hasattr(c, 'get') else True for c in campos]):
[Link]("Registro", "Complete todos los campos")
return False
if [Link]() != [Link]():
[Link]("Registro", "Las contraseñas no coinciden")
return False
if [Link].buscar_por_usuario([Link]()):
[Link]("Registro", "El usuario ya existe")
return False
return True
# ================= REGISTRAR USUARIO =================
def registrar_usuario(self):
if not self.validar_formulario():
return
exito, msg = [Link].insertar_usuario(
[Link](),
[Link](),
[Link],
[Link](),
[Link]()
)
if exito:
[Link]("Registro", msg)
self._cerrar_registro() # Cierra el Toplevel y vuelve al login
if self.volver_login:
self.volver_login() #vuelve al login
else:
[Link]("Registro", msg)
# ================= CERRAR / VOLVER LOGIN =================
def _cerrar_registro(self):
"""Cerrar registro y volver al login"""
if [Link]:
[Link]() # Solo cierra el Toplevel de registro
# ================= LIMPIAR CAMPOS =================
def limpiar(self):
for campo in [[Link], [Link], [Link],
[Link], [Link], [Link], [Link]]:
[Link](0, "end")
[Link]("")
[Link](0)