Home » Create random fact app using tkinter

Create random fact app using tkinter

Java SE 11 Programmer I [1Z0-815] Practice Tests
Java SE 11 Developer (Upgrade) [1Z0-817]
Java SE 11 Programmer II [1Z0-816] Practice Tests
1 Year Subscription
Spring Framework Basics Video Course
Oracle Java Certification

In a previous article we showed you how you get a  random fact using a library – http://www.maxpython.com/modules/display-a-random-fact-using-a-python-module.php

In this example we will create a simple GUI app using tkinter

Table of Contents

Installation

pip install randfacts

Code

import randfacts

def get_fact():
    txt1.config(state='normal')
    txt1.delete('1.0', tk.END)
    myfact = randfacts.getFact(False)
    txt1.insert(tk.END,myfact)
    txt1.config(state='disabled')
     
def exit():
    window.destroy()
 
import tkinter as tk
window = tk.Tk()
window.geometry("700x250")
window.config(bg="blue")
window.resizable(width=False,height=False)
window.title('FACT APP')
 
lbl1 = tk.Label(window,text="Welcome to the Fact App",font=("Arial", 25),fg="Black",bg="yellow")
lbl2= tk.Label(window,text="Click on the 'Get A Fact!' button to get a fact!",font=("Arial", 15,"bold"),fg="Black",bg="yellow")
btn1 = tk.Button(window,text="Get A Fact",font=("Arial", 15),command=get_fact)
btn2 = tk.Button(window,text="Exit",font=("Arial", 15),command=exit)
txt1 = tk.Text(window,width=60,height=4,font=("Arial",15),state='disabled',bg="yellow")
 
lbl1.pack()
lbl2.pack()
btn1.pack()
txt1.pack()
btn2.pack()
 
window.mainloop()

 

Links

https://github.com/programmershelp/maxpython/blob/main/tkinter/tkinterfacts.py

You may also like

Leave a Comment

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More