The threading module

Multithreaded application programming can be quite challenging to grasp fully, but the standard library's threading module makes working with threads about as simple as it can be.

To demonstrate the basic use of threading, let's create a slow function:

from time import sleep

def print_slowly(string):
    words = string.split()
    for word in words:
        sleep(1)
        print(word)

This function takes a string and prints it at a rate of one word per second. This will simulate a long-running, computationally expensive process and give us some feedback that it's running.

Let's create a Tkinter GUI frontend for this function:

import tkinter as tk ... class App(tk.Tk): def __init__(self): super().__init__() self.text = tk.StringVar() tk.Entry(self, ...

Get Python GUI Programming with Tkinter now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.