Advertisement
ancestor_tunji

Music player

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