Python Progress Bar

后端 未结 30 2224
礼貌的吻别
礼貌的吻别 2020-11-22 06:13

How do I use a progress bar when my script is doing some task that is likely to take time?

For example, a function which takes some time to complete and returns

30条回答
  •  一向
    一向 (楼主)
    2020-11-22 07:02

    @Massagran: It works well in my programs. Furthermore, we need to add a counter to indicate the loop times. This counter plays as the argument of the method update. For example: read all lines of a test file and treat them on something. Suppose that the function dosth() do not concern in the variable i.

    lines = open(sys.argv[1]).readlines()
    i = 0
    widgets=[Percentage(), Bar()]
    pbar = ProgressBar(widgets=widgets,maxval=len(lines)).start()
    pbar.start()
    for line in lines:
        dosth();
        i += 1
        pbar.update(i)
    pbar.finish()

    The variable i controls the status of pbar via the method update

提交回复
热议问题