Practical example of thread using Python

The practical application

Threads are really amazing concept to learn and useful too…as we know that multi-tasking is the need of every software.i have created a small program in python to show use of threads

  • its a simple GUI Tkinter based app
simple GUI

first we need to import all required libraries

from tkinter import *import threadingimport time

and then creating a simple frame

root =Tk()root.title(‘example’)root.geometry(“400x300+600+100”)

here we need to create two thread for performing two different operations

creating two classes for two threads

class t1(threading.Thread):     def run(self):
print("entering into thread")
for i in range(1,20): print(i) time.sleep(1)obj1 = t1()btn = Button(root,text="click me to start first thread",command=obj1.start)class t2(threading.Thread):
def run(self):
root.configure(background='darkslateblue')
obj2 = t2()btn1 = Button(root,text="start second thread",command=obj2.start)btn.pack()btn1.pack()
root.mainloop()

OUTPUT

Engineering student who loves coding