G
G
GreenX52020-06-07 07:31:01
Python
GreenX5, 2020-06-07 07:31:01

Python. How to ensure that a thread is closed by closing a Tkinter window?

Can you please tell me how to ensure that the created separate thread is closed after closing the Tkinter window? This construction works, but in case of an exception crash, window.mainloop() may not be overstepped and the thread will continue to thresh, checked.

from tkinter import *
import threading, time

run = True

def clik():
    while run:  
        print('loop')
        time.sleep(2)
   
Potok = threading.Thread(target = clik)

window = Tk()
window.geometry('300x300+500+500')
window.title("Thread Loop")

btn = Button(window, text="Старт!", command = lambda: Potok.start())
btn.grid(column=1, row=0)

window.mainloop()
run = False

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Gornostaev, 2020-06-07
@GreenX5

threading.Thread(target = clik, daemon=True)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question