Skip to content

Commit 6806630

Browse files
author
Deepansha
committed
Added Random Joke Generator in Python
1 parent e52ece8 commit 6806630

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

Python/random_joke_generator.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import tkinter as tk
2+
import requests
3+
4+
def get_joke():
5+
"""Fetch a random joke from JokeAPI and display it."""
6+
url = "https://v2.jokeapi.dev/joke/Any"
7+
try:
8+
response = requests.get(url)
9+
data = response.json()
10+
11+
if data["type"] == "single":
12+
joke = data["joke"]
13+
else:
14+
joke = f"{data['setup']}\n\n{data['delivery']}"
15+
16+
joke_text.config(state=tk.NORMAL)
17+
joke_text.delete(1.0, tk.END)
18+
joke_text.insert(tk.END, joke)
19+
joke_text.config(state=tk.DISABLED)
20+
except Exception as e:
21+
joke_text.config(state=tk.NORMAL)
22+
joke_text.delete(1.0, tk.END)
23+
joke_text.insert(tk.END, f"Error fetching joke:\n{e}")
24+
joke_text.config(state=tk.DISABLED)
25+
26+
# --- GUI setup ---
27+
root = tk.Tk()
28+
root.title("Random Joke Generator")
29+
root.geometry("500x300")
30+
root.config(bg="#fafafa")
31+
32+
title = tk.Label(root, text="😂 Random Joke Generator 😂", font=("Helvetica", 16, "bold"), bg="#fafafa")
33+
title.pack(pady=10)
34+
35+
joke_text = tk.Text(root, wrap=tk.WORD, font=("Helvetica", 12), bg="#fff", height=8, width=55)
36+
joke_text.pack(padx=10, pady=10)
37+
joke_text.config(state=tk.DISABLED)
38+
39+
next_button = tk.Button(root, text="Next Joke 👉", command=get_joke, font=("Helvetica", 12, "bold"), bg="#4CAF50", fg="white")
40+
next_button.pack(pady=10)
41+
42+
# Load the first joke automatically
43+
get_joke()
44+
45+
root.mainloop()

0 commit comments

Comments
 (0)