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.
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 modules import tkinter as tk from tkinter import * import randfacts import time |
Step 2) Making the move function(For adding the facts)
Python3
# function to add facts def 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 window def destroy(): root.destroy() |
Step 3) Create the buttons in the driver code.
Python3
# driver code root = tk.Tk() # adjust window root.config(bg = "red" ) root.geometry( "400x400" ) # add buttons button = 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 modules import tkinter as tk from tkinter import * import randfacts import time # function to add facts def move(): facts = randfacts.getFact( True ) c = "*)" label = Label(root, text = c + facts) label.pack() # function to close window def destroy(): root.destroy() # driver code root = tk.Tk() # adjust window root.config(bg = "red" ) root.geometry( "400x400" ) # add buttons button = 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:
Contact Us