Hey coder! This tutorial covers an application that will generate random jokes for the programmers to keep them entertained.
It isn’t easy being a programmer who codes and debug all day and night, trying to clean thousands of code lines every day! A healthy sense of humor can help you deal with tough times when you code all day.
To get random jokes we will use the pyjokes
python library which will provide us with jokes in the application. The pyjokes is a python library that helps to create one-liner jokes and is it is also a fun python library that is pretty simple to use.
Implementing a Random Jokes Generator in Python
As I have mentioned before we will be using pyjokes
the python library for the project. We need to install the library before we use it in our program which you can achieve through the command below.
pip install pyjokes
Now you are ready to build the application in the next few sections.
Designing the Application Screen
We will first by designing the screen using the tkinter
library which will include creating the application and adding various elements to the screen.
The code for the whole design of the application is below. I have added some comments for your reference as well. There is an empty function that is connected with the button joke_gen_btn
. We will complete the function in the next section.
# Importing Module
from tkinter import *
# Creating Screen
app = Tk()
# Customizing Screen
app.title("Random Joke Generator!")
app.geometry('600x200+50+50')
app.resizable(False, False)
app.attributes('-topmost', 1)
app.configure(bg='#FFC300')
# Title of the Application
Title = Label(text="Welcome to Random Joke Generator!",bg="black",fg="white",
font=("Helvetica", 14)).place(x=150, y=7)
# Function that will generate the random joke
def Gen_Joke():
pass
# Button that will generate the random joke
joke_gen_btn = Button(text="Generate Random Joke",font=("Helvetica",14),
width=50,command =Gen_Joke)
joke_gen_btn.place(x=21, y=50)
# Textbox that will display the generated joke
joke_text = Text(height=5,width=70,state='disabled')
joke_text.place(x=20,y=100)
# Run the application
app.mainloop()

Completing the Gen_Joke
function
First we import the pyjokes
library and then use get_joke()
function to get the random joke. The function comes with 2 parameters: language and category.
import pyjokes
def Gen_Joke():
J = pyjokes.get_joke(language="en", category="all")
joke_text.configure(state='normal')
joke_text.delete('1.0',"end")
joke_text.insert('1.0', J)
joke_text.configure(state='disabled')
The language specifies in which language you want the joke that will be generated. By default, it is set to “en”. Similarly, the category parameter specifies the category of the joke which is by default “neutral”.
The Complete Code
Let’s have a look at the complete code with some sample output screens.
# Importing Module
from tkinter import *
# Creating Screen
app = Tk()
# Customizing Screen
app.title("Random Joke Generator!")
app.geometry('600x200+50+50')
app.resizable(False, False)
app.attributes('-topmost', 1)
app.configure(bg='#FFC300')
# Title of the Application
Title = Label(text="Welcome to Random Joke Generator!",bg="black",fg="white",
font=("Helvetica", 14)).place(x=150, y=7)
# Function that will generate the random joke
import pyjokes
def Gen_Joke():
J = pyjokes.get_joke(language="en", category="all")
joke_text.configure(state='normal')
joke_text.delete('1.0',"end")
joke_text.insert('1.0', J)
joke_text.configure(state='disabled')
# Button that will generate the random joke
joke_gen_btn = Button(text="Generate Random Joke",font=("Helvetica",14),
width=50,command =Gen_Joke)
joke_gen_btn.place(x=21, y=50)
# Textbox that will display the generated joke
joke_text = Text(height=5,width=70,state='disabled')
joke_text.place(x=20,y=100)
# Run the application
app.mainloop()


Conclusion
This was the tutorial on creating a joke generator application using python GUI. I hope you find this tutorial helpful and useful. Keep reading to learn more!
Also read: How To Create A GUI Age Calculator in Python Tkinter