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:
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 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:
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)
In the code above, change the PATH/OF/DIRECTORY with an appropriate path where the song files are stored.
2. 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()
3. 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()
4. 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()
5. 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()
6. Root Window Looping
The command will be:
root.mainloop()
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.