Python專案實踐 文字進度條

2021-10-19 08:19:02 字數 3838 閱讀 4014

採用字串方式列印可以動態變化的文字進度條

# textprobarv1.py

import time

scale =

10print

("------執行開始------"

)for i in

range

(scale +1)

: a =

"*"* i

b ="-"*

(scale - i)

c =(i / scale)

*100

print

("%[{}->{}]"

.format

(c, a, b)

) time.sleep(

0.1)

print

("------執行結束------"

重新整理的本質:用後列印的字元覆蓋之前的字元

# textprobarv2.py

import time

for i in

range

(101):

print

("\r%"

.format

(i), end="")

time.sleep(

0.1)

# textprobarv3.py

import time

scale =

50print

("執行開始"

.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%[{}->{}]s"

.format

(c, a, b, c)

, end="")

time.sleep(

0.1)

print

("\n"

+"執行結束"

文字進度條程式使用了time.perf_counter()計時計時

進度條

# textprobarv4.py

import time

from math import sin

scale =

50print

("執行開始"

.center(scale //2,

"-")

)start = time.perf_counter(

)for i in

range

(scale +1)

: x = i +(1

- sin(i *

3.14*2

+3.14/2

))/-

8 a =

"*"*

int(x)

b ="-"*

(scale -

int(x)

) c =

(i / scale)

*100

dur = time.perf_counter(

)- start

# print("\r%[{}->{}]耗時s".format(c, a, b, c), end="")

print

("\r%[{}->{}]"

.format

(c, a, b)

, end="")

time.sleep(

0.1)

print

("\n"

+"執行結束"

.center(scale //2,

"-")

)

power:speeds up

# textprobarv4.py

import time

scale =

10print

("執行開始"

.center(scale //2,

"-")

)start = time.perf_counter(

)for i in

range

(scale +1)

: x =

(i +(1

- i)

*0.03)**

2 a =

"*"*

int(x)

b ="-"*

(scale -

int(x)

) c =

(i / scale)

*100

dur = time.perf_counter(

)- start

# print("\r%[{}->{}]耗時s".format(c, a, b, dur), end="")

print

("\r%[{}->{}]s"

.format

(c, a, b, dur)

, end="")

time.sleep(

0.4)

print

("\n"

+"執行結束"

.center(scale //2,

"-")

)

# progressbarv5.py

progressbefore =

0def

printprogressbar

(percent, prefix ='')

:global progressbefore

if percent - progressbefore <

0.001

:# 只有當進度達到千分之一才重新整理,避免頻繁重新整理進度條

return

progressbefore = percent

percentstr ="".

format

(percent*

100)

filledlength =

int(

30* percent)

bar =

'█'* filledlength +

'-'*(30

- filledlength)

print

('\r%s |%s| %s%% '

%(prefix, bar, percentstr)

, end='')

if __name__ ==

"__main__"

:import time

for i in

range

(1000):

printprogressbar(

(i+1)/

1000

, prefix=

"progress:"

) time.sleep(

0.01

)# 當前程序暫停0.01s

ps:參閱python123.i

python 文字進度條

textprobarv.py import time scale 50 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...

python 文字進度條

需求分析 1 採用字串方式列印可以動態變化的文字進度條 2 進度條需要能在一行中逐漸變化 問題分析 如何獲得文字進度條的變化時間?採用sleep 模擬乙個持續的進度!簡單的開始 import time scale 10 print 執行開始 for i in range scale 1 a i b ...

python 文字進度條

這是乙個利用格式化輸出和時間延遲實現控制颱風格式文字進度條 print 函式在輸出結尾處會自動生成乙個 n 即換行符,從而讓游標移動到下一行行首 import time 引入time函式庫 scale 10print 執行開始 for i in range scale 1 a,b i,scale i...