Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- # -*- coding: utf-8 -*-
- #
- # player.py
- #
- # Copyright 2022 Usuario <Usuario@DESKTOP-SBRCMHE>
- #
- # This program is free software; you can redistribute it and/or modify
- # it under the terms of the GNU General Public License as published by
- # the Free Software Foundation; either version 2 of the License, or
- # (at your option) any later version.
- #
- # This program is distributed in the hope that it will be useful,
- # but WITHOUT ANY WARRANTY; without even the implied warranty of
- # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- # GNU General Public License for more details.
- #
- # You should have received a copy of the GNU General Public License
- # along with this program; if not, write to the Free Software
- # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
- # MA 02110-1301, USA.
- #
- import os
- import pygame
- import sys
- from tkinter import *
- from tkinter import filedialog as fd
- class MusicPlayer():
- def __init__(self,root):
- self.root = root
- # Título
- self.root.title("Reproductor ACME")
- # Dimensiones y posicion
- self.root.geometry("1000x200")
- self.root.resizable(True,True)
- self.root.minsize(950,170)
- # Inicializamos Pygame
- pygame.init()
- # Inicializo el mixer de Pygame
- pygame.mixer.init()
- # Variable que almacena la canción a reproducir
- self.pista = StringVar()
- # Variable que almacena el estado del reproductor
- self.estado = StringVar()
- self.estado.set("HOLA")
- # Creo el frame de las pistas con las etiquetas
- # de las canciones y del estado del reproductor
- frame_pistas = LabelFrame(
- self.root,
- text = "CANCION",
- font = ("times new roman", 15, "bold"),
- bg = "Gray",
- fg = "LightSteelBlue1",
- bd = 5,
- relief = GROOVE
- )
- frame_pistas.grid(row=0, column=0, sticky="nsew")
- # Insertamos la etiqueta con la pista de la cancion
- cancion = Label(
- frame_pistas,
- textvariable = self.pista,
- width = 50,
- height = 2,
- font = ("times new roman", 12, "bold"),
- bg = "gold",
- fg = "dodger blue",
- )
- cancion.grid(row=0, column=0, padx=10, pady=5)
- # Insertamos la etiqueta con el estado del reproductor
- frame_estado = Label(
- frame_pistas,
- textvariable = self.estado,
- width = 15,
- height = 2,
- font = ("times new roman", 14, "bold"),
- bg = "gold",
- fg = "dodger blue",
- )
- frame_estado.grid(row=0, column=1, padx=10, pady=5)
- # Creo el frame para contener los botones
- frame_boton = LabelFrame(
- self.root,
- text = "Panel de Control",
- font = ("times new roman", 15, "bold"),
- bg = "Gray",
- fg = "LightSteelBlue1",
- bd = 5,
- relief = GROOVE
- )
- frame_boton.grid(row=1, column=0, sticky="nsew")
- # boton play
- boton = Button(
- frame_boton,
- text = "\u25B6",
- command = self.playsong,
- width = 5,
- height = 1,
- font = ("times new roman", 16, "bold"),
- fg = "white",
- bg = "LightSteelBlue3",
- )
- boton.grid(row=0, column=0, padx=10, pady=5)
- # boton pausa
- boton = Button(
- frame_boton,
- text = "\u23F8",
- command = self.pausesong,
- width = 5,
- height = 1,
- font = ("times new roman", 16, "bold"),
- fg = "white",
- bg = "LightSteelBlue3",
- )
- boton.grid(row=0, column=1, padx=10, pady=5)
- # boton stop
- boton = Button(
- frame_boton,
- text = "\u23F9",
- command = self.stopsong,
- width = 5,
- height = 1,
- font = ("times new roman", 16, "bold"),
- fg = "white",
- bg = "LightSteelBlue3",
- )
- boton.grid(row=0, column=2, padx=10, pady=5)
- # boton para cargar lista de canciones
- boton = 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.grid(row=0, column=3, padx=10, pady=5)
- # creo el frame para mostrar la lista de canciones
- frame_cancion = LabelFrame(
- self.root,
- text = "Lista de canciones",
- font = ("times new roman", 15, "bold"),
- bg = "gray",
- fg = "white",
- bd = 5,
- relief = GROOVE
- )
- frame_cancion.grid(row=0, column=1, rowspan=2, sticky="nsew")
- # Configuro el tamaño de cada frame al redimensionar
- self.root.columnconfigure(1, weight=5)
- self.root.rowconfigure(0, weight=1)
- self.root.rowconfigure(1, weight=1)
- # insertamos las barras de scroll
- scroll_y = Scrollbar(frame_cancion, orient=VERTICAL)
- scroll_x = Scrollbar(frame_cancion, orient=HORIZONTAL)
- # insertamos una listbox con las canciones
- self.playlist = Listbox(
- frame_cancion,
- yscrollcommand = scroll_y.set,
- xscrollcommand = scroll_x.set,
- selectbackground = "blue",
- selectmode=SINGLE,
- font = ("times new roman", 12, "bold"),
- bg = "gray",
- fg = "white",
- bd = 5,
- relief = GROOVE
- )
- # aplicamos las scrollbars 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 playsong(self):
- # reproduzco la canción
- if self.estado.get() == "Pausado":
- self.estado.set("Reproduciendo")
- pygame.mixer.music.unpause()
- else:
- # muestro la canción elegida
- titulo = self.playlist.get(ACTIVE)
- self.pista.set(os.path.basename(titulo))
- self.estado.set("Reproduciendo")
- # cargo la canción
- pygame.mixer.music.load(self.playlist.get(ACTIVE))
- pygame.mixer.music.play()
- def pausesong(self):
- self.estado.set("Pausado")
- pygame.mixer.music.pause()
- def stopsong(self):
- self.estado.set("Parado")
- pygame.mixer.music.stop()
- def cargar(self):
- # Elijo el directorio donde cargar las canciones
- ruta = fd.askdirectory()
- self.playlist.delete(0,END)
- # Insertar las canciones dentro de 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)
- if __name__ == '__main__':
- root = Tk()
- reproductor = MusicPlayer(root)
- root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement