DEV Community

Cover image for Music Player App GUI using Python
Arnab Dey
Arnab Dey

Posted on

Music Player App GUI using Python

Here, we will create a Music Player Application in Python using Pygame and Tkinter module.

In our daily life, we see every person has a hobby and that is listening to music. So in order to listen to music, they all need a Music player(hardware or software) where they can play their favorite songs. And we have to install this music player on our computer, based the Operating system i.e Windows, Macintosh, Android, Linux, etc. Then we can listen to our favorite songs.

Now we will help you to code and create a Music Player from scratch.

Libraries used for Music Player Application:
Now we will tell you about the Libraries we will use in our code :

1.Tkinter

We had already told you in the title of this page that we are going to use the Tkinter library, which is a standard library for GUI creation. The Tkinter library is most popular and very easy to use and it comes with many widgets (these widgets helps in the creation of nice-looking GUI Applications).

Also, Tkinter is a very light-weight module and it is helpful in creating cross-platform applications(so the same code can easily work on Windows, macOS, and Linux)

To use all the functions of Tkinter you need to import it in your code and the command for the same is:

from tkinter import *

2.Pygame module

Pygame is a Python module that works with computer graphics and sound libraries and designed with the power of playing with different multimedia formats like audio, video, etc. While creating our Music Player application, we will be using Pygame's mixer.music module for providing different functionality to our music player application that is usually related to the manipulation of the song tracks.

Command used to install pygame is:

pip install pygame

Click and drag to moveTo use this module into your code you need to write this:

import pygame

  1. OS module

There is no need to install this module explicitly, as it comes with the standard library of Python. This module provides different functions for interaction with the Operating System. In this tutorial, we are going to use the OS module for fetching the playlist of songs from the specified directory and make it available to the music player application.

To use this module in your code you need to import its and command for the same is as follows:

import OS

After importing Libraries and modules, now it's time to create a basic window where we will add our UI elements or Tkinter widgets. You can add this code either after importing libraries or also at the end just before the looping of the root window and the code is as follows:

root = Tk() # In order to create an empty window

Passing Root to MusicPlayer Class

MusicPlayer(root)
MusicPlayer Class
Here we have the constructor and the other functions defined in the MusicPlayer class.

  1. init Constructor With the help of this constructor, we will set the title for the window and geometry for the window. We will initiate pygame and pygame mixer and then declare track variable and status variable.

We will then Create the Track Frames for Song label & status label and then after Insert the Song Track Label and Status Label.

After that, we will create the Button Frame and insert play, pause, unpause, and stop buttons into it.

Then we will create the playlist frame and add the scrollbar to it and we will add songs into playlist.

The code snippet is as follows:

def init(self,root):
self.root = root
# Title of the window
self.root.title("MusicPlayer")
# Window Geometry
self.root.geometry("1000x200+200+200")
# Initiating Pygame
pygame.init()
# Initiating Pygame Mixer
pygame.mixer.init()
# Declaring track Variable
self.track = StringVar()
# Declaring Status Variable
self.status = StringVar()

# Creating the Track Frames for Song label & status label
trackframe = LabelFrame(self.root,text="Song Track",font=("times new roman",15,"bold"),bg="Navyblue",fg="white",bd=5,relief=GROOVE)
trackframe.place(x=0,y=0,width=600,height=100)
# Inserting Song Track Label
songtrack = Label(trackframe,textvariable=self.track,width=20,font=("times new roman",24,"bold"),bg="Orange",fg="gold").grid(row=0,column=0,padx=10,pady=5)
# Inserting Status Label
trackstatus = Label(trackframe,textvariable=self.status,font=("times new roman",24,"bold"),bg="orange",fg="gold").grid(row=0,column=1,padx=10,pady=5)

