Coders Packet

Music Player GUI application in Python

By Sweta Kumari

This project is Music Player GUI application in python which enables music and lets you listen to your favorite music by adding it to your list.

This music application is made from Tkinter Python. In which you can make your playlist by adding songs and playing them according to your mood.

This project also contains features of deleting songs from the playlist.

 

This project is made in the following steps:

 

step 1:

we will be importing *(ie, everything) from Tkinter() and also Filedialog which will help you to design your outlet Filedialogs help you open, save files or directories. This is the type of dialog you get when you click file, open. This dialog comes out of the module, there’s no need to write all the code manually. and also pygame for you audio of music.

from tkinter import *
import pygame
from tkinter import filedialog

step 2:

make functions of adding songs that will let you add songs as per your choice and put your file location where your song is placed. This code outlet contains two function :

First for adding one song at a time

Second for adding many songs at a time

def add_song():
    song = filedialog.askopenfilename(initialdir='audio/', title="Choose A Song", filetypes=(("mp3 Files", "*.mp3"), ))
    song = song.replace("C:/Users/sweta/Pictures/images/", "")
    song = song.replace(".mp3", "")
    
    song_box.insert(END, song)

    
    
def add_many_songs():
    songs = filedialog.askopenfilenames(initialdir='audio/', title="Choose A Song", filetypes=(("mp3 Files", "*.mp3"), ))
    for song in songs:
        song = song.replace("C:/Users/sweta/Pictures/images/", "")
        song = song.replace(".mp3", "")
        song_box.insert(END, song)

 

step 3:

In the third step, we are going to create a function for the song to play, pause and stop so functions are made accordingly.

In-play function: we have used pygame mixer music.

iIn-stop function: we again stopped our stop by giving the command to pygame mixer music.

In-pause function:  we will use global variable in this , which could be used inside as well as outside function and make pause variable in it and with The help of pygame we gonna use it.

#PLAY FUNCTION
    
def play():
    song = song_box.get(ACTIVE)
    song = f'C:/Users/sweta/Pictures/images/{song}.mp3'
    
    pygame.mixer.music.load(song)
    pygame.mixer.music.play(loops=0)




#STOP FUNCTION

def stop():
    pygame.mixer.music.stop()
    song_box.selection_clear(ACTIVE)
    






#PAUSE FUNCTION

#global pause variable
global paused 
paused=False
    
def pause(is_paused):
    
    global paused
    
    paused=is_paused
    
    if paused: 
        #then unpause it
        pygame.mixer.music.unpause()
        paused=False
    else:
        #pause it
        pygame.mixer.music.pause()
        paused=True
    

 

step 4:

In step 4 we going make function for previous as well as next song button function.

The important thing to understand in these function is the use of line , like in previous function we would increase it to the next song and in the next song function we have to increase it to the next song.

 

#for next song button

def next_music(): #get the next song tupple number next_song= song_box.curselection() #add it to the current song next_song=next_song[0]+1 song=song_box.get(next_one) song = f'C:/Users/sweta/Pictures/images/{song}.mp3' pygame.mixer.music.load(song) pygame.mixer.music.play(loops=0) song_box.selection_clear(0, END) song_box.activate(next_one) song_box.selection_set(next_one, last=None) #for previous button def prev_music(): #get the previous song tupple number next_one= song_box.curselection() #add it to the current song next_one=next_one[0]-1 music=music_box.get(next_one) music = f'C:/Users/sweta/Pictures/images/{song}.mp3' pygame.mixer.music.load(song) pygame.mixer.music.play(loops=0) song_box.selection_clear(0, END) song_box.activate(next_one) song_box.selection_set(next_one, last=None)

 

 

step 5:

Deleting songs could be done by these two easy function .

One function could be deleting one song at a time and another one could be deleting many songs at  a time.

 

#DELETING ONE SONG AT A TIME

def delete_music():
    music_box.delete(ANCHOR)                        
    pygame.mixer.music.stop()                        



#DELETING MANY SONG AT A TIME

def delete_all_musics():
    music_box.delete(0, END)  
    
    pygame.mixer.music.stop()   

 

step 6:

Here we just have to make variables for uploading images of our button

back_btn_img = PhotoImage(file = "C:/Users/sweta/Pictures/images/back.png")
forward_btn_img = PhotoImage(file="C:/Users/sweta/Pictures/images/forward.png")
play_btn_img = PhotoImage(file="C:/Users/sweta/Pictures/images/play.png")
pause_btn_img = PhotoImage(file="C:/Users/sweta/Pictures/images/pause.png")
stop_btn_img = PhotoImage(file="C:/Users/sweta/Pictures/images/stop.png")

step 7:

Place your buttons on the page.

back_button.grid(row=0, column=0, padx=10)
forward_button.grid(row=0, column=1, padx=10)
play_button.grid(row=0, column=2, padx=10)
pause_button.grid(row=0, column=3, padx=10)
stop_button.grid(row=0, column=4, padx=10)

 

Download Complete Code

Comments

No comments yet