本题目要求生成一个可自动刷新的文本进度条,运行效果如下:
时间: 2024-03-12 12:47:13 浏览: 26
好的,我可以帮你实现一个Python的文本进度条。你可以将下面的代码复制到你的Python文件中运行:
```python
import time
def progress_bar(iteration, total, prefix='', suffix='', decimals=1, length=100, fill='█'):
"""
可自动刷新的文本进度条
:param iteration: 当前迭代次数
:param total: 总迭代次数
:param prefix: 进度条前缀
:param suffix: 进度条后缀
:param decimals: 进度条百分比保留小数位数
:param length: 进度条长度
:param fill: 进度条填充字符
"""
percent = ("{0:." + str(decimals) + "f}").format(100 * (iteration / float(total)))
filled_length = int(length * iteration // total)
bar = fill * filled_length + '-' * (length - filled_length)
print('\r%s |%s| %s%% %s' % (prefix, bar, percent, suffix), end='\r')
# 避免换行
if iteration == total:
print()
# 示例
total = 1000
for i in range(total):
time.sleep(0.01) # 模拟耗时操作
progress_bar(i + 1, total, prefix='Progress:', suffix='Complete', length=50)
```
这段代码实现了一个可以自动刷新的文本进度条,可以根据你的需要调整进度条的前缀、后缀、长度、填充字符等参数。你只需要在需要显示进度条的地方调用 `progress_bar` 函数即可。
希望这个代码对你有所帮助!
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)