目录
用过计算机的都见过
如何获得文本进度条的变化时间?
# TextProBarV1.py
import time
scale = 10
print("------执行开始------")
for i in range(scale + 1):
a = '*' * i
b = '.' * (scale - i)
c = (i / scale) * 100
print("{:^3.0f}%[{}->{}]".format(c, a, b))
time.sleep(0.1)
print("------执行结束------")
------执行开始------
0 %[->..........]
10 %[*->.........]
20 %[**->........]
30 %[***->.......]
40 %[****->......]
50 %[*****->.....]
60 %[******->....]
70 %[*******->...]
80 %[********->..]
90 %[*********->.]
100%[**********->]
------执行结束------
刷新的关键是 \r
print()
需要被控制\r
注意:IDLE如Pycharm屏蔽了\r
功能
# TextProBarV2.py
import time
for i in range(101):
print("\r{:3}%".format(i), end="")
time.sleep(0.1)
100%
# TextProBarV1.py
import time
scale = 10
print("执行开始".center(scale // 2, "-"))
start = time.perf_counter()
for i in range(scale + 1):
a = '*' * i
b = '.' * (scale - i)
c = (i / scale) * 100
dur = time.perf_counter() - start
print("\r{:^3.0f}%[{}->{}]{:.2f}s".format(c, a, b, dur), end='')
time.sleep(0.1)
print("\n" + "执行结束".center(scale // 2, '-'))
-执行开始
100%[**********->]1.03s
-执行结束
计算问题扩展
perf_counter()
计时进度条应用
文本进度条的不同设计函数
设计名称 | 趋势 | 设计函数 |
---|---|---|
Linear | Constant | \(f(x) = x\) |
Early Pause | Speeds up | \(f(x) = x+(1-sin(x*π*2+π/2)/-8\) |
Late Pause | Slows down | \(f(x) = x+(1-sin(x*π*2+π/2)/8\) |
Slow Wavy | Constant | \(f(x) = x+(1-sin(x*π*2+π/2)/8\) |
Fast Wavy | Constant | \(f(x) = x+(1-sin(x*π*2+π/2)/8\) |
Power | Speeds up | \(f(x) = {(x+(1-x)*0.03)}^2\) |
Inverse Power | Slows down | \(f(x) =1+{(1-x)}^{1.5}*-1\) |
Fast Power | Speeds up | \(f(x) = {(x+(1-x)/2)}^8\) |
Inverse Fast Power | Slows down | \(f(x) = 1+{(1-x)}^3*-1\) |
原文:https://www.cnblogs.com/nickchen121/p/11183608.html