I have this python function that downloads a file and shows the progress bar using tqdm.tk. Now I'm getting the default tkinter icon and I want to change it. How would I do it?
import requests
import os
from tqdm.tk import tqdm
def download_update(url, destination):
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, #1024
colour= '#f8b4aa',
) as bar:
for data in response.iter_content(chunk_size=1024): #1024
size = file.write(data)
bar.update(size)
And I get a default warning message in the console which says TqdmExperimentalWarning: GUI is experimental/alpha when I use tqdm.tk. How can I disable it?
Source code for tqdm.tk shows that it keeps main window tkinter.Tk()
as self._tk_window
so you can try to use
bar._tk_window.iconbitmap("myIcon.ico")
or
from PIL import Image, ImageTk
image = Image.open('image.jpg') # or `.png`
photo = ImageTk.PhotoImage(image)
bar._tk_window.wm_iconphoto(False, photo)
EDIT:
It seems you may also first create own window, next set icon, and next send this window to tqdm.tk
but it creates tqdm
as second window (tk.Toplevel()
) and icon will be assigned to main window. So it still needs to use bar._tk_window
import tkinter as tk
window = tk.Tk()
window.iconbitmap("main_windown.ico") # or `window.wm_iconphoto(...)
with ...., tqdm.tk(..., tk_parent=window) as bar:
bar._tk_window.iconbitmap("progressbar.ico") # or `window.wm_iconphoto(...)
# .... rest ...
bar.update(...)
Full working example. It needs only some image.png
.
import time
from tqdm.tk import tqdm
from PIL import Image, ImageTk
size = 10
with tqdm(total=size) as bar:
#bar._tk_window.iconbitmap('icon.ico')
image = Image.open('image.png') # or `.jpg`
photo = ImageTk.PhotoImage(image)
bar._tk_window.wm_iconphoto(False, photo)
for _ in range(size):
time.sleep(0.3)
bar.update(1)