当前位置:   article > 正文

python --进度条(tk下载进度条)_python tkinter下载文件进度条

python tkinter下载文件进度条

windows下tk下载进度条

import threading
import multiprocessing
import requests
import tkinter as tk
from tkinter import ttk



class JinDuTiao(object):
    '''进度条'''

    def __init__(self):
        self.root = tk.Tk()
        self.root.overrideredirect(True)  # 去掉窗口边框

        # 获取屏幕宽度和高度
        screen_width = self.root.winfo_screenwidth()
        screen_height = self.root.winfo_screenheight()

        # 设置窗口大小和位置
        window_width = 300
        window_height = 45
        x = (screen_width - window_width) // 2
        y = (screen_height - window_height) // 2
        self.root.geometry(f"{window_width}x{window_height}+{x}+{y}")  # 设置窗口剧中

        self.progress_bar = ttk.Progressbar(self.root, orient='horizontal', length=300, mode='determinate')
        self.progress_bar.pack()

        self.percentage_label = tk.Label(self.root, text="")
        self.percentage_label.pack()

    def download_file(self, url, save_path):
        response = requests.get(url, stream=True)
        total_size = 28740241

        with open(save_path, 'wb') as file:
            self.progress_bar['maximum'] = total_size
            downloaded = 0

            for data in response.iter_content(chunk_size=1024):
                file.write(data)
                downloaded += len(data)
                self.progress_bar['value'] = downloaded
                percentage = (downloaded / total_size) * 100
                self.percentage_label.config(text=f"edg最新最资源包下载进度:{percentage:.2f}%")
                self.root.update_idletasks()
        self.root.destroy()

    def start(self):
        url = "http://118.178.137.66/static/106.zip"  # 下载地址
        save_path = "106.zip"  # 下载后保存文件名

        # 创建一个新线程来执行文件下载操作
        download_thread = threading.Thread(target=self.download_file, args=(url, save_path))
        download_thread.start()
        self.root.mainloop()

if __name__ == '__main__':
    a = JinDuTiao()
    a.start()

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62

一:

import time
import random


def progress_bar():
    for i in range(1, 300):
        print("Download progress: {}%: ".format(i), "▋" * (i // 2), f'{i}/100')
        time.sleep(random.random())


if __name__ == '__main__':
    progress_bar()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

二:

import sys
import time
 
 
def progress_bar():
  for i in range(1, 101):
    print("\r", end="")
    print("Download progress: {}%: ".format(i), "▋" * (i // 2), end="")
    sys.stdout.flush()
    time.sleep(0.05)
 
if __name__ == '__main__':
  progress_bar()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

三(带时间):

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_counter() - start
  print("\r{:^3.0f}%[{}->{}]{:.2f}s".format(c,a,b,dur),end = "")
  time.sleep(0.1)
 
print("\n"+"执行结束,万幸".center(scale // 2,"-"))
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/知新_RL/article/detail/761207
推荐阅读
相关标签
  

闽ICP备14008679号