Python制作进度条

Python制作进度条

1号进度条
import datetime
import time

class Tiao(object):
    def __init__(self):
        self.obj1=datetime.timedelta(seconds=1)
        self.var='%H:%M:%S'

    def add(self,var):
        a=datetime.datetime.strptime(var,self.var)
        b=a+self.obj1
        return str(b.strftime(self.var))

    def str_time(self,var3):
        a,b,c=[int(i) for i in var3.split(":")]
        a*=3600
        b*=60
        return a+b+c

    def yuan(self,var1,var2):
        """
        计算百分比
        var1:现在的时间
        var2:总时间
        """
        var1=self.str_time(var1)
        var2=self.str_time(var2)
        return '{:.2f}'.format(var1/var2)

def show(var):
    a=Tiao()
    b="00:00:00"
    while True:
        if var ==b:
            break;
        b=a.add(b)
        print("{0}/{1} ({2}%)".format(b,var,a.yuan(b,var)))
        # print('\r%s/%s (%s)'%(b,var,a.yuan(b,var)),end='')
        time.sleep(0.5)

init="00:00:37"
show(init)

2号进度条
import time


class Index(object):
    def __init__(self,number=50,decimal=2):
        """
        param decimal:保留的小数点
        param number : # 号的个数
        """
        self.decimal=decimal
        self.number=number
        self.a=100/number # 百分比,判断几时增加一个#号

    def __call__(self,now,total):
        """

        """
        # 1.获取当前的百分比
        percentage=self.percentage_number(now,total)

        # 2.根据现在百分比计算
        well_num=int(percentage/self.a)

        # 3.打印字符进度条
        prograss_bar_num=self.prograss_bar(well_num)

        # 4.完成进度条
        return "\r%s %s"%(prograss_bar_num,percentage)

    def percentage_number(self,now,total):
        """
        计算百分比
        param   now:现在的数
        param total:总数
        """
        return round(now/total*100,self.decimal)

    def prograss_bar(self,num):
        """
        显示进度条的位置
        """
        # 1."#"号的个数
        well_num="#"*num

        # 2. 空格的个数
        space_num=" "*(self.number-num)

        return '[%s%s]'%(well_num,space_num)


index=Index()
start=37
for i in range(start+1):
    print(index(i,start)+"%",end='')
    time.sleep(0.3)





3号进度条
import time,sys

for i in range(50):
    sys.stdout.write("#")
    sys.stdout.flush()
    time.sleep(0.1)

参考地址

你可能感兴趣的:(Python)