Python动态进度条N种整理

Python动态进度条N种整理

我们在规范代码的时候,经常需要查看代码运行的状态,往往需要进度条来观察任务进行到什么程度了,这里就汇总了几种动态的进度条以供参考。

# -*- encoding: utf-8 -*-
'''
@Software:   PyCharm
@Project :   Progressbar
@Time    :   2021-10-12 11:19
@Author  :   yanpenggong
@Contact :   yanpenggong@163.com
@Version :   1.0
'''
import sys, time


def method1():
	"""使用 print 功能 进行实时刷新进度条"""
	t = 100  # for遍历的次数
	print("**************print 功能 进行实时刷新进度条**************")
	percent_n = t / 100  # 百分比
	print_len = 40  # 设置打印的进度条长度
	for i in range(1,t+1):
		percent_score = int(i / percent_n)
		print("\r", end="")
		print(f"进度: {'▓' * int(i // (percent_n * 2))} {percent_score}%", end="")
		sys.stdout.flush()
		time.sleep(0.01)  # 假设代码部分需要0.01s
	print("\n")


def method2():
	"""使用 print 功能进行实时刷新进度条,并加入时间(显示进度过程中所需要的时间)"""
	t = 60
	print("**************带时间的进度条**************")
	start = time.perf_counter()
	for i in range(t + 1):
		finish = "▓" * i
		need_do = "-" * (t - i)
		progress = (i / t) * 100
		dur = time.perf_counter() - start
		print("\r进度:{:^3.0f}%[{}{}]{:.2f}s".format(progress, finish, need_do, dur), end="")
		time.sleep(0.05)  # 假设代码部分需要0.05s


def method3():
	"""
	tqdm库, 专门用于进度条的一个Python库
	官方链接:https://pypi.org/project/tqdm/#description
	"""
	from tqdm import tqdm
	print("\n**************tqdm 的进度条**************")
	a = 0
	for i in tqdm(range(1, 60)):
		"""
		此处填需要用到的代码
		"""
		a += i

		time.sleep(0.05)  # 假设代码部分需要0.05s
	print(f"a: {a}")


def method4():
	"""
	alive_progress库, 动态的实时显示进度条库
	官方链接: https://pypi.org/project/alive-progress/#description
	"""
	from alive_progress import alive_bar
	print("\n**************alive_progress 的进度条**************")
	a = 0
	with alive_bar(len(range(100))) as bar:
		for item in range(100):  # 遍历任务
			bar()  # 显示进度
			"""
			此处填需要用到的代码
			"""
			a += 1
			time.sleep(0.05)  # 假设代码部分需要0.05s


def method5():
	"""
	PySimpleGUI库, 动态的进度条,自带GUI界面(基于PyQt, Tkinter等)
	官方链接: https://pypi.org/project/PySimpleGUI/#description
	"""
	import PySimpleGUI as sg
	print("\n**************PySimpleGUI 的进度条**************")

	a = 0
	count = range(100)
	for i, item in enumerate(count):
		sg.one_line_progress_meter("实时进度条", i + 1, len(count), "--keys--")
		"""
		此处填需要用到的代码
		"""
		a += 1
		time.sleep(0.05) # 假设代码部分需要0.05s


def method6():
	"""
	progressbar库
	官方链接: https://pypi.org/project/progressbar/#description
	"""
	import progressbar
	print("\n**************progressbar 的进度条**************")

	a = 0
	p = progressbar.ProgressBar()
	# 假设需要执行100个任务,放到ProgressBar()for i in p(range(100)):
		"""
		此处填需要用到的代码
		"""
		a += 1
		time.sleep(0.05) # 假设代码部分需要0.05s


def main():
	# 方法1: 普通进度条
	method1()

	# 方法2: 带时间的普通进度条
	method2()

	# 方法3: tqdm库
	method3()

	# 方法4: alive_progress库
	method4()

	# 方法5: PySimpleGUI库
	method5()

	# 方法6: progressbar库
	method6()



if __name__ == '__main__':
	main()
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Kungs8

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值