Ive been trying to make a program with 2 buttons, pressing one of them will start an infinite loop and pressing the other one will stop it.
All the methods ive tried will just pause the loop.
from Tkinter import *
import time
s = 0
def stopit():
s = 1
print "stoped"
#
def callback():
if s == 0:
while True:
print "called the callback!"
time.sleep(3)
if s == 1:
break
#
#
#
#
root = Tk()
def main():
# create a menu
menu = Menu(root)
root.config(menu=menu)
b = Button(root, command=stopit)
b.pack()
filemenu = Menu(menu)
menu.add_cascade(label="File", menu=filemenu)
filemenu.add_command(label="New", command=callback)
filemenu.add_command(label="Open...", command=callback)
filemenu.add_separator()
filemenu.add_command(label="Exit", command=callback)
helpmenu = Menu(menu)
menu.add_cascade(label="Help", menu=helpmenu)
helpmenu.add_command(label="About...", command=callback)
mainloop()
time.sleep(3)
#
main()
callbackmethod never finishes, so I can not press the "stop" button at all. You should try to useTkinter.afterinstead.