Creating a Python Random Jokes Generator with a Graphical User Interface (GUI) in 30 Lines using Tkinter – Tutorial

Posted by

Python Random Jokes Generator with GUI – Tkinter Tutorial

Python Random Jokes Generator with GUI – Tkinter Tutorial

In this tutorial, we will create a simple Python program that generates random jokes and displays them in a graphical user interface (GUI) using the Tkinter library.

First, make sure you have Tkinter installed. If you don’t have it installed, you can install it using the following command:


pip install tk

Next, let’s create a Python script that generates random jokes:


import tkinter as tk
import random

jokes = ['Why was the math book sad? Because it had too many problems.',
'How do you organize a space party? You planet.',
'Why couldn’t the bicycle find its way home? It lost its bearings.',
'What do you get when you cross a snowman and a vampire? Frostbite.']

def generate_joke():
joke = random.choice(jokes)
joke_label.config(text=joke)

root = tk.Tk()
root.title("Random Jokes Generator")

joke_label = tk.Label(root, text="", wraplength=300)
joke_label.pack(pady=20)

button = tk.Button(root, text="Generate Joke", command=generate_joke)
button.pack(pady=10)

root.mainloop()

Save the script as jokes_generator.py and run it in your terminal. You should see a window displaying a random joke each time you click the “Generate Joke” button.

That’s it! You have successfully created a Python random jokes generator with a GUI using the Tkinter library.

Feel free to customize the jokes list and the GUI design to make it your own!

0 0 votes
Article Rating
1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
@vishalshrestha6733
4 months ago

Nice!