Urwid ProgessBar无法正确更新

时间:2014-03-25 17:02:21

标签: python progress-bar tui urwid

我正在玩urwid图书馆,到目前为止一直很好。 但我无法让Progressbar工作。我写了一个简单的测试程序:

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 在python 2.6.6,2.7,3.3上测试了所有相同的

1 个答案:

答案 0 :(得分:3)

这可能是因为没有调用主循环的draw_screen方法(通常在循环进入空闲状态时自动调用它)。

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