130 lines
5.3 KiB
Python
130 lines
5.3 KiB
Python
import tkinter as tk
|
|
from tkinter import messagebox
|
|
from app.chat import Chat
|
|
import threading
|
|
import time
|
|
import pygame # Para reproducir música
|
|
|
|
class PanelDerecho:
|
|
def __init__(self, frame):
|
|
# 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)
|
|
|
|
# Título del chat
|
|
chat_label = tk.Label(self.frame, text="Chat", font=("Helvetica", 12, "bold"), bg="white", fg="red")
|
|
chat_label.grid(row=0, column=0, sticky="ew", pady=5, padx=5)
|
|
|
|
# Área de entrada de mensaje
|
|
mensaje_label = tk.Label(self.frame, text="Mensaje", 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 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.")
|