为什么python进度条在完成下载后启动

问题描述

我在我的 python 程序中放了一个进度条, 但是下载完成后就开始了。

我下载了一个文件,它在很长时间后才开始。 当进度条从 0% 开始时,文件已经下载。

我能为它做什么? 我需要它在下载开始时显示

完整代码https://github.com/GH0STH4CKER/Youtube_to_mp3/blob/main/Youtube_to_mp3.py

进度条代码

def download(url,fname):
    resp = requests.get(url,stream=True)
    total = float(content_length)
    with open(fname,'wb') as file,tqdm(
            desc=fname,total=total,unit='iB',unit_scale=True,unit_divisor=1024,) as bar:
        for data in resp.iter_content(chunk_size=1024):
            size = file.write(data)
            bar.update(size)

download(d_link,songname)    

解决方法

也许尝试在不同的线程中运行它,因为我认为它会在之后执行

,
    def progres_(self,streams,chunk,bytes_remaining):
        percentage = (float(abs(bytes_remaining - self.size_inBytes) / self.size_inBytes)) * float(100)
        self.progres_bar["value"] = percentage
        self.progres_bar.update()
        self.lbl_percentage.config(text=f" Downloading: {str(round(percentage,2))}%")
        if round(percentage,2) == 100:
            self.lbl_message.config(text="Download Completed",fg="green")
            self.btn_download.config(state=tk.DISABLED)