202 lines
7.9 KiB
Python
202 lines
7.9 KiB
Python
import tkinter as tk
|
|
from tkinter import messagebox
|
|
from app.panel_derecho.chat import Chat
|
|
import threading
|
|
import time
|
|
import pygame # Para reproducir música
|
|
|
|
class PanelDerecho:
|
|
def __init__(self, frame):
|
|
self.usuario_email = None
|
|
self.usuario_password = None
|
|
# Inicializar el cliente de chat
|
|
self.chat_client = Chat()
|
|
try:
|
|
self.chat_client.conectar_al_servidor()
|
|
except Exception as e:
|
|
messagebox.showerror("Error", f"No se pudo conectar al servidor de chat: {e}")
|
|
|
|
# Inicializar el reproductor de música
|
|
pygame.mixer.init()
|
|
|
|
# Asociar el frame del panel derecho
|
|
self.frame = frame
|
|
self.frame.configure(bg="lightblue", width=200)
|
|
self.frame.grid_propagate(False)
|
|
self.frame.columnconfigure(0, weight=1)
|
|
|
|
# Botón de iniciar sesión (y cerrar sesión)
|
|
frame_login = tk.Frame(frame, bg="lightblue")
|
|
frame_login.grid(row=0, column=0, pady=5, sticky="ew")
|
|
|
|
self.btn_login = tk.Button(
|
|
frame_login, text="🔑 Iniciar Sesión",
|
|
font=("Helvetica", 11, "bold"), bg="orange", fg="white",
|
|
command=self.mostrar_login
|
|
)
|
|
self.btn_login.pack(padx=5)
|
|
|
|
self.btn_logout = tk.Button(
|
|
frame_login, text="🚪 Cerrar Sesión",
|
|
font=("Helvetica", 11, "bold"), bg="red", fg="white",
|
|
command=self.cerrar_sesion
|
|
)
|
|
self.btn_logout.pack(padx=5)
|
|
self.btn_logout.pack_forget() # Ocultar hasta iniciar sesión
|
|
|
|
# Área de entrada de mensaje
|
|
mensaje_label = tk.Label(self.frame, text="CHAT", font=("Helvetica", 10), bg="lightblue")
|
|
mensaje_label.grid(row=1, column=0, sticky="w", padx=5)
|
|
self.entrada_mensaje = tk.Text(self.frame, height=5, bg="lightyellow", wrap="word")
|
|
self.entrada_mensaje.grid(row=2, column=0, sticky="ew", padx=5, pady=5)
|
|
|
|
# Botón de enviar
|
|
boton_enviar = tk.Button(self.frame, text="Enviar", bg="lightgreen", command=self.enviar_mensaje)
|
|
boton_enviar.grid(row=3, column=0, pady=5, padx=5, sticky="ew")
|
|
|
|
# Listado de mensajes
|
|
mensajes_frame = tk.Frame(self.frame, bg="white", bd=2, relief="sunken")
|
|
mensajes_frame.grid(row=4, column=0, sticky="nsew", padx=5, pady=5)
|
|
mensajes_frame.columnconfigure(0, weight=1)
|
|
self.mensajes_frame = mensajes_frame
|
|
|
|
# Reproductor de música
|
|
musica_frame = tk.Frame(self.frame, bg="lightgray", height=50)
|
|
musica_frame.grid(row=5, column=0, sticky="ew", padx=5, pady=5)
|
|
tk.Label(musica_frame, text="Reproductor música", bg="lightgray", font=("Helvetica", 10)).pack()
|
|
|
|
# Botones de control de música
|
|
boton_play = tk.Button(musica_frame, text="Play", bg="green", fg="white", command=self.reproducir_musica_thread)
|
|
boton_play.pack(side="left", padx=5)
|
|
|
|
boton_pause = tk.Button(musica_frame, text="Pause", bg="orange", fg="white", command=self.pausar_musica_thread)
|
|
boton_pause.pack(side="left", padx=5)
|
|
|
|
boton_restart = tk.Button(musica_frame, text="Restart", bg="blue", fg="white", command=self.reiniciar_musica_thread)
|
|
boton_restart.pack(side="left", padx=5)
|
|
|
|
self.frame.rowconfigure(4, weight=1)
|
|
|
|
# Iniciar actualización de mensajes
|
|
self.iniciar_actualizacion_mensajes()
|
|
|
|
def mostrar_login(self):
|
|
login_window = tk.Toplevel()
|
|
login_window.title("Iniciar Sesión")
|
|
login_window.geometry("300x200")
|
|
|
|
tk.Label(login_window, text="Correo Electrónico:", font=("Helvetica", 11)).pack(pady=5)
|
|
entry_email = tk.Entry(login_window, width=30)
|
|
entry_email.pack(pady=5)
|
|
|
|
tk.Label(login_window, text="Contraseña:", font=("Helvetica", 11)).pack(pady=5)
|
|
entry_password = tk.Entry(login_window, width=30, show="*")
|
|
entry_password.pack(pady=5)
|
|
|
|
def validar_login():
|
|
email = entry_email.get()
|
|
password = entry_password.get()
|
|
|
|
if not email or not password:
|
|
messagebox.showerror("Error", "⚠️ Debes ingresar ambos campos.")
|
|
return
|
|
|
|
self.set_credentials(email, password)
|
|
messagebox.showinfo("Éxito", "✅ Inicio de sesión exitoso.")
|
|
self.btn_login.pack_forget()
|
|
self.btn_logout.pack()
|
|
login_window.destroy()
|
|
|
|
tk.Button(
|
|
login_window, text="🔑 Iniciar Sesión",
|
|
font=("Helvetica", 11, "bold"),
|
|
bg="orange", fg="white",
|
|
command=validar_login
|
|
).pack(pady=10)
|
|
|
|
def cerrar_sesion(self):
|
|
"""Cierra sesión y elimina las credenciales almacenadas."""
|
|
self.clear_credentials()
|
|
messagebox.showinfo("Sesión Cerrada", "🚪 Has cerrado sesión correctamente.")
|
|
self.btn_logout.pack_forget()
|
|
self.btn_login.pack()
|
|
|
|
def set_credentials(self, email, password):
|
|
"""Establece las credenciales de inicio de sesión."""
|
|
self.usuario_email = email
|
|
self.usuario_password = password
|
|
|
|
def clear_credentials(self):
|
|
"""Limpia las credenciales de inicio de sesión."""
|
|
self.usuario_email = None
|
|
self.usuario_password = None
|
|
|
|
def get_credentials(self):
|
|
"""Devuelve las credenciales de usuario (email, password)."""
|
|
return self.usuario_email, self.usuario_password
|
|
|
|
|
|
def enviar_mensaje(self):
|
|
"""Enviar un mensaje al servidor."""
|
|
texto = self.entrada_mensaje.get("1.0", tk.END).strip()
|
|
if texto:
|
|
try:
|
|
self.chat_client.enviar_mensaje(texto)
|
|
self.entrada_mensaje.delete("1.0", tk.END)
|
|
except Exception as e:
|
|
messagebox.showerror("Error", f"No se pudo enviar el mensaje: {e}")
|
|
else:
|
|
messagebox.showwarning("Aviso", "El mensaje está vacío.")
|
|
|
|
def actualizar_lista_mensajes(self):
|
|
"""Muestra los mensajes en la interfaz."""
|
|
for widget in self.mensajes_frame.winfo_children():
|
|
widget.destroy()
|
|
|
|
for mensaje in self.chat_client.mensajes_recibidos:
|
|
mensaje_label = tk.Label(self.mensajes_frame, text=mensaje, anchor="w", justify="left", bg="white", wraplength=180)
|
|
mensaje_label.pack(fill="x", padx=5, pady=2)
|
|
|
|
def iniciar_actualizacion_mensajes(self):
|
|
"""Inicia un ciclo para actualizar los mensajes en la interfaz."""
|
|
def ciclo():
|
|
while True:
|
|
self.actualizar_lista_mensajes()
|
|
self.frame.update_idletasks()
|
|
time.sleep(0.5)
|
|
|
|
threading.Thread(target=ciclo, daemon=True).start()
|
|
|
|
|
|
# Métodos para control de música
|
|
def reproducir_musica_thread(self):
|
|
"""Inicia un hilo para reproducir música."""
|
|
threading.Thread(target=self.reproducir_musica, daemon=True).start()
|
|
|
|
def reproducir_musica(self):
|
|
"""Reproduce una canción."""
|
|
try:
|
|
pygame.mixer.music.load("cancion.mp3")
|
|
pygame.mixer.music.play()
|
|
messagebox.showinfo("Reproducción", "Reproduciendo canción.")
|
|
except pygame.error as e:
|
|
messagebox.showerror("Error", f"No se pudo reproducir la canción: {e}")
|
|
|
|
def pausar_musica_thread(self):
|
|
"""Inicia un hilo para pausar la música."""
|
|
threading.Thread(target=self.pausar_musica, daemon=True).start()
|
|
|
|
def pausar_musica(self):
|
|
"""Pausa la música."""
|
|
pygame.mixer.music.pause()
|
|
messagebox.showinfo("Reproducción", "Música en pausa.")
|
|
|
|
def reiniciar_musica_thread(self):
|
|
"""Inicia un hilo para reiniciar la música."""
|
|
threading.Thread(target=self.reiniciar_musica, daemon=True).start()
|
|
|
|
def reiniciar_musica(self):
|
|
"""Reinicia la música desde el principio."""
|
|
pygame.mixer.music.stop()
|
|
pygame.mixer.music.play()
|
|
messagebox.showinfo("Reproducción", "Reproduciendo desde el principio.") |