簡體   English   中英

python 中的進度條更新

[英]Progress bar update in python

我在stackoverflow中搜索如何在tkinter中實現ProgressBar。 這是代碼:

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()

我發現這應該可行,但不適用於我的情況,因為條形圖一次全部更新為 100%,而我看不到中間步驟。

大家好,我解決了添加線程的問題。 這是代碼:

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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM