Create a GUI to Extract Lyrics from Songs

If you love music and want to create an application to extract lyrics from song using Python, you’ve come to the right place. In this tutorial We will develop a Python based GUI application that extracts lyrics from any song using the lyrics extraction library and Tkinter GUI framework.

By the end of this guide, you’ll have a fully functional tool where you can input a song’s name and retrieve its lyrics instantly.

Overview

We will:

  1. Use the song extraction library to extract songs from song title input.
  2. Google Custom Search JSON API integration to enable music retrieval.
  3. Create an interactive and intuitive GUI with Tkinter to display music.

Let’s get started!

Prerequisites

Before we begin, ensure you have the following:

  1. Python Installed (3.7 or above recommended).
  2. API Key and Engine ID from Google Custom Search JSON API.
  3. The lyrics-extractor module installed.

Install lyrics-extractor Module

Run this command in your terminal:

JavaScript
pip install lyrics-extractor

Get API Key and Engine ID

To fetch song lyrics, you need to:

  1. Create a Programmable Search Engine at Google Custom Search Engine.
  2. Include popular lyric websites (e.g., Genius, LyricsMint) in your search engine configuration.
  3. Obtain the API Key and Engine ID after creating the custom search engine.

Step 1: Extract Lyrics Using Python

First, let’s see how to fetch lyrics with the lyrics-extractor module.

Code for Extracting Lyrics

Python
# Import the required module
from lyrics_extractor import SongLyrics

# Pass API Key and Engine ID
extract_lyrics = SongLyrics("YOUR_API_KEY", "YOUR_ENGINE_ID")

# Get lyrics of a song
result = extract_lyrics.get_lyrics("Shape of You")
print(result['lyrics'])

Replace YOUR_API_KEY and YOUR_ENGINE_ID with your Google API credentials. The code fetches the lyrics for “Shape of You” and prints the result.

Output:

JavaScript
The club isn't the best place to find a lover
So the bar is where I go...

Step 2: Create a GUI Application with Tkinter

We’ll now create a simple GUI using the Tkinter library that lets users enter a song name and retrieve its lyrics.

Complete Code for GUI-Based Lyrics Extractor

Python
# Import required modules
from tkinter import *
from lyrics_extractor import SongLyrics

# Function to fetch lyrics
def get_lyrics():
    extract_lyrics = SongLyrics("YOUR_API_KEY", "YOUR_ENGINE_ID")
    song_name = e.get()
    try:
        temp = extract_lyrics.get_lyrics(song_name)
        result.set(temp['lyrics'])
    except Exception as ex:
        result.set("Error: " + str(ex))

# Create main window
master = Tk()
master.title("Lyrics Extractor")
master.configure(bg='light grey')

# Define StringVar for result
result = StringVar()

# Widgets for GUI
Label(master, text="Enter Song Name: ", bg="light grey").grid(row=0, sticky=W)
Label(master, text="Lyrics: ", bg="light grey").grid(row=2, sticky=W)

# Entry widget for song name input
e = Entry(master, width=50)
e.grid(row=0, column=1)

# Label to display lyrics
Label(master, text="", textvariable=result, bg="light grey", wraplength=400, justify=LEFT).grid(row=2, column=1)

# Button to fetch lyrics
Button(master, text="Fetch Lyrics", command=get_lyrics, bg="blue", fg="white").grid(row=0, column=2)

# Run the GUI
mainloop()

Explanation of the Code

  1. Import Modules: We import Tkinter for GUI creation and SongLyrics for lyrics extraction.
  2. Define the Function: The get_lyrics() function fetches lyrics based on the song name entered by the user.
  3. Create GUI Widgets:
    • Entry widget for user input.
    • Label widgets to display instructions and the result.
    • Button to trigger the get_lyrics() function.
  4. Error Handling: If no lyrics are found, an error message is displayed.
  5. Run the GUI: The mainloop() keeps the GUI application running.

Output

When you run the program, you’ll see a window with:

  • An input box to enter a song name.
  • A button labeled Fetch Lyrics.
  • A section to display the fetched lyrics.

Example:

  • Input: “Tujhse Naraz Nahi Zindagi Lyrics”
  • Output:
JavaScript
Tujhse naraz nahi zindagi
Hairan hoon main...

Conclusion

You’ve successfully created a Python GUI application to extract song lyrics using the lyrics-extractor module and Tkinter. This project not only demonstrates the power of APIs but also highlights how you can create interactive applications with Python.

Share This Post:

Leave a Reply

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

Scroll to Top