Practical example of thread using Python

  • its a simple GUI Tkinter based app
simple GUI

The first button will print numbers within a range and with second button we can change color to a specified color while first task is being executed

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”)

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

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

DevOps Is A Myth

How to say Bye Bye to Dual Boot and Virtual Machine

What is Firebase?

A Quick introduction to Git: what it is, and how to use it — By artistwhocode(Siddharth Verma)

Using Google Speech API to transcribe interviews

Why you should write Unit test?

LiftOff Weekly Review

Common Multithreading Mistakes

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Shashankk

Shashankk

Engineering student who loves coding

More from Medium

Coroutines vs Coroutine Functions in Python

Flask, from zero to hero: the complete tutorial (work in progress).

Intro to Python SSH Client Paramiko

[Python] Iterator/Generator/Coroutine