关于#python#进度条的问题,请各位专家解答!

#遇到问题的现象

#返回: 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

\r不换行,会从上一行的开头进行覆盖,你覆盖之后上一行的1 to go没有被完全覆盖,所以后面还有to go
你在完成对应的字符串后面加几个空格就没问题了
有帮助望采纳

我已解决:
\r不换行,会从上一行的开头进行覆盖,上一行的1 to go没有被完全覆盖,所以后面还有to go
解决代码:

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 = "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)
    string1 = " Finish!!! {0} |{1}{2}| {5}/{5} (100%)"
    string1 = string1.format(content,current_length,current_location,remaining_length,current_progress,overall_progress,percentage,remaining_progress)
    if percentage == 100:
        difference_space = len(string)-len(string1)+10
        string = string1 + difference_space * " "
    return (string)

def progressbar(count, prefix="", times = 0.2, ridesize = 1, stylelist=["=","□","-"], file=sys.stdout):
    it = range(1,count+1)
    text = get_progress_bar(prefix,0,count,ridesize,[stylelist[2],stylelist[2],stylelist[2]])
    file.write("\r"+text)
    for item in it:
        time.sleep(times)
        text = get_progress_bar(prefix,item,count,ridesize,stylelist)
        file.write("\r"+text)
    file.write("\n")

progressbar(count=5, prefix="Python", times = 0.5, ridesize = 1)

difference_space = len(string)-len(string1)+10
此处+10确保能完全覆盖