My question is similar to this: Python TKinter multiple operations. However, The answer provided does not help me since it points to an article with a list of possible functions to use. I want to see an actual implementation of the solution please.
My question: I have two buttons on a frame. one button calls the 'execute' function as long as the toggle variable is set to true. The 2nd button sets the toggle value to False. I want the 'execute' function to keep going once i press the execute button but stop when i press the 2nd (toggle false) button. However, the frame gets stuck once i press 'execute'. I understand its because of callbacks. How can i fix this? Heres my sample code:
from Tkinter import *
from time import sleep
class App:
def __init__(self, master):
self.toggle = False
frame = Frame(master)
frame.pack()
self.exeButton = Button(frame, text="Execute", fg="blue", command=self.execute)
self.exeButton.pack(side=LEFT)
self.tOffButton = Button(frame, text="Toggle Off", command=self.toggleOff)
self.tOffButton.pack(side=LEFT)
def execute(self):
self.toggle = True
while(self.toggle):
print "hi there, everyone!"
sleep(2)
def toggleOff(self):
self.toggle = False
root = Tk()
app = App(root)
root.mainloop()