Don't Have A Music Player Lets Create One....
For Music Player you need two libraries pygame and tkinter.
You need to install both via python -m install 'library name' command.
You can get all the libraries from here: https://pypi.org/project/pip/
Now lets dive into code:-
- Step 1 would be create a function for playing the song.
- Next to pause the song
- Then finally to resume the song
- And finally to stop the song
- So there is something called as ListBox which returns you a playlist env
- You just need to get the song in folder and via os iterate throught the folder
- Insert the song files to the playlist
- Now final step would be create different buttons for different functions
- And play around with the inbuilt methods in the mixer lib which comes from pygame
- Lets view the code now.
from pygame import mixer
from tkinter import *
import os
def play_song():
current_song = playlist.get(ACTIVE)
print(current_song)
mixer.music.load(current_song)
song_status.set("Playing")
mixer.music.play()
def pause_song():
song_status.set("Paused")
mixer.music.pause()
def stop_song():
song_status.set("Stopped")
mixer.music.stop()
def resume_song():
song_status.set("Resuming")
mixer.music.unpause()
root = Tk()
root.title('UJ Music Player')
mixer.init()
song_status = StringVar()
song_status.set("choosing")
playlist = Listbox(root, selectmode=SINGLE, bg="Pink", fg="black", font=('arial', 15), width=40)
playlist.grid(columnspan=5)
os.chdir(r'D:\songs')
songs = os.listdir()
for s in songs:
playlist.insert(END, s)
play_btn = Button(root, text="play", command=play_song)
play_btn.config(font=('arial', 20), bg="Green", fg="white", padx=7, pady=7)
play_btn.grid(row=1, column=0)
pause_btn = Button(root, text="Pause", command=pause_song)
pause_btn.config(font=('arial', 20), bg="Blue", fg="white", padx=7, pady=7)
pause_btn.grid(row=1, column=1)
stop_btn = Button(root, text="Stop", command=stop_song)
stop_btn.config(font=('arial', 20), bg="Red", fg="white", padx=7, pady=7)
stop_btn.grid(row=1, column=2)
Resume_btn = Button(root, text="Resume", command=resume_song)
Resume_btn.config(font=('arial', 20), bg="Orange", fg="white", padx=7, pady=7)
Resume_btn.grid(row=1, column=3)
mainloop()

Comments
Post a Comment