Featured Post

Let's create a joke generator using python and random joke API and Tkinter | Ashish Pandey

 In this article, we will be going through a Python script that creates a simple graphical user interface (GUI) application for generating jokes. The application uses the Tkinter library to create the GUI and the requests and json libraries to fetch jokes from a public API called the JokeAPI (https://jokeapi.dev/).

The script starts by importing the necessary libraries:

import tkinter as tk
import requests
import json

Tkinter is a built-in Python library for creating GUI applications, while requests and json are commonly used for making HTTP requests and parsing JSON data, respectively.

The script then defines a function called “fetch_joke()”, which is responsible for fetching the jokes from the JokeAPI. This function uses the requests library to make a GET request to the JokeAPI, and then uses the json library to parse the response and extract the joke. Depending on the type of joke received, it will either display the ‘joke’ or the ‘setup’ and ‘delivery’ in the label.

def fetch_joke():
url = "https://jokeapi.dev/joke/Any"
response = requests.get(url)
joke = json.loads(response.text)
if 'joke' in joke:
label.config(text=joke['joke'])
else:
label.config(text=joke['setup'] + '\n' + joke['delivery'])

Next, the script creates the main window of the application using Tkinter’s Tk() class and sets the title of the window to “Joke Generator”.

root = tk.Tk()
root.title("Joke Generator")

The script then creates a button widget using Tkinter’s Button() class, sets the text of the button to “Generate Joke”, and assigns the fetch_joke() function as the button’s command. The button is placed in the center of the window using the place() method.

button = tk.Button(root, text="Generate Joke", command=fetch_joke)
button.place(relx=.5, rely=.2, anchor="c")

A label widget is also created using Tkinter’s Label() class, and is placed in the center of the window as well. This label is used to display the joke generated by the fetch_joke() function. The script then generates a random joke at the beginning and starts the event loop, which allows the application to respond to user input.Copy code

label = tk.Label(root, text="", font=("Cursive", 18))
label.place(relx=.5, rely=.5, anchor="c")


fetch_joke()
root.mainloop()

In summary, this script creates a simple GUI application that fetches a joke from the JokeAPI and displays it in a label widget. When the user clicks the “Generate Joke” button, a new joke is fetched and displayed in the label. The Tkinter library is used to create the GUI elements, while the requests and json libraries are used to fetch and parse the joke data from the JokeAPI.

import tkinter as tk
import requests
import json

def fetch_joke():
url = "https://jokeapi.dev/joke/Any"
response = requests.get(url)
joke = json.loads(response.text)
if 'joke' in joke:
label.config(text=joke['joke'])
else:
label.config(text=joke['setup'] + '\n' + joke['delivery'])

root = tk.Tk()
root.title("Joke Generator")

button = tk.Button(root, text="Generate Joke", command=fetch_joke)
button.place(relx=.5, rely=.2, anchor="c")

label = tk.Label(root, text="", font=("Cursive", 18))
label.place(relx=.5, rely=.5, anchor="c")

fetch_joke()
root.mainloop()

Output:

Joke generator using python

This script can be run in any python environment and it will generate a GUI window with a button and a label, when the button is pressed it will fetch a joke from the JokeAPI and display it on the label.

Comments