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()
相关推荐
金銀銅鐵1 小时前
[Python] 从《千字文》中随机挑选汉字
后端·python
cup116 小时前
[技术复盘] Windows Python 打包实战:Nuitka 环境踩坑总结与 CI 自动化构建全指南
python·ai·环境变量·ci·nuitka·skill
aqi008 小时前
15天学会AI应用开发(七)有了大模型为什么还要引入RAG
人工智能·python·大模型·ai编程·ai应用
金銀銅鐵10 小时前
用 Python 实现 Take-Away 游戏
python·游戏
copyer_xyf10 小时前
Agent 流程编排
后端·python·agent
copyer_xyf11 小时前
Agent RAG
后端·python·agent
copyer_xyf11 小时前
【RAG】向量数据库:milvus
后端·python·agent
copyer_xyf11 小时前
Agent 记忆管理
后端·python·agent
星云穿梭1 天前
用Python写一个带图形界面的学生管理系统——完整教程
python