Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Create a GUI to Extract Lyrics from a song using Python
Lyrics are the words that are sung in a song that conveys the meaning and emotions of a song. Python offers several libraries to extract lyrics from a song. In this tutorial, we will create a Graphical User Interface (GUI) using Python's tkinter library that extracts lyrics from a song.
Prerequisites
Before we dive into the details of creating a GUI, you should have a basic understanding of Python programming, object-oriented programming (OOP) concepts, and how to work with the Tkinter module.
Required installations and setup ?
Install required libraries:
pip install lyricsgenius(tkinter comes pre-installed with Python)A code editor like VS Code, PyCharm, or any online Python compiler
Python 3.6 or higher
Genius API access token (free from genius.com)
Setting up the Genius API
To extract lyrics, we'll use the lyricsgenius library which connects to the Genius API. You need to ?
Create an account on genius.com
Go to the API section and create a new app
Copy your Client Access Token
Creating the Lyrics Extractor GUI
Step 1: Import the Required Modules
import tkinter as tk from tkinter import messagebox, scrolledtext from lyricsgenius import Genius
Step 2: Create the Main Window
# Create main window
window = tk.Tk()
window.title("Lyrics Extractor")
window.geometry("600x500")
window.configure(bg='#f0f0f0')
Step 3: Define the Lyrics Extraction Function
def extract_lyrics():
# Get the song name and artist from the text boxes
song_name = song_entry.get().strip()
artist_name = artist_entry.get().strip()
if not song_name or not artist_name:
messagebox.showwarning("Input Error", "Please enter both song name and artist name!")
return
try:
# Clear previous results
lyrics_text.delete(1.0, tk.END)
lyrics_text.insert(tk.END, "Searching for lyrics...")
window.update()
# Create a Genius object and search for the song
genius = Genius("YOUR_ACCESS_TOKEN_HERE")
song = genius.search_song(song_name, artist_name)
# Clear the text area
lyrics_text.delete(1.0, tk.END)
# Display the lyrics in the text box
if song is not None:
lyrics_text.insert(tk.END, song.lyrics)
else:
lyrics_text.insert(tk.END, "Lyrics not found. Please check the song name and artist.")
except Exception as e:
lyrics_text.delete(1.0, tk.END)
lyrics_text.insert(tk.END, f"Error occurred: {str(e)}")
Step 4: Create the GUI Elements
# Song name label and entry
song_label = tk.Label(window, text="Song Name:", font=("Arial", 12), bg='#f0f0f0')
song_label.pack(pady=5)
song_entry = tk.Entry(window, width=40, font=("Arial", 11))
song_entry.pack(pady=5)
# Artist name label and entry
artist_label = tk.Label(window, text="Artist Name:", font=("Arial", 12), bg='#f0f0f0')
artist_label.pack(pady=5)
artist_entry = tk.Entry(window, width=40, font=("Arial", 11))
artist_entry.pack(pady=5)
# Extract button
extract_button = tk.Button(window, text="Extract Lyrics", command=extract_lyrics,
bg='#4CAF50', fg='white', font=("Arial", 12),
padx=20, pady=5)
extract_button.pack(pady=15)
# Lyrics display area with scrollbar
lyrics_label = tk.Label(window, text="Lyrics:", font=("Arial", 12), bg='#f0f0f0')
lyrics_label.pack(anchor='w', padx=10)
lyrics_text = scrolledtext.ScrolledText(window, width=70, height=20,
font=("Arial", 10), wrap=tk.WORD)
lyrics_text.pack(pady=10, padx=10, fill=tk.BOTH, expand=True)
Complete Program
import tkinter as tk
from tkinter import messagebox, scrolledtext
from lyricsgenius import Genius
def extract_lyrics():
# Get the song name and artist from the text boxes
song_name = song_entry.get().strip()
artist_name = artist_entry.get().strip()
if not song_name or not artist_name:
messagebox.showwarning("Input Error", "Please enter both song name and artist name!")
return
try:
# Clear previous results
lyrics_text.delete(1.0, tk.END)
lyrics_text.insert(tk.END, "Searching for lyrics...")
window.update()
# Create a Genius object and search for the song
genius = Genius("YOUR_ACCESS_TOKEN_HERE") # Replace with your actual token
song = genius.search_song(song_name, artist_name)
# Clear the text area
lyrics_text.delete(1.0, tk.END)
# Display the lyrics in the text box
if song is not None:
lyrics_text.insert(tk.END, song.lyrics)
else:
lyrics_text.insert(tk.END, "Lyrics not found. Please check the song name and artist.")
except Exception as e:
lyrics_text.delete(1.0, tk.END)
lyrics_text.insert(tk.END, f"Error occurred: {str(e)}")
# Create main window
window = tk.Tk()
window.title("Lyrics Extractor")
window.geometry("600x500")
window.configure(bg='#f0f0f0')
# Song name label and entry
song_label = tk.Label(window, text="Song Name:", font=("Arial", 12), bg='#f0f0f0')
song_label.pack(pady=5)
song_entry = tk.Entry(window, width=40, font=("Arial", 11))
song_entry.pack(pady=5)
# Artist name label and entry
artist_label = tk.Label(window, text="Artist Name:", font=("Arial", 12), bg='#f0f0f0')
artist_label.pack(pady=5)
artist_entry = tk.Entry(window, width=40, font=("Arial", 11))
artist_entry.pack(pady=5)
# Extract button
extract_button = tk.Button(window, text="Extract Lyrics", command=extract_lyrics,
bg='#4CAF50', fg='white', font=("Arial", 12),
padx=20, pady=5)
extract_button.pack(pady=15)
# Lyrics display area with scrollbar
lyrics_label = tk.Label(window, text="Lyrics:", font=("Arial", 12), bg='#f0f0f0')
lyrics_label.pack(anchor='w', padx=10)
lyrics_text = scrolledtext.ScrolledText(window, width=70, height=20,
font=("Arial", 10), wrap=tk.WORD)
lyrics_text.pack(pady=10, padx=10, fill=tk.BOTH, expand=True)
# Start the GUI event loop
window.mainloop()
How to Use the Application
To use this lyrics extractor GUI ?
Replace
"YOUR_ACCESS_TOKEN_HERE"with your actual Genius API tokenRun the Python script
Enter the song name in the first text field
Enter the artist name in the second text field
Click "Extract Lyrics" button
The lyrics will be displayed in the text area below
Conclusion
This GUI application provides a simple and user-friendly interface to extract song lyrics using Python's tkinter and the lyricsgenius library. The application includes error handling and a scrollable text area for better user experience when viewing long lyrics.
---