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()
相关推荐
沪漂阿龙15 小时前
Embedding:文本怎么变成向量?语义检索为什么能工作?
人工智能·python·embedding
生信碱移15 小时前
Vscode 连接 ipynb 选择内核无法自动显示 conda 环境对应的 python
服务器·人工智能·经验分享·vscode·python
Cloud_Shy61815 小时前
解读《Effective Python 3rd Edition》:从练气到老魔(第七章 Item 48 - 50)
开发语言·人工智能·笔记·python·microsoft·学习方法
喵叔哟15 小时前
Week 3 --Day 4:生产级部署
python·langchain
秋雨梧桐叶落莳15 小时前
iOS——QQ音乐仿写项目总结
学习·macos·ui·ios·mvc·objective-c·xcode
huzhongqiang15 小时前
Python实现单例装饰器:支持持久序列化
python
winfredzhang15 小时前
用 wxPython + 通义千问 VL 打造一款“批量人物图像识别“桌面应用
python·sqlite·wxpython·qwen 3.7max·分析照片
codeaideaai15 小时前
使用UV创建python项目
python·fastapi·uv
yongche_shi15 小时前
ragas官方文档中文版(十七)
python·ai·ragas·事实正确性
闵孚龙15 小时前
Tensor:PyTorch 世界里的一切都是张量
人工智能·pytorch·python