Home > Software engineering >  pair tkinter progress bar with wget download
pair tkinter progress bar with wget download

Time:09-27

I have a download button when clicked it calls a download function that downloads a video from a media url using wget example of a video url http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4

from tkinter import *
window = Tk()
name = Entry(window, width=10)
name.grid(column=0, row=0, sticky=W)

url = Entry(window, width=10)
url.grid(column=0, row=1, sticky=W)
 
dl_button = Button(window, text='download', command=dl) #*********
dl_button.grid(column=2, row=0, sticky=W)
 
status_lable = Label(window, text='Ready')
status_lable.grid(column=0, row=1, sticky=W)

bar = Progressbar(window, length=200)
bar.grid(column=1, row=1, sticky=W)

window.mainloop()

the dl function opens up 2 threads one for the download and one for the gui, the gui has a progress bar, that i want to be updated alongside the download:

import threading
from tkinter.ttk import Progressbar
import wget
def dl():
    def wg():
        wget.download(
            url.get(), 'C:/Users/Desktop/downloads/' name.get() '.mp4')
            
    def update_progress():
        status_lable.config(text='Downloading...')
        # also update the progress bar, based on the progress of the download from wget
        
    dl_thread = threading.Thread(target=wg)
    progress_thread = threading.Thread(target=update_progress)
    dl_thread.start()
    progress_thread.start()

is this doable, is it doable with somthing other than wget, or is it simply just not doable?

thx.

CodePudding user response:

ok after along research alot of trial and error, i managed to make it, here's a demo code:

import threading
from tkinter.ttk import Progressbar
import wget
from tkinter import *


def dl():
    def update_progress_bar(block_num, block_size, total_size):
        bar.config(value=block_num, maximum=block_size)
        # when done downloading remove the progress bar
        if block_num == block_size:
            bar.destroy()
            print("Download Complete")

    def wg():
        wget.download('http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4',
                        'C:/Users/hanno/Desktop/egybest python/downloads/'   'koko' '.mp4', bar= update_progress_bar)

    dl_thread = threading.Thread(target=wg)
    dl_thread.start()


window = Tk()
dl_button = Button(window, text='download', command=dl)
dl_button.grid(column=2, row=0, sticky=W)

bar = Progressbar(window, length=200)
bar.grid(column=1, row=2)
window.mainloop()

its all about the bar parameter in wget, it takes a function and calles it every block of the download, and sends it the full size of the file, the number of blocks the full size is divided into, and the current block number, then you can update the progress bar for every block inside the called function as shown in the code, good luck all.

important: I've been told in the comments that calling tkinter methods from an outside thread may cause the the program to crash, I couldn't find any other solution for this problem online or on my own. thank you @TheLizzard and @Rory

  • Related