Experimenting with, installing, and tinkering with new applications on your PC can be a great way to get used to the basics of programming. One of the most fun and interesting projects to start with is creating a music player from scratch. It is also a great way to get used to the basics of programming with it. You will not only have the chance to experiment with the libraries and modules of python but your computer’s settings and customization options on an individual basis!

In this tutorial, we’ll show you how you can create your music player in Python with the help of its modules and libraries.

Prerequisites 

This guide assumes that you already have Python installed on your computer, if it’s not you can download it from here.

You’ll also have to install two packages to proceed with this article, these are:

  1. Tkinter

Tkinter is a standard Python language library (called an add-on module) that allows one to create GUI cards called widgets. Tkinter is the most popular and very easy to use. Tkinter comes with many pre-programmed widget options for creating GUI application UIs.

You can install Tkinter with the pip package manager like so:

pip install tk

Note: If you don’t already have pip installed on your system, you can see how to install it here.

  1. Pygame

Pygame is a cross-platform Python library that is used to play with and create multimedia formats, especially those that involve audio, video, and other interactive media. It is primarily designed for writing video games but can also be used to do various other things like in this case we’ll be using its mixer() module for providing different functionality to our music player.

Pygame can also be installed with the pup package manager:

pip install pygame
  1. OS

This module is used to interact with our operating systems, like folders and directories. This will allow us to fetch songs and other media in our music player. There is no need to install this as it comes prepackaged with every version of Python.

Now, once you have everything installed for our project, we are ready to create the code for our music player program.

Creating the Application 

First, create a file called main.py for our application then, import all the packages that we installed just now:

from tkinter import *

from pygame import mixer

import os 

After that, we can add the Tk() class which would give us the interface on which we will create our application.

root = Tk()
root.title('Music player')

Here we have also titled our app’s name, you can even start this now to see how it looks, just put mainloop() at the end of the code:

As you see it is pretty barebones right now. So let’s start populating it now. But first, we’ll have to define the function that we are going to use:

#Defining the function of the music player
def playsong():  #Play
    currentsong = playlist.get(ACTIVE)
    print(currentsong)
    mixer.music.load(currentsong)
    mixer.music.play()

def pausesong():  #Pause
    mixer.music.pause()

def stopsong():  #stop
    mixer.music.stop()

def resumesong():  #Resume
    mixer.music.unpause()

So here we have created 4 functions and passed commands. The command uses the mixer.music which lets us interact with any audio file. Later in the process, we will create the buttons for each of these functions which will help in playing, stopping, resuming, and stopping the song.

Next, let’s create a playlist view to showcase all the songs present in the directory. To do that create a variable called playlist and list the songs present in the file with the os module.

#Playlist to see the songs inside the application
mixer.init()  #Initializing pygame mixer
playlist = Listbox(root, selectmode=SINGLE, bg="black", fg="white", font=('arial', 15), width=40)  #Creating listbox customizing the looks of it
playlist.grid(columnspan=5)
os.chdir = os.listdir()
songs = os.listdir()  #Loading Music File

for s in songs:  #Displaying the songs
    playlist.insert(END, s)

Now for the last step, we can finally create buttons to interact with our music player:

#Adding interactive buttons
playbtn = Button(root, text="play", command=mixer.music.play()
)
playbtn.grid(row=1, column=0)

pausebtn = Button(root, text="Pause", command=pausesong)
pausebtn.grid(row=1, column=1)

stopbtn = Button(root, text="Stop", command=stopsong)
stopbtn.grid(row=1, column=2)

Resumebtn = Button(root, text="Resume", command=resumesong)
Resumebtn.grid(row=1, column=3)

#And finally run the loop start the application
mainloop()  

Here we have linked these buttons to the functions that we created earlier using the command parameter. You can see that we have also customized it a little bit just to make it usable and the last line which is mainloop() is the Tkinter module that closes any Tkinter application.

All in all the final code will look like this:

from pygame import mixer
from tkinter import *
import os


root = Tk() 
root.title('Music player')

def playsong():
    currentsong = playlist.get(ACTIVE)
    print(currentsong)
    mixer.music.load(currentsong)
    mixer.music.play()

def pausesong():
    mixer.music.pause()

def stopsong():
    mixer.music.stop()

def resumesong():
    mixer.music.unpause()

mixer.init() 
playlist = Listbox(root, selectmode=SINGLE, bg="black", fg="white", font=('arial', 15), width=40) 
playlist.grid(columnspan=5)
os.chdir = os.listdir()
songs = os.listdir() 

for s in songs: 
    playlist.insert(END, s)

playbtn = Button(root, text="play", command=playsong)
playbtn.grid(row=1, column=0)

pausebtn = Button(root, text="Pause", command=pausesong)
pausebtn.grid(row=1, column=1)

stopbtn = Button(root, text="Stop", command=stopsong)
stopbtn.grid(row=1, column=2)

Resumebtn = Button(root, text="Resume", command=resumesong)
Resumebtn.grid(row=1, column=3)

mainloop() 

And finally, we can now run this application!

Output

To run this, run this file as you would any other .py file.

python main.py  #Windows
Or
python3 main.py  #Linux/Mac

If it worked successfully you see a new window pop-up like this:

As you can see, our application works. We can see the buttons and the playlist that we define. You may have noticed that the playlist is only showing the contents of the directory it is located in. You can change it and put the location of a different directory if you want pretty easily.

In other words, we’ll have to import some songs to play with this. So put some songs in the folder where the main.py file is located and test it out!

Final Words

In this tutorial, we went through the process of creating a music player from scratch in Python with the help of its pygame and Tkinter. We hope you enjoyed it. We know our tutorial was brief, but we wanted to keep it short and easily digestible. If you did enjoy it, however, we would love it if you could share it with your friends and subscribe to our blog! We plan to publish more tutorials in the future, so stay tuned!

Here are some useful tutorials that you can read: