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 on 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.
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 help 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:
Pygame is a Python module that works with computer graphics and sound libraries and is 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.
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:
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
Here we have the constructor and the other functions defined in the MusicPlayer class.
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 the track variable and status variable.
We will then Create the Track Frames for the 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 the playlist.
The code snippet is as follows:
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)
# 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)
# 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)
# 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:
In the code above, change the PATH/OF/DIRECTORY with an appropriate path where the song files are stored.
Now we will define the Play Song Function and the code is:
# Displaying Selected Song title
# Displaying Status
# Loading Selected Song
# Playing Selected Song
The code snippet to stop the song:
# Displaying Status
# Stopped Song
The code snippet to pause the song:
# Displaying Status
# Paused Song
The code snippet to unpause the song:
# It will Display the Status
# Playing back Song
6. Root Window Looping
The command will be:
Now here are some of the screenshots of our application:
The Folder named studytonight where the code file and folder for songs is placed looks like this:
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 this:
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. Do share more ideas with us for Tkinter based desktop applications and we will definitely add more Tkinter projects to our Tkinter tutorial.