【发布时间】:2013-12-25 18:24:23
【问题描述】:
我一直在尝试制作一个有 2 个按钮的程序,按其中一个将启动无限循环,按另一个将停止。
我尝试过的所有方法都会暂停循环。
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()
【问题讨论】:
-
问题是什么?另外,请贴出相关代码。
-
我正在尝试这样做,所以如果你按下一个按钮,它将运行一个循环,如果你按下另一个按钮,它将停止它。
-
@tobias_k。你的意思是?我想要它,让它跳出循环,而不是只是暂停。
-
当我运行程序时,GUI 只是冻结,因为
callback方法永远不会完成,所以我根本无法按下“停止”按钮。您应该尝试改用Tkinter.after。 -
@tobias_K 谢谢!!这似乎有效!