# Creating Button Frame
buttonframe = LabelFrame(self.root,text="Control Panel",font=("times new roman",15,"bold"),bg="grey",fg="white",bd=5,relief=GROOVE)
buttonframe.place(x=0,y=100,width=600,height=100)
# Inserting Play Button
playbtn = Button(buttonframe,text="PLAYSONG",command=self.playsong,width=10,height=1,font=("times new roman",16,"bold"),fg="navyblue",bg="pink").grid(row=0,column=0,padx=10,pady=5)
# Inserting Pause Button
playbtn = Button(buttonframe,text="PAUSE",command=self.pausesong,width=8,height=1,font=("times new roman",16,"bold"),fg="navyblue",bg="pink").grid(row=0,column=1,padx=10,pady=5)
# Inserting Unpause Button
playbtn = Button(buttonframe,text="UNPAUSE",command=self.unpausesong,width=10,height=1,font=("times new roman",16,"bold"),fg="navyblue",bg="pink").grid(row=0,column=2,padx=10,pady=5)
# Inserting Stop Button
playbtn = Button(buttonframe,text="STOPSONG",command=self.stopsong,width=10,height=1,font=("times new roman",16,"bold"),fg="navyblue",bg="pink").grid(row=0,column=3,padx=10,pady=5)

# Creating Playlist Frame
songsframe = LabelFrame(self.root,text="Song Playlist",font=("times new roman",15,"bold"),bg="grey",fg="white",bd=5,relief=GROOVE)
songsframe.place(x=600,y=0,width=400,height=200)
# Inserting scrollbar
scrol_y = Scrollbar(songsframe,orient=VERTICAL)
# Inserting Playlist listbox
self.playlist = Listbox(songsframe,yscrollcommand=scrol_y.set,selectbackground="gold",selectmode=SINGLE,font=("times new roman",12,"bold"),bg="silver",fg="navyblue",bd=5,relief=GROOVE)
# Applying Scrollbar to listbox
scrol_y.pack(side=RIGHT,fill=Y)
scrol_y.config(command=self.playlist.yview)
self.playlist.pack(fill=BOTH)
# Changing Directory for fetching Songs
os.chdir("PATH/OF/DIRECTORY")
# Fetching Songs
songtracks = os.listdir()
# Inserting Songs into Playlist
for track in songtracks:
  self.playlist.insert(END,track)
Enter fullscreen mode Exit fullscreen mode

In the code above, change the PATH/OF/DIRECTORY with appropriate path where the song files are stored.

  1. The playsong() Function Now we will define the Play Song Function and the code is:

def playsong(self):
# Displaying Selected Song title
self.track.set(self.playlist.get(ACTIVE))
# Displaying Status
self.status.set("-Playing")
# Loading Selected Song
pygame.mixer.music.load(self.playlist.get(ACTIVE))
# Playing Selected Song
pygame.mixer.music.play()

  1. The stopsong() Function The code snippet to stop the song:

def stopsong(self):
# Displaying Status
self.status.set("-Stopped")
# Stopped Song
pygame.mixer.music.stop()

  1. The pausesong() Function The code snippet to pause the song:

def pausesong(self):
# Displaying Status
self.status.set("-Paused")
# Paused Song
pygame.mixer.music.pause()

  1. The unpausesong() Function The code snippet to unpause the song:

def unpausesong(self):
# It will Display the Status
self.status.set("-Playing")
# Playing back Song
pygame.mixer.music.unpause()

  1. Root Window Looping The command will be:

root.mainloop()
Now here are some of the screenshots of our application:

We will provide the path of the songs folder in our code where all songs are placed in order to access them.

Now the following screenshot is to show you how the application will look like:

image

whenever you made a click on the song it will look like:
image

On clicking the PLAYSONG Button:
image

On clicking the PAUSE Button:
image

On clicking the STOP Button:
image

So this was all about building the Music Player Application using Tkinter. Hope you all like this application.

Top comments (1)

Collapse
 
pablonax profile image
Pablo Discobar

Hey, cool job! Let's develop the community together. I wrote an article about free Flutter templates, if you have any products (not necessarily a flutter template) that you would like to share, please submit them on my website and I will add them to my article! - code.market/sign-up/


dev.to/pablonax/free-flutter-templ...