Urwid进度条未正确更新
我正在玩一个叫做urwid的库,感觉还不错。不过,我遇到了一个问题,就是进度条无法正常工作。我写了一个简单的测试程序,代码如下:
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)
这个代码运行得很好。看起来进度条在循环中调用时就不太正常。这听起来有点奇怪!有没有人知道怎么解决这个问题?
提前谢谢大家 :)
附注:
信息:
urwid 1.2.0
在python 2.6.6、2.7和3.3上测试,结果都是一样的。
1 个回答
3
这可能是因为主循环的 draw_screen
方法没有被调用(通常在循环进入空闲状态时会自动调用)。
在你的 for 循环里面加上 loop.draw_screen()
。