我一直在試着用2個按鈕製作一個程序,按其中一個會啓動一個無限循環,按下另一個會停止它。Python tkinter啓動和停止循環?
所有嘗試過的方法都會暫停循環。
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()
問題是什麼?另請發佈相關代碼。 – Fiver
我試圖做到這一點,如果你按下一個按鈕,它會運行一個循環,如果你按另一個按鈕,它會停止它。 – Anthony
@tobias_k。你什麼意思?我想要它,所以它打破了循環,而不是暫停。 – Anthony