'threading.timer does not get closed

My code is similar to below:

from tkinter import *
from threading import Timer

root = Tk()
###
def print_data():
    Timer(1.0, lambda: print_data()).start()
    data.set('sdsds')
###
data = StringVar()
data_label = Label(root, bg = '#FFF', textvariable = data)
data_label.pack()

timer = Timer(1.0, lambda: print_data());timer.start()
root.mainloop()

This raises RuntimeError: main thread is not in main loop.

What I tried:

root.protocol("WM_DELETE_WINDOW", lambda: timer.cancel())          (close button will not work)

What is wrong?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source