Python progressbar源码修改(支持设置进度条样式)

Python progressbar源码修改(支持设置进度条样式)

                                          By qianghaohao(CodeNutter)

     默认progressbar进度条是#字符,用起来太单调,如果要设置更加美观的进度条,需要修改下源码,
修改了progressbar目录的__init__.py的ProgressBar类的构造函数,给构造函数添加了一个设置进度
条样式的参数(bar_style),在构造函数传递进度条样式参数bar_style,默认为bar_style='BST',代表
#样式,本来想改成可以通过参数直接传递进度条的样式字符,但是如果这样会存在编码问题,对初学
者使用不方便,因此通过传递各个样式的编号(BSTx)来选择不同的样式。
    在此添加了9种样式可供选择:
Python progressbar源码修改(支持设置进度条样式)_第1张图片
    修改后源码(progress目录__init__.py文件ProgressBar类的__init__(...)构造函数):
    修改后源码已上传到:http://download.csdn.net/detail/qianghaohao/9605256
    # ------------------Modified By qianghaohao--------------------------
    # _DEFAULT_WIDGETS = [Percentage(), ' ', Bar()]
    # Modified by qianghaohao
    # Date: 2016-8-16
    #
    # -------------------------------------------------------------------
    def __init__(self, maxval=None, widgets=None, term_width=None, poll=1,
                 left_justify=True, fd=sys.stderr, bar_style = 'BST1'):
        '''Initializes a progress bar with sane defaults'''

        # Don't share a reference with any other progress bars
        # ------------------Modified By qianghaohao--------------------------
        # Don't share a reference with any other progress bars
        #
        # Modified by qianghaohao
        # Date: 2016-8-16
        # --------------------------------------- bar_style ----------------------------------------
        #             BST1     BST2      BST3    BST4      BST5    BST6     BST7     BST8    BST9
        # bar_style = (u'#',   u'▏',    u'▎',   u'▍',    u'▌',   u'▋',   u'▊',   u'▉',   u'█')
        # ------------------------------------------------------------------------------------------
        bst = {
                'BST1':u'#',
                'BST2':u'▏',
                'BST3':u'▎',
                'BST4':u'▍',
                'BST5':u'▌',
                'BST6':u'▋',
                'BST7':u'▊',
                'BST8':u'▉',
                'BST9':u'█'
              }
        default_widgets = [Percentage(), ' ']
        default_widgets.append(Bar(bst[bar_style]))
        # -------------------------------------------------------------------

        if widgets is None:
            widgets = list(default_widgets)

        self.maxval = maxval
        self.widgets = widgets
        self.fd = fd
        self.left_justify = left_justify

        self.signal_set = False
        if term_width is not None:
            self.term_width = term_width
        else:
            try:
                self._handle_resize()
                signal.signal(signal.SIGWINCH, self._handle_resize)
                self.signal_set = True
            except (SystemExit, KeyboardInterrupt): raise
            except:
                self.term_width = self._env_size()

        self.__iterable = None
        self._update_widgets()
        self.currval = 0
        self.finished = False
        self.last_update_time = None
        self.poll = poll
        self.seconds_elapsed = 0
        self.start_time = None
        self.update_interval = 1
     使用方法:progressbar的使用还是和原先的一样,只不过在使用时可以给ProgressBar传递进度条样式参数,
默认还是‘#’样式,如果需要更加美观的进度条,则传递bar_style参数,赋值为对应的编号(BSTx)即可。使用时
将progress目录放到工程目录即可直接使用。
 使用示例代码:
import sys, time
from progressbar import *

total = 10

progress1 = ProgressBar(bar_style='BST1')
progress2 = ProgressBar(bar_style='BST2')
progress3 = ProgressBar(bar_style='BST3')
progress4 = ProgressBar(bar_style='BST4')
progress5 = ProgressBar(bar_style='BST5')
progress6 = ProgressBar(bar_style='BST6')
progress7 = ProgressBar(bar_style='BST7')
progress8 = ProgressBar(bar_style='BST8')
progress9 = ProgressBar(bar_style='BST9')

for i in progress1(range(total)):
    time.sleep(0.1)
print  '\n'

for i in progress2(range(total)):
    time.sleep(0.1)
print  '\n'

for i in progress3(range(total)):
    time.sleep(0.1)
print  '\n'

for i in progress4(range(total)):
    time.sleep(0.1)
print  '\n'

for i in progress5(range(total)):
    time.sleep(0.1)
print  '\n'

for i in progress6(range(total)):
    time.sleep(0.1)
print  '\n'

for i in progress7(range(total)):
    time.sleep(0.1)
print  '\n'

for i in progress8(range(total)):
    time.sleep(0.1)
print  '\n'

for i in progress9(range(total)):
    time.sleep(0.1)
运行效果:
Python progressbar源码修改(支持设置进度条样式)_第2张图片
修改后源码地址:
            http://download.csdn.net/detail/qianghaohao/9605256
   

你可能感兴趣的:(Python)