How to use threads in Tkinter Python application

share link

by Abdul Rawoof A R dot icon Updated: Aug 23, 2023

technology logo
technology logo

Solution Kit Solution Kit  

In Python, a thread refers to a separate flow of execution within a program. Many tasks can run simultaneously and share the resources of one process. 

 

Threads are lighter than processes because they share memory and can talk to each other. We use threads to achieve concurrent programming and handle tasks. A program employs them in situations where it needs to perform many tasks. You can make your Python programs work better by using threads. This is true when doing tasks that need input/output or waiting for external events. Assigning these tasks to separate threads will not interrupt the main execution. Python has a threading module. It helps you create and manage threads in your programs. This module lets you create threads, start and stop them, and sync their execution.   

 

Here are the different types of threads available in Python:   

  • Single-Threaded Execution: In this model, a program runs continuously from start to finish. It executes one line of code at a time. Python's default execution model is single-threaded.   
  • Threading Module: You can use the Threading Module to make and handle many threads in one process. It offers a high-level interface to work with threads. With the threading module, you can spawn many threads that can execute.   
  • Multiprocessing Module: Enables the creation and management of processes instead of threads. Each process runs in a separate memory space, providing true parallelism. The multiprocessing module allows you to leverage many CPU cores.   
  • Asynchronous Programming: Python offers asynchronous programming frameworks like asyncio. You can use coroutines, event loops, and non-blocking I/O to write many codes at the same time. Asynchronous programming achieves concurrency without using many threads or processes.   
  • Parallel Processing: Python has libraries like concurrent.futures and joblib. They enable parallel processing for intensive tasks. Libraries make it possible to divide tasks among many threads or processes.   

 

It is a standard interface to the Tk GUI toolkit shipped. The Python standard library makes it easier to work with threads in Python. Instead, this section briefly introduces the look of a Tkinter app. When you use a function in Python, Tkinter creates a structured window for you. Tkinter widgets that only work on the main thread have a small performance penalty. Until now, we have not used slow functions in tkinter programs. But now, we will use the threading module. Thread objects are instances of the Thread class. Each object represents a separate thread activity. Python's built-in Tcl/Tk language has a different threading model than Python. This can cause confusing errors when mixing Python threads with Tkinter. We can import a threading module and subclass its Thread class to use a thread. 

 

The Python interpreter may link with it, although it is usually a shared library (or DLL). GUI toolkits often access the GUI from one thread, so this recipe works for others too. All offer a toolkit-dependent way to set up periodic polling as this recipe does.   

 

In any case, a GUI already has several polling mechanisms built into it. So, adding one more will make little difference, especially since it seldom runs. When you click a button, it creates an event. The main loop needs to make the button appear pressed and run our callback. You can call the instance, which will wait for the Tcl/Tk main loop to run. It computes a result and passes it back to the calling thread. We can set them as daemon threads, so they run without us needing to remember them. We kill any daemon threads when we quit our program.   

 

Here is an example of how to use threads in the Tkinter application: