Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/b#in/env python
- # -*- coding: utf-8 -*-
- #
- from tkinter import *
- from tkinter import filedialog as fd
- import pygame
- import os
- import sys
- import time
- class MusicPlayer():
- def __init__(self,root):
- self.root = root
- # Título
- self.root.title("Reproductor ACME")
- # Dimensiones y posicionamiento
- self.root.geometry("1000x200")
- self.root.resizable(True,True)
- self.root.minsize(950,170)
- # Inicializamos Pygame
- pygame.init()
- # Inicializamos Pygame Mixer
- pygame.mixer.init()
- # Variable que almacena la pista
- self.pista = StringVar()
- # Variable con el estado del reproductor
- self.estado = StringVar()
- self.estado.set("HOLA")
- # Creamos el Frame de las pistas con las etiquetas
- # de las canciones y del estado del reproductor
- frame_pistas = LabelFrame(
- self.root,
- text="PISTA",
- font=("times new roman",15,"bold"),
- bg="Gray",
- fg="LightSteelBlue1",
- bd=5,
- relief=GROOVE
- )
- frame_pistas.grid(row=0, column=0, sticky="nswe")
- # Insertamos la etiqueta con la pista de la canción
- cancion = Label(
- frame_pistas,
- textvariable = self.pista,
- width=50,
- justify='left',
- height=2,
- font=("times new roman", 12, "bold"),
- bg="gold",
- fg="dodger blue"
- )
- cancion.grid(row=0, column=0, padx=10, pady=5)
- # Inserting la etiqueta con el estado
- frame_estado = Label(
- frame_pistas,
- width=15,
- height=2,
- textvariable=self.estado,
- font=("times new roman", 14, "bold"),
- bg="gold",
- fg="dodger blue"
- )
- frame_estado.grid(row=0,column=1,padx=10,pady=5)
- # Creamos el frame de los botones
- frame_boton = LabelFrame(
- self.root,
- text="Panel de Control",
- font=("times new roman",15,"bold"),
- fg="LightSteelBlue1",
- bg="gray",
- bd=5,
- relief=GROOVE
- )
- frame_boton.grid(row=1, column=0, sticky="nswe")
- # botón Play
- boton_play = Button(
- frame_boton,
- text="\u25B6",
- command=self.playsong,
- width=5,
- height=1,
- font=("times new roman",16,"bold"),
- fg="white",
- bg="LightSteelBlue3"
- )
- boton_play.grid(row=0,column=0,padx=10,pady=5)
- # botón Pause
- boton_play = Button(
- frame_boton,
- text="\u23F8",
- command=self.pausesong,
- width=5,
- height=1,
- font=("times new roman",16,"bold"),
- fg="white",
- bg="LightSteelBlue3"
- )
- boton_play.grid(row=0,column=1,padx=10,pady=5)
- # Botón Stop
- boton_play = Button(
- frame_boton,
- text="\u23F9",
- command=self.stopsong,
- width=5,
- height=1,
- font=("times new roman",16,"bold"),
- fg="white",
- bg="LightSteelBlue3"
- )
- boton_play.grid(row=0,column=2,padx=10,pady=5)
- # botón para cargar canciones
- boton_play = Button(
- frame_boton,
- text="Lista de Canciones",
- command=self.cargar,
- width=20,
- height=1,
- font=("times new roman",16,"bold"),
- fg="white",
- bg="LightSteelBlue3"
- )
- boton_play.grid(row=0,column=3,padx=10,pady=5)
- # Creamos el frame con la lista de canciones
- frame_cancion = LabelFrame(
- self.root,
- text="Lista de canciones",
- font=("times new roman",15,"bold"),
- bg="grey",
- fg="white",
- bd=5,
- relief=GROOVE
- )
- frame_cancion.grid(row=0, column=1, rowspan=2, sticky="nswe")
- self.root.columnconfigure(1, weight=5)
- self.root.rowconfigure(0, weight=1)
- self.root.rowconfigure(1, weight=1)
- # Insertamos la barra de scroll
- scroll_y = Scrollbar(frame_cancion, orient=VERTICAL)
- scroll_x = Scrollbar(frame_cancion, orient=HORIZONTAL)
- # Insertamos una listbox con la lista de canciones
- self.playlist = Listbox(
- frame_cancion,
- yscrollcommand=scroll_y.set,
- xscrollcommand=scroll_x.set,
- selectbackground="gold",
- selectmode=SINGLE,
- font=("times new roman",12,"bold"),
- bg="gray",
- fg="white",
- bd=5,
- relief=GROOVE
- )
- # Aplicamos la Scrollbar con listbox
- scroll_y.pack(side=RIGHT,fill=Y)
- scroll_y.config(command=self.playlist.yview)
- scroll_x.pack(side=BOTTOM,fill=X)
- scroll_x.config(command=self.playlist.xview)
- self.playlist.pack(expand=True, fill=BOTH)
- def cargar(self):
- # Elegimos el directorio para buscar las canciones
- ruta = fd.askdirectory()
- self.playlist.delete(0,END)
- # Insertamos las canciones en la lista
- for carpeta_actual, lista_carpetas, lista_archivos in os.walk(ruta):
- for archivo in lista_archivos:
- cancion = os.path.join(carpeta_actual, archivo)
- self.playlist.insert(END,cancion)
- def playsong(self):
- # reproduce la canción elegida
- if self.estado.get() == "Pausado":
- # Mostrará el estado
- self.estado.set("Reproduciendo")
- pygame.mixer.music.unpause()
- else:
- # muestra la canción elegida
- titulo = self.playlist.get(ACTIVE)
- self.pista.set(os.path.basename(titulo))
- # muestra el estado
- self.estado.set("Reproduciendo")
- # carga la canción elegida
- pygame.mixer.music.load(self.playlist.get(ACTIVE))
- pygame.mixer.music.play()
- def stopsong(self):
- # Muestra el estado
- self.estado.set("Parado")
- # Se detiene la canción
- pygame.mixer.music.stop()
- def pausesong(self):
- # Muestra el estado
- self.estado.set("Pausado")
- # Canción pausada
- pygame.mixer.music.pause()
- def unpausesong(self):
- # Reproduce la cancion
- pygame.mixer.music.unpause()
- if __name__ == '__main__':
- root = Tk()
- reproductor = MusicPlayer(root)
- root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement