我正在尝试使用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: INFO: urwid 1.2.0在python 2.6.6、2.7、3.3上测试都是一样的
发布于 2015-03-27 06:59:50
这可能是因为没有调用主循环的draw_screen方法(它通常在循环进入空闲状态时自动调用)。
在for循环中添加loop.draw_screen()。
https://stackoverflow.com/questions/22641529
复制相似问题