Extract Song Lyrics using Python with GUI

FREE Online Courses: Click, Learn, Succeed, Start Now!

In this project, we are going to create a GUI window using Python to extract lyrics of a song from some other website. This is a mediocre-level project which will use the API key and Engine Id to extract lyrics. We will be using the Tkinter library for creating GUI and the Lyrics_extractor Module. So without wasting any more time, let’s get started.

About Python Lyrics Extractor project

There are times when we want the lyrics of a particular song and we have to go through a long process to get them. This is where a lyrics extractor comes to use.

Lyrics Extractor is an application that will help users extract lyrics of any song. This application will save time by giving direct access to the lyrics without going to the web browser.

Project to Extract Lyrics of song Using Python

During the creation of this project, we will be using two modules:

  • Tkinter Module – For creating an easy GUI
  • Lyrics_Extractor – For extracting lyrics of a song.

We can use any website to extract lyrics from. Here we have used www.lyricsoff.com.

While creating the project, we will need an API Key and Engine ID. These will be used in the SongLyrics() method while extracting the lyrics. You can create your own API key and Engine ID using the below given links.

  • API key – https://developers.google.com/custom-search/v1/overview
  • Engine Id – https://cse.google.com/cse/create/new

It is advised to create a new API key and Engine ID for security reasons.

While creating the Engine ID, you will have to put in the name of the website you will be using to extract the lyrics. You can put in multiple website urls to create better results.

Project Prerequisites

To create this project to extract Lyrics of song Using Python, we will need the following libraries:

a. Tkinter Module – This provides us with a lot of inbuilt libraries which will ease us while creating a GUI for the project. The command to install it goes as –

Pip install tk

b. Lyrics_Extractor Module – This will help us extract lyrics from any website that we want. The command to install it goes as follows:

Pip install lyrics-extractor

Download the Source Code

Click here to download the source code of Python Lyrics Extractor Project from the following link: Song Lyrics Extractor using Python

Steps To Proceed with the Project

1. Import the required Libraries:

#importing required libraries
import tkinter as tk
from tkinter import *
from lyrics_extractor import SongLyrics
  • Tkinter Module – We will be importing the tkinter module as tk so that it is easy to use while the project is created.
  • SongLyrics – From the lyrics_extractor we are importing SongLyrics which will provide us with the lyrics extraction facility.

2. Creating the GUI:

window = Tk()#creating window
window.geometry('600x600')#giving size
window.title('PythonGeeks')#giving title
head=Label(window, text="Enter the song you want Lyrics for", font=('Calibri 15'))# a label
head.pack(pady=20)
  • Using the Tk() method, we create a GUI window named window. We want the window to be of some specific size so we make use of the geometry() method and to specify the title we use the title() method.
  • Label() – This is an inbuilt method of Tkinter Module. A label() method helps us display a certain text in the form of a label on the window. Inside the label we need to name the GUI window where we have to display the text and also specify the text that we want to display. There are certain other attributes like font, background colour, foreground colour, size of text etc that we can specify while creating the label.
  • pack() – To display the label that we have created, we are going to use the pack() method. While using the pack() method, you don’t need to specify the x and y coordinates. The method automatically sets the label for you where it should be. The pack() method uses the first come first serve kind of approach as the widget created first in the project is displayed first as there are no coordinates.
Entry(window, textvariable=song).pack()# enter a song name
Message(window,textvariable=result, bg="light grey").pack(side=TOP,anchor=W,fill=BOTH, expand=1)#displaying the lyrics
#create a button
Button(window, text="GO",command=get_lyrics).pack()
  • Now that we have created a window, we will be adding widgets to it. As we will be using the pack() method throughout, let us make the widgets in the same order we want them to be displayed on our window.
  • Entry() – This will let us create an entry field on our window where users will type in the name of the song. Here we have set textvariable= song as we want the value entered by the user to be stored in this song variable.
  • Message() – This Message widget will help us display the lyrics of the song chosen by the user. We chose Message() instead of label because it provides multiple line output while in Label() the output is displayed in a single line. While using the pack –
  • Side – determines which side the text will be displayed
  • Anchor = w – this will set the text to the west side in respect to the center.
  • Fill=BOTH – Fill the output in both x and y coordinates equally.
  • Expand – Expands to fill vacant spaces
  • Button() – Button() method is an inbuilt method to create a button on the window. We set the text that will be displayed on the button.
  • Command specifies which function of the program should be evoked when this button is clicked (in this case we are invoking the get_lyrics function which is explained later in the report).

3. Function to get the Lyrics:

def get_lyrics():
song_name=song.get()# using get method getting value of song
api_key = "AIzaSyAcZ6KgA7pCIa_uf8-bYdWR85vx6-dWqDg"
engine_id = "aa2313d6c88d1bf22"
extract_lyrics = SongLyrics(api_key, engine_id)# going to the website for which we have got the engine id
song_lyrics = extract_lyrics.get_lyrics(song_name)#getting the lyrics
result.set(song_lyrics)#setting the result
  • We are creating a function to extract lyrics from the website when the user clicks the button on the window.
  • As we have saved the song name in a variable named song, now we are getting the value of song using the get() method and saving it in another variable song_name.
  • We specify our api key and engine id and pass it to a method SongLyrics() which will go to the website we have chosen.
  • Now using the get_lyrics() method, we get the lyrics of the song which is passed to this method.
  • Finally as the message() variable was the result, we set the value to the result and this will display the lyrics on the window.
window.mainloop()
  • The mainloop() function will display all the widgets and the window that we have created.

Hurray! We have completed the project successfully. Now let us look at the output of this project.

Python Song Lyrics Extractor Output

python extract song lyrics output

Summary

During the creation of the project, we learned how to use the Tkinter Module and we also learned about some widgets in it. In addition to this, we also learned about the Lyrics Extractor Library and we saw how useful it can be to create a project like this one. Now using this project, we can easily extract lyrics of any song.

Did you like this article? If Yes, please give PythonGeeks 5 Stars on Google | Facebook


2 Responses

  1. Varshitha Kudum says:

    I couldn’t get output i think the problem is linking api and engine id ,pls help me

  2. TAVANAM VAISHNAVI says:

    I need source code

Leave a Reply

Your email address will not be published. Required fields are marked *