custom tkinter progress bar not working for "indeterminate" mode

130 views Asked by At

I'm trying to create a YouTube video downloader GUI using yt-dlp and custom-tkinter library.

I successfully implemented progressbar using CTkProgressBar() inside a CtkFrame for "determinate" mode which will show progress of the filesize downloaded.

For videos that doesn't have the filesize, I tried to show "interminate" progressbar but it seems to be not working as expected.

app = ctk.CTk()
app.title("App")
app.geometry("300x200")

class ProgressFrame(ctk.CTkFrame):
    def __init__(self, parent: tk.Tk, *args, **kwargs):
        super().__init__(parent, *args, **kwargs)
        self.progressbar = ctk.CTkProgressBar(
                        app, 
                        mode="indeterminate", 
                        indeterminate_speed=5
                    )
        self.progressbar.pack(side="top", expand=False, padx=20, pady=5)

    def update_indeterminate_progressbar(self):
        self.progressbar.step()
        self.progressbar.update()    # Now the progressbar is working properly.

progress_frame = ProgressFrame(app)
# progressbar.set(0)            # for "determinate" progressbar

ytdlp_options = {
            'progress_hooks': [progress_frame.update_indeterminate_progressbar],
            'outtmpl': "path/to/output/file",
            'format': "format id",
        }
video_info = {
    # youtbe video details.
}

with yt_dlp.YoutubeDL(ytdlp_options) as ytdl:
    ytdl.process_ie_result(video_info, download=True)

# progressbar.set(1)            # for "determinate" progressbar

app.mainloop()

When I run the above code, The progress bar stays still and there is no any change in progressbar. If I uncomment progressbar.start() the progress bar starts moving after the video is downloaded and not during the download.

When I tried to remove the progress_hook, update_indeterminate_progress and run, I get the same above result.

EDIT:

The problems seems to be with my progress_frame. its working now after I updated the progressbar inside the update_indeterminate_progress method.

TIA

1

There are 1 answers

1
Bryan Oakley On

It looks like the customtkinter progress bar does its own animation in the same thread that it is created. If you're running some long running code in the same thread, it won't be able to update its appearance.

You'll likely need to download the video in a separate thread or process.,