Facts App Using Tkinter module
Last Updated :
29 Dec, 2020
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 tkinter as tk
from tkinter import *
import randfacts
import time
|
Step 2) Making the move function(For adding the facts)
Python3
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
def destroy():
root.destroy()
|
Step 3) Create the buttons in the driver code.
Python3
root = tk.Tk()
root.config(bg = "red" )
root.geometry( "400x400" )
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 tkinter as tk
from tkinter import *
import randfacts
import time
def move():
facts = randfacts.getFact( True )
c = "*)"
label = Label(root, text = c + facts)
label.pack()
def destroy():
root.destroy()
root = tk.Tk()
root.config(bg = "red" )
root.geometry( "400x400" )
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:
Share your thoughts in the comments
Please Login to comment...