使用Tkinter.after方法时,代码继续传递,而不等待回调完成。在
import tkinter as tk
import tkinter.ttk as ttk
import time
from datetime import datetime
global i
i = 0
global j
j = 0
def SomeFunction():
global i
for num in range(10):
i+=1
x = barVar.get()
barVar.set(x+5)
histrun_mainWindow.update()
time.sleep(2)
def SecondFunction():
global j
for num in range(10):
j+=1
x = barVar.get()
barVar.set(x+5)
histrun_mainWindow.update()
time.sleep(2)
def Load(run_date):
histrun_mainWindow.after(50, SomeFunction)
histrun_mainWindow.after(50, SecondFunction)
global i, j
print 'Number is :', i + j
histrun_mainWindow = tk.Tk()
run_date = datetime.today().date()
barVar = tk.DoubleVar()
barVar.set(0)
bar = ttk.Progressbar(histrun_mainWindow, length=200, style='black.Horizontal.TProgressbar', variable=barVar, mode='determinate')
bar.grid(row=1, column=0)
button= tk.Button(histrun_mainWindow, text='Run for this date ' + str(run_date), command=lambda:Load(run_date))
button.grid(row=0, column=0)
histrun_mainWindow.mainloop()
这个例子展示了正在发生的事情。.after()调用Load()函数,但不等待Load()完成,它直接转到下一行。在
我想打印为10,但是因为.after()不等待Load()完成它的添加,所以我打印为0
进度条会继续更新,因此我知道在打印后,加载在后台继续进行时被调用
使用}冻结
Thread
防止{您的函数-
SomeFunction
,SecondFunction
-也可能在global
命名空间中。然后必须将
self.pbar
作为参数传递,例如SomeFunction(pbar): ... f(self.pbar)
。在用Python:3.5测试 *)无法使用Python 2.7进行测试
相关问题 更多 >
编程相关推荐