Advertisement
teslariu

reproductor

Jan 19th, 2023
858
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.44 KB | None | 0 0
  1. #!/usr/b#in/env python
  2. # -*- coding: utf-8 -*-
  3. #
  4. from tkinter import *
  5. from tkinter import filedialog as fd
  6. import pygame
  7. import os
  8. import sys
  9. import time
  10.  
  11.  
  12.  
  13. class MusicPlayer():
  14.     def __init__(self,root):
  15.         self.root = root
  16.        
  17.         # Título
  18.         self.root.title("Reproductor ACME")
  19.        
  20.         # Dimensiones y posicionamiento
  21.         self.root.geometry("1000x200")
  22.         self.root.resizable(True,True)
  23.         self.root.minsize(950,170)
  24.        
  25.         # Inicializamos Pygame
  26.         pygame.init()
  27.        
  28.         # Inicializamos Pygame Mixer
  29.         pygame.mixer.init()
  30.        
  31.         # Variable que almacena la pista
  32.         self.pista = StringVar()
  33.        
  34.            
  35.         # Variable con el estado del reproductor
  36.         self.estado = StringVar()
  37.         self.estado.set("HOLA")
  38.  
  39.         # Creamos el Frame de las pistas con las etiquetas
  40.         # de las canciones y del estado del reproductor
  41.         frame_pistas = LabelFrame(
  42.             self.root,
  43.             text="PISTA",
  44.             font=("times new roman",15,"bold"),
  45.             bg="Gray",
  46.             fg="LightSteelBlue1",
  47.             bd=5,
  48.             relief=GROOVE
  49.         )
  50.         frame_pistas.grid(row=0, column=0, sticky="nswe")
  51.        
  52.         # Insertamos la etiqueta con la pista de la canción
  53.         cancion = Label(
  54.             frame_pistas,
  55.             textvariable = self.pista,
  56.             width=50,
  57.             justify='left',
  58.             height=2,
  59.             font=("times new roman", 12, "bold"),
  60.             bg="gold",
  61.             fg="dodger blue"
  62.             )
  63.         cancion.grid(row=0, column=0, padx=10, pady=5)
  64.                
  65.        
  66.         # Inserting la etiqueta con el estado
  67.         frame_estado = Label(
  68.             frame_pistas,
  69.             width=15,
  70.             height=2,
  71.             textvariable=self.estado,
  72.             font=("times new roman", 14, "bold"),
  73.             bg="gold",
  74.             fg="dodger blue"
  75.         )
  76.         frame_estado.grid(row=0,column=1,padx=10,pady=5)
  77.  
  78.         # Creamos el frame de los botones
  79.         frame_boton = LabelFrame(
  80.             self.root,
  81.             text="Panel de Control",
  82.             font=("times new roman",15,"bold"),
  83.             fg="LightSteelBlue1",
  84.             bg="gray",
  85.             bd=5,
  86.             relief=GROOVE
  87.         )
  88.         frame_boton.grid(row=1, column=0, sticky="nswe")
  89.        
  90.         # botón Play
  91.         boton_play = Button(
  92.             frame_boton,
  93.             text="\u25B6",
  94.             command=self.playsong,
  95.             width=5,
  96.             height=1,
  97.             font=("times new roman",16,"bold"),
  98.             fg="white",
  99.             bg="LightSteelBlue3"
  100.         )
  101.         boton_play.grid(row=0,column=0,padx=10,pady=5)
  102.        
  103.         # botón Pause
  104.         boton_play = Button(
  105.             frame_boton,
  106.             text="\u23F8",
  107.             command=self.pausesong,
  108.             width=5,
  109.             height=1,
  110.             font=("times new roman",16,"bold"),
  111.             fg="white",
  112.             bg="LightSteelBlue3"
  113.         )
  114.         boton_play.grid(row=0,column=1,padx=10,pady=5)
  115.        
  116.         # Botón Stop
  117.         boton_play = Button(
  118.             frame_boton,
  119.             text="\u23F9",
  120.             command=self.stopsong,
  121.             width=5,
  122.             height=1,
  123.             font=("times new roman",16,"bold"),
  124.             fg="white",
  125.             bg="LightSteelBlue3"
  126.         )
  127.         boton_play.grid(row=0,column=2,padx=10,pady=5)
  128.  
  129.    
  130.         # botón para cargar canciones
  131.         boton_play = Button(
  132.             frame_boton,
  133.             text="Lista de Canciones",
  134.             command=self.cargar,
  135.             width=20,
  136.             height=1,
  137.             font=("times new roman",16,"bold"),
  138.             fg="white",
  139.             bg="LightSteelBlue3"
  140.         )
  141.         boton_play.grid(row=0,column=3,padx=10,pady=5)
  142.        
  143.  
  144.         # Creamos el frame con la lista de canciones
  145.         frame_cancion = LabelFrame(
  146.             self.root,
  147.             text="Lista de canciones",
  148.             font=("times new roman",15,"bold"),
  149.             bg="grey",
  150.             fg="white",
  151.             bd=5,
  152.             relief=GROOVE
  153.             )
  154.         frame_cancion.grid(row=0, column=1, rowspan=2, sticky="nswe")
  155.        
  156.        
  157.         self.root.columnconfigure(1, weight=5)
  158.         self.root.rowconfigure(0, weight=1)
  159.         self.root.rowconfigure(1, weight=1)
  160.        
  161.         # Insertamos la barra de scroll
  162.         scroll_y = Scrollbar(frame_cancion, orient=VERTICAL)
  163.         scroll_x = Scrollbar(frame_cancion, orient=HORIZONTAL)
  164.        
  165.         # Insertamos una listbox con la lista de canciones
  166.         self.playlist = Listbox(
  167.             frame_cancion,
  168.             yscrollcommand=scroll_y.set,
  169.             xscrollcommand=scroll_x.set,
  170.             selectbackground="gold",
  171.             selectmode=SINGLE,
  172.             font=("times new roman",12,"bold"),
  173.             bg="gray",
  174.             fg="white",
  175.             bd=5,
  176.             relief=GROOVE
  177.             )
  178.        
  179.         # Aplicamos la Scrollbar con listbox
  180.         scroll_y.pack(side=RIGHT,fill=Y)
  181.         scroll_y.config(command=self.playlist.yview)
  182.         scroll_x.pack(side=BOTTOM,fill=X)
  183.         scroll_x.config(command=self.playlist.xview)
  184.         self.playlist.pack(expand=True, fill=BOTH)
  185.        
  186.        
  187.            
  188.     def cargar(self):
  189.         # Elegimos el directorio para buscar las canciones
  190.         ruta = fd.askdirectory()
  191.         self.playlist.delete(0,END)
  192.         # Insertamos las canciones en la lista
  193.         for carpeta_actual, lista_carpetas, lista_archivos in os.walk(ruta):
  194.             for archivo in lista_archivos:
  195.                 cancion = os.path.join(carpeta_actual, archivo)
  196.                 self.playlist.insert(END,cancion)
  197.                
  198.            
  199.     def playsong(self):
  200.         # reproduce la canción elegida
  201.         if self.estado.get() == "Pausado":
  202.         # Mostrará el estado
  203.             self.estado.set("Reproduciendo")
  204.             pygame.mixer.music.unpause()
  205.                
  206.         else:
  207.             # muestra la canción elegida
  208.             titulo = self.playlist.get(ACTIVE)
  209.             self.pista.set(os.path.basename(titulo))
  210.             # muestra el estado
  211.             self.estado.set("Reproduciendo")
  212.             # carga la canción elegida
  213.             pygame.mixer.music.load(self.playlist.get(ACTIVE))
  214.             pygame.mixer.music.play()
  215.  
  216.  
  217.     def stopsong(self):
  218.         # Muestra el estado
  219.         self.estado.set("Parado")
  220.         # Se detiene la canción
  221.         pygame.mixer.music.stop()
  222.    
  223.    
  224.     def pausesong(self):
  225.         # Muestra el estado
  226.         self.estado.set("Pausado")
  227.         # Canción pausada
  228.         pygame.mixer.music.pause()
  229.    
  230.     def unpausesong(self):
  231.         # Reproduce la cancion
  232.         pygame.mixer.music.unpause()
  233.  
  234. if __name__ == '__main__':
  235.     root = Tk()
  236.     reproductor = MusicPlayer(root)
  237.     root.mainloop()
  238.    
  239.    
  240.    
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement