赞
踩
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()
一:
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()
二:
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()
三(带时间):
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,"-"))
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。