易三一世
1.方式一:
1.1 代码
from __future__ import division
import sys,time
j = '#'
if __name__ == '__main__':
for i in range(1,61):
j += '#'
sys.stdout.write(str(int((i/60)*100))+'% '+j+'->'+ "
")
sys.stdout.flush()
time.sleep(0.1)
1.2 效果,jdt1图片
2.方式二:
2.1 代码
import sys
from time import sleep
def viewBar(i):
output = sys.stdout
for count in range(0, i + 1):
second = 0.1
sleep(second)
output.write('
complete percent ----->:%.0f%%' % count)
output.flush()
viewBar(100)
2.2 效果图,jdt2图片
3. 方式三:
3.1 安装tqdm模块
pip3.8 install tqdm #注意一般电脑是安装是pip install tqdm
3.2 代码:
from time import sleep #这个也可以为:import time即可
from tqdm import tqdm
for i in tqdm(range(1, 500)):
sleep(0.01)
3.3 效果图,jdt3图片
4.方式四:
4.1 代码
class ProgressBar():
def __init__(self, width=50):
self.pointer = 0
self.width = width
def __call__(self,x):
# x in percent
self.pointer = int(self.width*(x/100.0))
return "|" + "#"*self.pointer + "-"*(self.width-self.pointer)+ "|
%d percent done" % int(x)
if __name__ == '__main__':
import time,os
pb = ProgressBar()
for i in range(101):
os.system('clear')
print( pb(i))
time.sleep(0.1)
4.2 效果图,jdt4图片
5.方式五
5.1 代码
import sys
import time
def view_bar(num,total):
rate = num / total
rate_num = int(rate * 100)
r = '
%s>%d%%' % ('=' * rate_num, rate_num,)
sys.stdout.write(r)
sys.stdout.flush
if __name__ == '__main__':
for i in range(0, 101):
time.sleep(0.1)
view_bar(i, 100)
5.2 效果图,jdt5图片
6.方式六
6.1 代码
import time
for i in range(1,101):
print('>'*i+f'{i}%' + '
',end='')
time.sleep(0.1)
6.2 效果图。jdt6图片
7.方式七
7.1 代码
import sys,time
print("正在下载:...")
for i in range(101):
sys.stdout.write("->"+str(i)+"%")
sys.stdout.flush()
time.sleep(0.1)
print("
"+"下载完成!")
7.2 效果图,jdt7图片
8.方式八:
8.1 代码:
import sys, time
class ShowProcess():
i = 0 # 当前的处理进度
max_steps = 0 # 总共需要处理的次数
max_arrow = 50 #进度条的长度
infoDone = 'done'
# 初始化函数,需要知道总共的处理次数
def __init__(self, max_steps, infoDone = 'Done'):
self.max_steps = max_steps
self.i = 0
self.infoDone = infoDone
# 显示函数,根据当前的处理进度i显示进度
# 效果为[>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>]100.00%
def show_process(self, i=None):
if i is not None:
self.i = i
else:
self.i += 1
num_arrow = int(self.i * self.max_arrow / self.max_steps) #计算显示多少个'>'
num_line = self.max_arrow - num_arrow #计算显示多少个'-'
percent = self.i * 100.0 / self.max_steps #计算完成进度,格式为xx.xx%
process_bar = '[' + '>' * num_arrow + '-' * num_line + ']'
+ '%.2f' % percent + '%' + '
' #带输出的字符串,'
'表示不换行回到最左边
sys.stdout.write(process_bar) #这两句打印字符到终端
sys.stdout.flush()
if self.i >= self.max_steps:
self.close()
def close(self):
print('')
print(self.infoDone)
self.i = 0
if __name__=='__main__':
max_steps = 100
process_bar = ShowProcess(max_steps, 'OK')
for i in range(max_steps):
process_bar.show_process()
time.sleep(0.01)
8.2效果图,jdt8图片
总结完美,我比较喜欢,简单代码的。进度条3
因为安装tqdm模块,所以代码简单,好看。
你喜欢吗?