Skip to content
Related Articles

Related Articles

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



filter_none

edit
close

play_arrow

link
brightness_4
code

# import required modules
import tkinter as tk
from tkinter import *
import randfacts
import time

chevron_right


Step 2) Making the move function(For adding the facts)

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# function to add facts
def move():
    facts = randfacts.getFact(True)
    c = "*)"
    label = Label(root, text=c+facts)
    label.pack()

chevron_right


Step 3) Making the destroy function for clearing the window

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# function to close window
def destroy():
    root.destroy()

chevron_right


Step 3) Create the buttons in the driver code.

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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()

chevron_right


Below is the complete program based on the above approach:

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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()

chevron_right


Output:


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.

My Personal Notes arrow_drop_up
Recommended Articles
Page :