2014-03-25 14 views
1

我在玩urwid圖書館,目前爲止它已經非常棒了。 但我無法讓進度條工作。我寫了這樣一個簡單的測試程序:Urwid ProgessBar沒有正確更新

import os 
import urwid 

    # a function that takes some times to complete 
def dosomething(steps, bar): 
    bar.done = steps 
    for i in range(steps + 1): 
     bar.set_completion(i) 
     os.system('sleep 0.2') 

    # make a button to start 
task_btn = urwid.Button(u'Task') 
    # progressbar object 
pbar = urwid.ProgressBar('pg normal', 'pg complete') 

    # function called when the task button is clicked 
def on_task_clicked(button): 
    dosomething(10, pbar) 

    # setup signal handler for the button 
urwid.connect_signal(task_btn, 'click', on_task_clicked) 

    """ 
    create the interface and the mainloop. 
    filler objects are our friend to prevent unpacking errors :D 
    """ 

loop = urwid.MainLoop(urwid.Pile([urwid.Filler(task_btn), urwid.Filler(pbar)])) 
loop.run() 

如果我啓動它,進度條應該是0%。然後我按下按鈕,幾秒鐘後進度條顯示100%。但我錯過了0%和100%之間的步驟。他們只是不會出現。

另外一個渲染函數的調用不起作用。

我也試過這樣的事情:

def on_task_clicked(button): 
    pbar.set_completion(pbar.current+1) 

這工作得很好。看起來進度條對被循環調用並不滿意。這似乎很奇怪?!有人有任何想法,以解決這個問題?

感謝提前:)

PS: 信息: urwid 1.2.0 對蟒蛇2.6.6測試,2.7,3.3都是一樣的

回答

3

這可能是因爲主迴路的draw_screen方法ISN不會被調用(通常在循環進入空閒狀態時會自動調用它)。

在for循環中添加loop.draw_screen()

+0

添加'loop.draw_screen()'爲我工作(Python 3.6和urwid 1.3.1)。但我認爲創建一個函數可以隨時更新百分比的變化。不知道是否有可能。 – meyer1994