V
V
Vasiliy_Ivanov2021-12-09 21:32:45
Python
Vasiliy_Ivanov, 2021-12-09 21:32:45

How to correctly use the progress bar in the code?

Good day, I am making a program that downloads videos from YouTube, but when using the progress bar, it does not update at the time of downloading the video, but only shows the full download at the end. I use pytube and tkinter libraries.
Here is the whole code:

import pytube
from threading import Thread
import tkinter as tk
from tkinter import ttk

win = tk.Tk()
win.geometry('250x50')

file_size = 0


def progress(stream=None, chunk=None, remaining=0):
    file_downloaded = (file_size - remaining)
    per = (file_downloaded / file_size) * 100
    pb.config(value=format(per))


def download():
    global file_size
    video_url = 'ссылка на видео'
    save_path = 'C:\\Users\\parfe\\Desktop'

    video = pytube.YouTube(video_url, on_progress_callback=progress).streams.filter(res='720p', file_extension='mp4', progressive=True).desc().first()
    file_size = video.filesize
    video.download(save_path)


def start():
    thread = Thread(target=download)
    thread.start()


pb = ttk.Progressbar(win, length=200)
pb.pack()

tk.Button(win, text='Start', command=start).pack()

win.mainloop()

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vindicar, 2021-12-09
@Vindicar

You need to give tkinter a chance to redraw and process other window messages.

pb.config(value=format(per))
win.update_idletasks()

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question