How to immediately stop a Python process from a Tkinter window?

I have a Python GUI that I use to test various aspects of my work. I currently have a Stop button that kills the process at the end of each test (there may be several tests configured to run immediately). However, some tests take a lot of time, and if I need to stop the test, I would like it to stop immediately. My thoughts are to use

import pdb; pdb.set_trace()
exit

But I'm not sure how to do this in the next line of code. Is it possible?

+5
source share
1 answer

, thread ( _thread Python 3), , thread.exit().

:


( , ) , , join() , .

:

class MyThread(threading.Thread):

    def __init__(self):
        super(MyThread, self).__init__()
        self._stop_req = False

    def run(self):
        while not self._stop_req:
            pass
            # processing

        # clean up before exiting

    def stop(self):
        # triggers the threading event
        self._stop_req = True;

def main():
    # set up the processing thread
    processing_thread = MyThread()
    processing_thread.start()

    # do other things

    # stop the thread and wait for it to exit
    processing_thread.stop()
    processing_thread.join()

if __name__ == "__main__":
    main()
+5

All Articles