Create a Quote Generator in Python Using Tkinter

Faraz

By Faraz -

Learn how to create an inspirational quote generator in Python using Tkinter. Includes step-by-step instructions, code snippets, and tips for beginners.


create-a-quote-generator-in-python-using-tkinter.webp

Table of Contents

  1. Setting Up the Environment
  2. Defining the Quotes
  3. Creating the Tkinter Application
  4. Running the Application
  5. Full Quote Generator Source Code
  6. Conclusion

Have you ever needed a dose of inspiration, motivation, or even just a good laugh? With a simple Python application using Tkinter, you can create a versatile quote generator that can provide quotes across various categories like Inspirational, Motivational, Funny, Love, Life, Friendship, Wisdom, and Success. In this blog, we will walk you through creating this quote generator step-by-step.

Setting Up the Environment

Before starting, ensure you have Python installed on your computer. You can download it from the official Python website. Next, set up a virtual environment to manage your project dependencies. This step helps keep your project organized and avoid conflicts with other Python projects.

Installing TKinter

TKinter comes bundled with Python, so you might already have it. To check, open your terminal and type python -m tkinter. If a TKinter window appears, you’re all set. If not, you can install it using pip install tk. This will install the necessary files to get you started with your GUI application.

Defining the Quotes

We'll start by defining our quotes for different categories. Here's the Python dictionary containing quotes for each category:

import random
import tkinter as tk

# Define the quotes for different categories
quotes = {
    "Inspirational": [
        "The best way to get started is to quit talking and begin doing. - Walt Disney",
        "The pessimist sees difficulty in every opportunity. The optimist sees opportunity in every difficulty. - Winston Churchill",
        "Don't let yesterday take up too much of today. - Will Rogers"
    ],
    "Motivational": [
        "People who are crazy enough to think they can change the world, are the ones who do. - Rob Siltanen",
        "We may encounter many defeats but we must not be defeated. - Maya Angelou",
        "The only limit to our realization of tomorrow is our doubts of today. - Franklin D. Roosevelt"
    ],
    "Funny": [
        "I'm on a whiskey diet. I've lost three days already. - Tommy Cooper",
        "I'm not arguing, I'm just telling you why you're wrong. - Anonymous",
        "I'm not lazy, I'm on energy-saving mode. - Anonymous"
    ],
    "Love": [
        "Love is composed of a single soul inhabiting two bodies. - Aristotle",
        "You know you're in love when you can't fall asleep because reality is finally better than your dreams. - Dr. Seuss",
        "Love is that condition in which the happiness of another person is essential to your own. - Robert A. Heinlein"
    ],
    "Life": [
        "In the end, it's not the years in your life that count. It's the life in your years. - Abraham Lincoln",
        "Life is what happens when you're busy making other plans. - John Lennon",
        "Get busy living or get busy dying. - Stephen King"
    ],
    "Friendship": [
        "Friendship is born at that moment when one person says to another, ‘What! You too? I thought I was the only one.’ - C.S. Lewis",
        "A real friend is one who walks in when the rest of the world walks out. - Walter Winchell",
        "Friendship is the only cement that will ever hold the world together. - Woodrow Wilson"
    ],
    "Wisdom": [
        "The only true wisdom is in knowing you know nothing. - Socrates",
        "Turn your wounds into wisdom. - Oprah Winfrey",
        "Wisdom is not a product of schooling but of the lifelong attempt to acquire it. - Albert Einstein"
    ],
    "Success": [
        "Success is not final, failure is not fatal: It is the courage to continue that counts. - Winston Churchill",
        "Success usually comes to those who are too busy to be looking for it. - Henry David Thoreau",
        "Don't be afraid to give up the good to go for the great. - John D. Rockefeller"
    ]
}

Creating the Tkinter Application

Next, let's create the Tkinter application. We'll create a main window and set up a dropdown menu for selecting categories, a button to generate quotes, and a label to display the quotes.

# Function to get a random quote from the selected category
def get_quote():
    category = category_var.get()
    if category in quotes:
        quote = random.choice(quotes[category])
        quote_label.config(text=quote)
    else:
        quote_label.config(text="Select a category to get a quote!")

# Create the main window
root = tk.Tk()
root.resizable(0, 0)
root.title("Quote Generator")

# Create a StringVar to store the selected category
category_var = tk.StringVar(value="Select a category")

# Create a dropdown menu for categories
category_menu = tk.OptionMenu(root, category_var, *quotes.keys())
category_menu.config(width=20)
category_menu.pack(pady=10)

# Create a button to generate the quote
generate_button = tk.Button(root, text="Generate Quote", command=get_quote)
generate_button.pack(pady=10)

