Advertisement
ancestor_tunji

Music Player

Apr 30th, 2020
980
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.26 KB | None | 0 0
  1. #Building a music player
  2.  
  3. import os
  4. from tkinter.filedialog import askdirectory
  5. import pygame
  6. from tkinter import*
  7.  
  8.  
  9. root = Tk()
  10. root.minsize(450,500) # this sets the size of the music player interface
  11.  
  12. listofsongs = []
  13.  
  14. v = StringVar()
  15. songlabel = Label(root,textvariable = v,width = 25)
  16.  
  17. index = 0
  18.  
  19. def nextsong(event): # this enables the next button to funtion as expected when pressed
  20.     global index
  21.     index += 1
  22.     pygame.mixer.music.load(listofsongs[index])
  23.     pygame.mixer.music.play()
  24.     updatelabel()
  25.    
  26. def prevsong(event): # this enables the previous button to funtion as expected when pressed
  27.     global index
  28.     index -= 1
  29.     pygame.mixer.music.load(listofsongs[index])
  30.     pygame.mixer.music.play()
  31.     updatelabel()
  32.    
  33. def stopsong(event): #this enables the stop button to funtion as expected when pressed
  34.     pygame.mixer.music.stop()
  35.     v.set(" ")
  36.     #return songname
  37.    
  38. def updatelabel():
  39.     global index
  40.     global songname
  41.     v.set(listofsongs[index])
  42.     return songname
  43.  
  44. def directory_chooser(): # this allows user to choose which directries to play songs from
  45.     directory = askdirectory()
  46.     os.chdir(directory)
  47.    
  48.     for files in os.listdir():
  49.         if files.endswith(".mp3"): # this helps to pick only files with .mp3 only
  50.            
  51.    
  52.            
  53.             listofsongs.append(files)
  54.            
  55.     pygame.mixer.init()
  56.     pygame.mixer.music.load(listofsongs[0])
  57.     pygame.mixer.music.play()
  58.    
  59.    
  60. directory_chooser()
  61.    
  62. label = Label(root,text = "Music Player")
  63. label.pack
  64.  
  65. listbox = Listbox(root)
  66. listbox.pack()
  67.  
  68. listofsongs.reverse()
  69.  
  70. for items in listofsongs:
  71.     listbox.insert(0,items)
  72.  
  73. listofsongs.reverse()
  74.  
  75. nextbutton = Button(root,text = "Next Song") #creates the next button on the music playing interface
  76. nextbutton.pack()
  77.  
  78. previousbutton = Button(root,text = "Previos Song") #creates the previous button on the music playing interface
  79. previousbutton.pack()
  80.  
  81. stopbutton = Button(root,text = "Stop Song") #creates the stop button on the music playing interface
  82. stopbutton.pack()
  83.            
  84. nextbutton.bind("<Button-1>",nextsong)
  85. previousbutton.bind("<Button-1>",prevsong)
  86. stopbutton.bind("<Button-1>",stopsong)
  87.  
  88. songlabel.pack()
  89.  
  90.  
  91. root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement