python使用tkinter添加下载进度UI

  1. 添加进度组件

    python 复制代码
    def download():          
                # 创建下载进度框
                progress_window = tk.Toplevel()
                progress_window.title("下载进度")
                progress_label = tk.Label(progress_window, text="正在下载视频文件...")
                progress_label.pack(pady=10)
                progress_bar = ttk.Progressbar(progress_window, orient="horizontal", length=300, mode="determinate")
                progress_bar.pack(pady=10)
    
                # 获取窗口宽高并居中显示
                progress_window.update_idletasks()
                window_width = progress_window.winfo_width()
                window_height = progress_window.winfo_height()
                screen_width = progress_window.winfo_screenwidth()
                screen_height = progress_window.winfo_screenheight()
                x = (screen_width - window_width) // 2
                y = (screen_height - window_height) // 2
                progress_window.geometry(f"{window_width}x{window_height}+{x}+{y}")
  2. 创建进度更新方法

    python 复制代码
    def update_progress(progress):
                    progress_bar['value'] = progress
                    progress_window.update_idletasks()
  3. 调用下载方法,传入进度更新方法

    python 复制代码
    ls_setoption.get_dump_video(video_dir, progress_callback=update_progress)
    
    progress_window.destroy()
  4. 使用子线程调用下载方法,注意用子线程,否则下载过程UI会卡死,进度不更新

    python 复制代码
        download_thread = threading.Thread(target=download)
        download_thread.start()
相关推荐
IvanCodes10 小时前
Python 数据处理(十三):JSON、CSV 与数据序列化
开发语言·python
Patrick在香港11 小时前
Claude 工具调用返回空:8 次失败里只有 1 次状态码不对,其余全带 200
爬虫·python·api·claude·香港
Web3&Basketball11 小时前
CRM Agent 后训练实战:3 倍更少错误
python·架构·大模型·agent·推理
AIFQuant12 小时前
ETF行情API接入踩坑记:从报错到跑通的七个问题
python·金融·区块链·etf·基金
GPU实战笔记14 小时前
云端 Python 开发:JupyterLab 还是 VS Code Remote-SSH?
python·vs code·jupyterlab·remote-ssh·远程开发
狗狗狗狗狗乐啊14 小时前
搭一个 AI 对话工作台 AChat:从 0 到可用的完整记录(一)
python·react.js·ai编程
白猫不黑15 小时前
Python实现简易Web弱口令爆破与防护方案
python·web安全·计算机·网络安全·黑客·信息安全·渗透测试
kuuailetianzi15 小时前
Python初识:定位、优势与发展历程
python
guoran_shini15 小时前
LLM微调-训练垂类问答模型
python·lora·sft
basketball61615 小时前
Python FastAPI 介绍以及常用方法
python·fastapi·vllm·ai infra