在 CLI 应用程序中创建进度条

发布于 2024-11-16 08:05:46 字数 311 浏览 3 评论 0原文

当使用 bzrdoxygenscpwget 等工具时,我发现它们都取得了不错的进展 。

|=============>---------| 55% ETA 3:30

我尝试使用 \b 字符在 C++ 中编写类似的内容,就像我以前写过的内容一样多次 输出闪烁得非常严重,并且看起来不像上述工具那样平滑。

如何用Python顺利地创建这样一个进度条(或者至少改变显示的ETA)?

When using tools like bzr, doxygen and scp or wget, I see that all of them have a nice progress bar that looks like this:

|=============>---------| 55% ETA 3:30

I tried writing something like that in C++ using the \b character as many times as I had written something out before. The output was flickering pretty badly and did not look nearly as smooth as the mentioned tools do.

How can I create such a progress bar (or at least change the displayed ETA) with Python smoothly?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

身边 2024-11-23 08:05:46

使用“\r”将光标发送到行首。每秒重印次数不要超过 2-3 次,以避免闪烁。

Use "\r" to send the cursor to the beginning of the line. Reprint no more than 2-3 times per second to avoid flickering.

累赘 2024-11-23 08:05:46

这可能是有用的示例:

http://code .google.com/p/corey-projects/source/browse/trunk/python2/progress_bar.py

import sys
import time


class ProgressBar:
    def __init__(self, duration):
        self.duration = duration
        self.prog_bar = '[]'
        self.fill_char = '#'
        self.width = 40
        self.__update_amount(0)

    def animate(self):
        for i in range(self.duration):
            if sys.platform.lower().startswith('win'):
                print self, '\r',
            else:
                print self, chr(27) + '[A'
            self.update_time(i + 1)
            time.sleep(1) 
        print self

    def update_time(self, elapsed_secs):
        self.__update_amount((elapsed_secs / float(self.duration)) * 100.0)
        self.prog_bar += '  %ds/%ss' % (elapsed_secs, self.duration)

    def __update_amount(self, new_amount):
        percent_done = int(round((new_amount / 100.0) * 100.0))
        all_full = self.width - 2
        num_hashes = int(round((percent_done / 100.0) * all_full))
        self.prog_bar = '[' + self.fill_char * num_hashes + ' ' * (all_full - num_hashes) + ']'
        pct_place = (len(self.prog_bar) / 2) - len(str(percent_done))
        pct_string = '%d%%' % percent_done
        self.prog_bar = self.prog_bar[0:pct_place] + \
            (pct_string + self.prog_bar[pct_place + len(pct_string):])

    def __str__(self):
        return str(self.prog_bar)


if __name__ == '__main__':
    """ example usage """   

    # print a static progress bar
    #  [##########       25%                  ]  15s/60s

    p = ProgressBar(60)
    p.update_time(15)
    print 'static progress bar:'
    print p


    # print a static progress bar
    #  [=================83%============      ]  25s/30s

    p = ProgressBar(30)
    p.fill_char = '='
    p.update_time(25)
    print 'static progress bar:'
    print p


    # print a dynamic updating progress bar on one line:
    #
    #  [################100%##################]  10s/10s
    #  done

    secs = 10
    p = ProgressBar(secs)
    print 'dynamic updating progress bar:'
    print 'please wait %d seconds...' % secs

    # spawn asych (threads/processes/etc) code here that runs for secs.
    # the call to .animate() blocks the main thread.

    p.animate()

    print 'done'

this might be useful example:

http://code.google.com/p/corey-projects/source/browse/trunk/python2/progress_bar.py

import sys
import time


class ProgressBar:
    def __init__(self, duration):
        self.duration = duration
        self.prog_bar = '[]'
        self.fill_char = '#'
        self.width = 40
        self.__update_amount(0)

    def animate(self):
        for i in range(self.duration):
            if sys.platform.lower().startswith('win'):
                print self, '\r',
            else:
                print self, chr(27) + '[A'
            self.update_time(i + 1)
            time.sleep(1) 
        print self

    def update_time(self, elapsed_secs):
        self.__update_amount((elapsed_secs / float(self.duration)) * 100.0)
        self.prog_bar += '  %ds/%ss' % (elapsed_secs, self.duration)

    def __update_amount(self, new_amount):
        percent_done = int(round((new_amount / 100.0) * 100.0))
        all_full = self.width - 2
        num_hashes = int(round((percent_done / 100.0) * all_full))
        self.prog_bar = '[' + self.fill_char * num_hashes + ' ' * (all_full - num_hashes) + ']'
        pct_place = (len(self.prog_bar) / 2) - len(str(percent_done))
        pct_string = '%d%%' % percent_done
        self.prog_bar = self.prog_bar[0:pct_place] + \
            (pct_string + self.prog_bar[pct_place + len(pct_string):])

    def __str__(self):
        return str(self.prog_bar)


if __name__ == '__main__':
    """ example usage """   

    # print a static progress bar
    #  [##########       25%                  ]  15s/60s

    p = ProgressBar(60)
    p.update_time(15)
    print 'static progress bar:'
    print p


    # print a static progress bar
    #  [=================83%============      ]  25s/30s

    p = ProgressBar(30)
    p.fill_char = '='
    p.update_time(25)
    print 'static progress bar:'
    print p


    # print a dynamic updating progress bar on one line:
    #
    #  [################100%##################]  10s/10s
    #  done

    secs = 10
    p = ProgressBar(secs)
    print 'dynamic updating progress bar:'
    print 'please wait %d seconds...' % secs

    # spawn asych (threads/processes/etc) code here that runs for secs.
    # the call to .animate() blocks the main thread.

    p.animate()

    print 'done'
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文