Python Tkinter Examples: Random Facts Machine

Feature Img Fact Machine

Hello there learner! Today we are going to build a GUI application, Random Facts Machine using Python Tkinter. Let us begin!

What kind of facts are we referring to?

Facts about random stuff from around the world. Could be literally anything as long the statement is true, and is based upon an event that has actually happened.

Facts answer the ‘where,’ ‘when’, ‘why’, and ‘how’ questions. Facts are always supported by evidence, that is why facts are always true.

Reading facts also helps in increasing one’s vocabulary, reading abilities and one’s knowledge obviously. It can also help in releasing stress and anxiety of a person.

Recommended read: How to build a celsius to fahrenheit converter using Tkinter?

Building the Random Facts Machine

Let’s get right into the steps to build our random facts machine using the Tkinter module in Python.

1. Designing the Interface

The designing of the application involves the following steps:

  1. Creating the window
    • Importing Tkinter module
    • Create a customized blank window
    • Set the resizable attributes as False to keep the dimensions unchanged
  2. Adding Basic Elements
    • Labels
    • Get Facts button
    • Textbox to display facts
    • Button to exit the application

The code for the same is shown below. You can customize the window according to your own preferences.

import tkinter as tk
window = tk.Tk()
window.geometry("700x250")
window.config(bg="#E67E22")
window.resizable(width=False,height=False)
window.title('FACT MACHINE')

l1 = tk.Label(window,text="Welcome to the Fact Machine!",font=("Arial", 25),fg="Black",bg="White")
l2= tk.Label(window,text="Click on the 'Get new Fact!' button to get a fact!",font=("Arial", 15,"bold"),fg="darkgreen",bg="#E67E22")
btn1 = tk.Button(window,text="Get new Fact!",font=("Arial", 15))
btn2 = tk.Button(window,text="Exit application",font=("Arial", 15))
t1 = tk.Text(window,width=60,height=2,font=("Arial",15),state='disabled',bg="lightgreen")

l1.pack()
l2.pack()
btn1.pack()
t1.pack()
btn2.pack()

The final design created is shown below.

Initial Screen Fact Machine
Initial Screen Fact Machine

Adding Functions to the Buttons

Button 1: Exit Button

To add the exit button functionality, all we need to do is create a exit function which destroys the window. And then add the command attribute to the button and set it to exit function.

The code for the same is shown below:

def exit():
    window.destroy()

Button 2: The Get Fact button

Now to get the random facts on every click we make use of the randfacts module in Python. In case the module doesn’t exist, then run pip install randfacts in the command prompt on your system.

To get new facts, we make use of the getFact function and add the parameter as False for a random fun fact! After getting the fact we clear the text box and add the fact to the textbox.

After declaring the function, the command attribute is added to the get fact button and it is set as the get_fact function. The code for the same is shown below:

import randfacts
def get_fact():
    t1.config(state='normal')
    t1.delete('1.0', tk.END)
    f = randfacts.getFact(False)
    t1.insert(tk.END,f)
    t1.config(state='disabled')

Complete Code for Random Facts Machine in Tkinter

The final code for the whole application is mentioned below:

import randfacts
def get_fact():
    t1.config(state='normal')
    t1.delete('1.0', tk.END)
    f = randfacts.getFact(False)
    t1.insert(tk.END,f)
    t1.config(state='disabled')
    
def exit():
    window.destroy()

import tkinter as tk
window = tk.Tk()
window.geometry("700x250")
window.config(bg="#E67E22")
window.resizable(width=False,height=False)
window.title('FACT MACHINE')

l1 = tk.Label(window,text="Welcome to the Fact Machine!",font=("Arial", 25),fg="Black",bg="White")
l2= tk.Label(window,text="Click on the 'Get new Fact!' button to get a fact!",font=("Arial", 15,"bold"),fg="darkgreen",bg="#E67E22")
btn1 = tk.Button(window,text="Get new Fact!",font=("Arial", 15),command=get_fact)
btn2 = tk.Button(window,text="Exit application",font=("Arial", 15),command=exit)
t1 = tk.Text(window,width=60,height=2,font=("Arial",15),state='disabled',bg="lightgreen")

l1.pack()
l2.pack()
btn1.pack()
t1.pack()
btn2.pack()

window.mainloop()

Output

The application works perfectly as you can see for yourself in the image below:

Random Facts Machine in Tkinter
Output1 Fact Machine
Output2 Fact Machine Random Facts Machine in Tkinter
Output2 Fact Machine

Conclusion

Congratulations! You have successfully build your own Fact Machine. Hope you liked it! Happy Learning!

Thank you for reading!