# Create a label to display the quote
quote_label = tk.Label(root, text="Select a category to get a quote!", wraplength=400, justify="center", font=("Helvetica", 12), bg="white", relief="sunken", width=50, height=10)
quote_label.pack(pady=20)

# Start the Tkinter event loop
root.mainloop()

Running the Application

Save your script with a .py extension, for example, quote_generator.py, and run it using the command:

python quote_generator.py

This will open a window with a dropdown menu to select a category, a button to generate a quote, and a label to display the quote.

Full Quote Generator Source Code

import tkinter as tk
import random

# Define the quotes for different categories
quotes = {
    "Inspirational": [
        "The best way to get started is to quit talking and begin doing. - Walt Disney",
        "The pessimist sees difficulty in every opportunity. The optimist sees opportunity in every difficulty. - Winston Churchill",
        "Don't let yesterday take up too much of today. - Will Rogers"
    ],
    "Motivational": [
        "People who are crazy enough to think they can change the world, are the ones who do. - Rob Siltanen",
        "We may encounter many defeats but we must not be defeated. - Maya Angelou",
        "The only limit to our realization of tomorrow is our doubts of today. - Franklin D. Roosevelt"
    ],
    "Funny": [
        "I'm on a whiskey diet. I've lost three days already. - Tommy Cooper",
        "I'm not arguing, I'm just telling you why you're wrong. - Anonymous",
        "I'm not lazy, I'm on energy-saving mode. - Anonymous"
    ],
    "Love": [
        "Love is composed of a single soul inhabiting two bodies. - Aristotle",
        "You know you're in love when you can't fall asleep because reality is finally better than your dreams. - Dr. Seuss",
        "Love is that condition in which the happiness of another person is essential to your own. - Robert A. Heinlein"
    ],
    "Life": [
        "In the end, it's not the years in your life that count. It's the life in your years. - Abraham Lincoln",
        "Life is what happens when you're busy making other plans. - John Lennon",
        "Get busy living or get busy dying. - Stephen King"
    ],
    "Friendship": [
        "Friendship is born at that moment when one person says to another, ‘What! You too? I thought I was the only one.’ - C.S. Lewis",
        "A real friend is one who walks in when the rest of the world walks out. - Walter Winchell",
        "Friendship is the only cement that will ever hold the world together. - Woodrow Wilson"
    ],
    "Wisdom": [
        "The only true wisdom is in knowing you know nothing. - Socrates",
        "Turn your wounds into wisdom. - Oprah Winfrey",
        "Wisdom is not a product of schooling but of the lifelong attempt to acquire it. - Albert Einstein"
    ],
    "Success": [
        "Success is not final, failure is not fatal: It is the courage to continue that counts. - Winston Churchill",
        "Success usually comes to those who are too busy to be looking for it. - Henry David Thoreau",
        "Don't be afraid to give up the good to go for the great. - John D. Rockefeller"
    ]
}

# Function to get a random quote from the selected category
def get_quote():
    category = category_var.get()
    if category in quotes:
        quote = random.choice(quotes[category])
        quote_label.config(text=quote)
    else:
        quote_label.config(text="Select a category to get a quote!")

# Create the main window
root = tk.Tk()
root.resizable(0,0)
root.title("Quote Generator")

# Create a StringVar to store the selected category
category_var = tk.StringVar(value="Select a category")

# Create a dropdown menu for categories
category_menu = tk.OptionMenu(root, category_var, *quotes.keys())
category_menu.config(width=20)
category_menu.pack(pady=10)

# Create a button to generate the quote
generate_button = tk.Button(root, text="Generate Quote", command=get_quote)
generate_button.pack(pady=10)

# Create a label to display the quote
quote_label = tk.Label(root, text="Select a category to get a quote!", wraplength=400, justify="center", font=("Helvetica", 12), bg="white", relief="sunken", width=50, height=10)
quote_label.pack(pady=20)

# Start the Tkinter event loop
root.mainloop()

Conclusion

Creating a quote generator in Python using TKinter is a rewarding project that enhances your programming skills and creativity. By following this guide, you’ve learned how to set up your environment, design a GUI, implement the core logic, and add enhancements to make a fully functional application. Whether you're looking to inspire, motivate, or entertain, your quote generator can be a delightful tool for users.

That’s a wrap!

I hope you enjoyed this article

Did you like it? Let me know in the comments below 🔥 and you can support me by buying me a coffee.

And don’t forget to sign up to our email newsletter so you can get useful content like this sent right to your inbox!

Thanks!
Faraz 😊

End of the article

Subscribe to my Newsletter

Get the latest posts delivered right to your inbox


Latest Post

Please allow ads on our site🥺