Skip to content
Advertisement

Start and Stop threads with while loop

I’m building a tkinter GUI to retrieve incoming data from a device (through request) and appending them into a csv file, while having a preview of these data.

I use thread but struggle to understand it properly, especially how to pause and restart it.

When I click on the start button, my threads are working properly. Once I try to click the stop streaming button, my stop_threads variable change to 0, however, the while loop in my thread do not see this change. Do you know why?

Thanks a lot!

JavaScript

Advertisement

Answer

You have to change only one thing. You need to define stop_threads as global inside the functions. Python then knows that you are referring to the global variable instead of creating a new variable inside the local function scope.

JavaScript
Advertisement