python手動程式設計完成進度條提示

2021-10-05 17:11:49 字數 2568 閱讀 1874

進度條的顯示,重點是不能換行,因此需要sys.stdout.write和sys.stdout.flush(),以50個空格為乙個進度條,完成使用█,實現整體效果,如下圖**所示,是本人實現的延時進度條,流程處理進度條例項:

import sys

class

showprocess()

:#####延遲並提示進度

defsleepandshowprogress

(self, sleeptime, tips=

"add"):

d =0 sleeppertime=sleeptime/

10for data in

range(1

,11):

time.sleep(sleeppertime)

d +=

1 done =

int(

50* d /10)

sys.stdout.write(

"\r[%s%s] %d%% --[sleep %s s] [%s]"%(

'█'* done,

' '*(50

- done),10

* d, sleeptime, tips)

) sys.stdout.flush(

)####顯示普通進度條

defindexshowprogress

(self, index, maxval=

100, maxlenth =

50, tips=

"add"):

for data in

range(0

, maxval)

: done =

int(maxlenth * index / maxval)

percent_sum = index/maxval*

100 sys.stdout.write(

"\r[%s%s] %d%% --[sum:%d - index:%d] [%s]"%(

'█'* done,

' '*

(maxlenth - done)

, percent_sum, maxval, index, tips)

) sys.stdout.flush(

)#####顯示紅色進度條

defindexshowprogressred

(self, index, maxval=

100, maxlenth =

50, tips=

"add"):

for data in

range(0

, maxval)

: done =

int(maxlenth * index / maxval)

percent_sum = index/maxval*

100 sys.stdout.write(

"\r\033[31m[%s%s] %d%% --[sum:%d - index:%d] [%s]\033[0m"%(

'█'* done,

' '*

(maxlenth - done)

, percent_sum, maxval, index, tips)

) sys.stdout.flush(

)#####顯示綠色進度條

defindexshowprogressgreen

(self, index, maxval=

100, maxlenth =

50, tips=

"add"

, delaycurrent=

float(0

)):for data in

range(0

, maxval)

: done =

int(maxlenth * index / maxval)

percent_sum = index/maxval*

100if

(delaycurrent ==0)

: sys.stdout.write(

"\r\033[32m[%s%s] %d%%[sum:%d-index:%d][%s]\033[0m "%(

'█'* done,

' '*

(maxlenth - done)

, percent_sum, maxval, index, tips)

)else

: sys.stdout.write(

"\r\033[32m[%s%s] %d%%[sum:%d-index:%d][%s][延遲:%.2f]\033[0m "%(

'█'* done,

' '*

(maxlenth - done)

, percent_sum, maxval, index, tips, delaycurrent)

) sys.stdout.flush(

)

Python程式設計 文字進度條

textprobar.py import time scale 50print 執行開始 center scale 2,start time.perf counter for i in range 1 scale 1 a i b scale i c i scale 100 dur time.perf...

Python 模擬進度條

usr bin python coding utf 8 import time import sys print sys.argv 命令列引數list,第乙個元素是程式本身路徑 print sys.exit goodbye 退出程式 sys.version 獲取python 的版本資訊 sys.ma...

python 列印進度條

列印進度條 coding utf 8 import sys import time def progress percent,width 50 進度條列印 if percent 100 percent 100 show str ds width int width percent 100 字串拼接的...