【发布时间】:2016-04-23 13:02:36
【问题描述】:
在 python 2.7 中,如果我在循环运行时单击按钮 IDLE 将停止工作,直到 python 退出循环。我附上了我的整个代码,因为我不知道为什么会发生这种情况。
import time
import Tkinter as tk
from Tkinter import StringVar
import threading
x="False"
def xval(*args):
for i in range(0,9):
global x
if(x=="False"):
print "x=false %d time"%i
time.sleep(1)
def stop(event):
resume_btn.configure(state="normal")
global x
x ="True"
print "execution stopped:%s"%x
def start(event):
global x
x ="False"
print "execution started:%s"%x
xval()
root = tk.Tk()
th = threading.Event()
t = threading.Thread(target=xval,args=(th,))
t.deamon=True
t.start()
x_btn = tk.Button(root, text="Stop", background="Snow", width=20)
x_btn.grid(row=0, column=4, sticky="W", padx=20, pady=5)
x_btn.bind('<Button-1>',stop)
resume_btn = tk.Button(root, text="Start", background="Snow", width=20)
resume_btn.configure(state="disabled")
resume_btn.grid(row=0, column=6, sticky="W", padx=20, pady=5)
resume_btn.bind('<Button-1>',start)
root.mainloop()
这两个按钮在第一次运行时都可以正常工作,但第二次在我单击停止时 x 的值都不会更新,也不会在 python 退出循环之前按钮正常工作。谁能说出为什么会这样。
【问题讨论】:
标签: python-2.7 loops button tkinter