Python - How to retrieve a variable from another threading.Thread
07:06 29 Jul 2026

Note: I'm French, so the translation is probably very bad—sorry.

Hello, I wrote an initial Python script that retrieves a piece of information and writes it to the first line of a text file ("data.txt"). Now I want to create a GUI using Tkinter to display this information. To do this, I created a second script with a loop that retrieves the data into a variable named `data_load`, and I wrapped this in a `threading.Thread()` so that the Tkinter window and the `get_data_loop` function can run simultaneously. However, I don't know how to access the variable from the thread; here is my code. I want to create a label that displays the information retrieved by `get_data_loop`, but I get an error stating that the variable is undefined.

import tkinter as tk
import threading

def get_data_loop():
    while True:
        global data_load
        try:
            data = open("data.txt", "r")
            data_load = data.readlines()[0]
        except:
            pass

threading.Thread(target=get_data_loop).start()
root = tk.Tk()
root.geometry("300x300")

data_label = tk.Label(root, text=data_load)
data_label.pack()

root.mainloop()
python multithreading tkinter