[英]Progress bar update in python
I search in stackoverflow how to implement a ProgressBar in tkinter.我在stackoverflow中搜索如何在tkinter中实现ProgressBar。 This is the code:
这是代码:
def worker(x):
return x+x
#time.sleep(1)
def compute():
pool = multiprocessing.Pool(2)
objects=range(1,10)
for i, _ in enumerate(pool.imap_unordered(worker, objects), 1):
#print("completed =" +str(i/len(listF)))
scanned.set((i/len(objects)*100))
if __name__ == '__main__':
window = Tk()
frame = Frame(master = window)
frame.grid(row=0,column=0)
button2 = Button(master=frame,text="Start", command=compute)
button2.pack()
frame = Frame(master = window)
scanned = IntVar()
frame.grid(row=3,column=0,padx=5,pady=10)
progress = Progressbar(master=frame, orient='horizontal',maximum=100, variable=scanned,mode='determinate')
progress.pack()
window.mainloop()
I found that this should work, but not in my case because the bar is update to 100% all in a time and I can not see the intermediate step.我发现这应该可行,但不适用于我的情况,因为条形图一次全部更新为 100%,而我看不到中间步骤。
Hi guys I solve the problem adding a Thread.大家好,我解决了添加线程的问题。 This is the code:
这是代码:
def worker(x):
return x+x
#time.sleep(1)
def precompute():
t=threading.Thread(target=compute)
t.start()
def compute():
pool = multiprocessing.Pool(2)
objects=range(1,1000)
for i, _ in enumerate(pool.imap(worker, objects), 1):
#print("completed =" +str(i/len(listF)))
scanned.set((i/len(objects)*100))
if __name__ == '__main__':
window = Tk()
frame = Frame(master = window)
frame.grid(row=0,column=0)
button2 = Button(master=frame,text="Start", command=precompute)
button2.pack()
frame = Frame(master = window)
scanned = IntVar()
frame.grid(row=3,column=0,padx=5,pady=10)
progress = Progressbar(master=frame, orient='horizontal',maximum=100, variable=scanned,mode='determinate')
progress.pack()
window.mainloop()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.