如何修改tqdm.tk GUI进度条的Tkinter图标并禁用实验警告?
解决方案
1. 修改Tkinter窗口图标
tqdm.tk的进度条窗口基于Tkinter实现,你可以通过获取窗口实例来设置自定义图标:
- Windows系统优先使用
.ico格式图标 - 若需支持PNG等格式,可借助
PIL库扩展支持
直接使用ICO图标(无需额外依赖)
import requests import os import warnings from tqdm.tk import tqdm from tqdm import TqdmExperimentalWarning def download_update(url, destination): # 过滤实验警告 warnings.filterwarnings("ignore", category=TqdmExperimentalWarning) response = requests.get(url, stream=True) total_size = int(response.headers.get('content-length', 0)) print('Update in Progress (Make sure the connection is stable)\n') with open(destination, 'wb') as file, tqdm( desc='Update in Progress', total=total_size, unit='B', unit_scale=True, unit_divisor=1024, colour= '#f8b4aa', ) as bar: # 获取Tk根窗口并设置图标 root_window = bar.container.master # 替换为你的图标文件路径 root_window.iconbitmap("path/to/your/icon.ico") for data in response.iter_content(chunk_size=1024): size = file.write(data) bar.update(size)
使用PNG图标(需安装PIL)
先执行pip install pillow安装依赖,再修改图标设置部分:
from PIL import Image, ImageTk # ... 保留其他代码 ... root_window = bar.container.master icon_img = Image.open("path/to/your/icon.png") tk_icon = ImageTk.PhotoImage(icon_img) root_window.iconphoto(True, tk_icon)
2. 消除「TqdmExperimentalWarning」警告
有两种简洁的解决方式:
- 方式一:精准过滤警告类(推荐)
如上述代码所示,导入TqdmExperimentalWarning后,用warnings.filterwarnings忽略该类警告。 - 方式二:通过环境变量全局禁用
在代码最开头添加:import os os.environ["TQDM_DISABLE_WARNINGS"] = "1"
内容的提问来源于stack exchange,提问作者Abdullah Zaman Babar
相关产品推荐
相关产品推荐

