Facts App Using Tkinter module
This is an app that will provide us with Facts. With Python, we can make things very easy. We have a very amazing library so that we can do it.
We just need two of the libraries and if we just combine the concept, we will be able to make a new thing. We just have to use tkinter and randfacts to do this so.
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning - Basic Level Course
Module Required
- Tkinter: The tkinter package (“Tk interface”) is the standard Python interface to the Tk GUI toolkit. It is an inbuilt module in python.
- RandFacts: Randfacts is a python library that generates random facts. You can use randfacts.getFact() to return a random fun fact. Use the below command to install this module in the system.
pip install randfacts
We have to import the libraries and make two buttons one for adding the facts and one for clearing the window.
Step-by-step Approach:
Step 1) Importing the modules.
Python3
# import required modulesimport tkinter as tkfrom tkinter import *import randfactsimport time |
Step 2) Making the move function(For adding the facts)
Python3
# function to add factsdef move(): facts = randfacts.getFact(True) c = "*)" label = Label(root, text=c+facts) label.pack() |
Step 3) Making the destroy function for clearing the window
Python3
# function to close windowdef destroy(): root.destroy() |
Step 3) Create the buttons in the driver code.
Python3
# driver coderoot = tk.Tk() # adjust windowroot.config(bg="red")root.geometry("400x400") # add buttonsbutton = tk.Button(root, text="Click here for Facts", command=move)button2 = tk.Button(root, text="Clear and quit", command=destroy)button.pack()button2.pack() root.mainloop() |
Below is the complete program based on the above approach:
Python3
# import required modulesimport tkinter as tkfrom tkinter import *import randfactsimport time # function to add factsdef move(): facts = randfacts.getFact(True) c = "*)" label = Label(root, text=c+facts) label.pack() # function to close windowdef destroy(): root.destroy() # driver coderoot = tk.Tk() # adjust windowroot.config(bg="red")root.geometry("400x400") # add buttonsbutton = tk.Button(root, text="Click here for Facts", command=move)button2 = tk.Button(root, text="Clear and quit", command=destroy)button.pack()button2.pack() root.mainloop() |
Output:

