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 :


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 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 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
# Window Geometry
# Initiating Pygame
# Initiating Pygame Mixer
# 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),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),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),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
# Changing Directory for fetching Songs
# Fetching Songs
songtracks = os.listdir()
# Inserting Songs into Playlist
for track in songtracks:
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
# Displaying Status
# Loading Selected Song
# Playing Selected Song

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

def stopsong(self):
# Displaying Status
# Stopped Song

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

def pausesong(self):
# Displaying Status
# Paused Song

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

def unpausesong(self):
# It will Display the Status
# Playing back Song

  1. Root Window Looping The command will be:

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:


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

On clicking the PLAYSONG Button:

On clicking the PAUSE Button:

On clicking the STOP Button:

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

Top comments (1)

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! -