#遇到问题的现象
#返回: Finish!!! Python |====□| 5/5 (100%)1 to go
#原本应该返回: Finish!!! Python |====□| 5/5 (100%)
#问题相关代码片
import time,sys
def get_progress_bar(content,current_progress,overall_progress,size=1,stylelist=["=","□","-"]):
# 参数 content,current_progress,overall_progress,size
# 计算
remaining_progress=overall_progress-current_progress
current_length=int(current_progress*size-1)*stylelist[0]
current_location=stylelist[1]
remaining_length=int(remaining_progress*size)*stylelist[2]
percentage=int((current_progress/overall_progress)*100)
string = ""
if current_progress == overall_progress:
string = " Finish!!! {0} |{1}{2}| {5}/{5} (100%)"
else:
string = "Working... {0} |{1}{2}{3}| {4}/{5} ({6}%) {7} to go"
string = string.format(content,current_length,current_location,remaining_length,current_progress,overall_progress,percentage,remaining_progress)
return (string)
def progressbar(count, prefix="", times = 0.2, ridesize = 1, stylelist=["=","□","-"], file=sys.stdout):
it = range(1,count+1)
file.write(get_progress_bar(prefix,0,count-1,ridesize,[stylelist[0],stylelist[2],stylelist[2]]))
for item in it:
time.sleep(times)
file.write("\r")
file.write(get_progress_bar(prefix,item,count,ridesize,stylelist))
file.write("\n")
progressbar(count=5, prefix="Python", times = 0.5, ridesize = 1)
#运行结果
Working... Python |-----| 0/5 (0%)5 to go
Working... Python |□----| 1/5 (20%)4 to go
Working... Python |=□---| 2/5 (40%)3 to go
Working... Python |==□--| 3/5 (60%)2 to go
Working... Python |===□-| 4/5 (80%)1 to go
Finish!!! Python |====□| 5/5 (100%)1 to go
<--一次只会显示一行--!>
#报错内容
无报错内容
#操作环境、软件版本等相关信息
操作环境:Visual Studio Code
软件版本:1.59.1