How to stop or interrupt a function in python 3 with Tkinter
<p>I started with programming in python just a few months ago and I really love it.
It's so intuitive and fun to start with.</p>

<p>Data for starting point:
I have a linux mashine on which runs python 3.2.3 I have three buttons on GUI to start a function with and one to stop that proccess or the proccesses (Idea).</p>

<p>The source is as follows:</p>

<pre><code>def printName1(event):
    while button5 != True
    print('Button 1 is pressed')
    time.sleep(3) # just for simulation purposes to get reaction time for stopping

    return
print('STOP button is pressed')

def StopButton():
    button5 = True
</code></pre>

<p>I have tried while, and try with except, but the main problem is that the GUI (thinter) is not responding at that time during the process is running. It stores the input and runs it after the first function (printName1) is finished.
I also looked here at stackoverflow, but the solutions didn't worked properly for me and they had the same issues with interrupting.
I apologize for that (maybe) basic question, but I am very new in python and spend a few day of searching an trying. </p>

<p>Is there a way to do that? The solution can maybe made with threading? But how?
Any advise/help is really appreciated.</p>

<p>Thanks a lot!</p>

<p>Use <code>threading.Event</code></p>

<pre><code>import threading
class ButtonHandler(threading.Thread):
    def __init__(self, event):
        threading.Thread.__init__(self)
        self.event = event
    def run (self):
        while not self.event.is_set():
            print("Button 1 is pressed!")
            time.sleep(3)
        print("Button stop")

myEvent = threading.Event()

#The start button
Button(root,text="start",command=lambda: ButtonHandler(myEvent).start()).pack()



#Say this is the exit button
Button(root, text="stop",command=lambda: myEvent.set()).pack()
</code></pre>

python tkinter