PyQt5 – How to automate Progress Bar while downloading using urllib?

PyQt5 is one of the emerging GUI libraries in terms of developing Python GUI Desktop apps. It has rich and robust functionality which ensures production quality apps. Learning PyQt5 library is an add-on to your knowledge. You can develop your consumer quality, highly professional apps.

In this article, we will learn how to automate the Progress Bar in PyQt5. By automating what we mean is to dynamically change and set the value of progress bar. Suppose, you are downloading any file over the internet and want to show the progress of the download, then this article will surely help you.

In the present example, we are using the Urllib library to download the files as its the most common library to download files using python.



self.progressBar = QProgressBar(self)

QProgressBar class is for creating the progress bar object.

Firstly, go through the following code, then we will explain what the whole thing does.

Code : 






# importing libraries
import urllib.request
from PyQt5.QtWidgets import *
import sys
class GeeksforGeeks(QWidget):
    def __init__(self):
        # calling a defined method to initialize UI
    # method for creating UI widgets
    def init_UI(self):
        # creating progress bar
        self.progressBar = QProgressBar(self)
        # setting its size
        self.progressBar.setGeometry(25, 45, 210, 30)
        # creating push button to start download
        self.button = QPushButton('Start', self)
        # assigning position to button
        self.button.move(50, 100)
        # assigning activity to push button
        # setting window geometry
        self.setGeometry(310, 310, 280, 170)
        # setting window action
        # showing all the widgets
    # when push button is pressed, this method is called
    def Handle_Progress(self, blocknum, blocksize, totalsize):
        ## calculate the progress
        readed_data = blocknum * blocksize
        if totalsize > 0:
            download_percentage = readed_data * 100 / totalsize
    # method to download any file using urllib
    def Download(self):
        # specify the url of the file which is to be downloaded
        down_url = '' # specify download url here
        # specify save location where the file is to be saved
        save_loc = 'C:\Desktop\GeeksforGeeks.png'
        # Dowloading using urllib
        urllib.request.urlretrieve(down_url,save_loc, self.Handle_Progress)
# main method to call our app
if __name__ == '__main__':
    # create app
    App = QApplication(sys.argv)
    # create the instance of our window
    window = GeeksforGeeks()
    # start the app


Explanation : 

Below is the syntax for urllib, we have to study all the parameters it takes. 

Synatx: urllib.request.urlretrieve(url, filename, reporthook)

Parameters: This method will take following parameters :
The first parameter is the url of the file, which is to be downloaded.

The second parameter, if present, specifies the file location to save the file (if this argument is not passed, the location will be a temp file with an auto-generated name).

The third parameter is a callable that will be called when the file is being downloaded and once after another, each block would be read. The callable (which is a function Handle_Progress in this case) will be passed as three arguments :

  • a count of blocks transferred so far (blocknum)
  • block size in bytes (blocksize)
  • the total size of the file (totalsize)

The function Handle_Progress hence receives three arguments. The current downloaded size of the file is calculated dynamically by multiplying blocknum and blocksize and is stored in the variable readed_data.

The rest of the work is done by the formula for calculating the percentage. We multiply readed_data by 100 and divide it by the total size of the file. It gives us the current download percentage. Then we set this download percentage to the progress bar using setValue() method of progressBar object.


Output : 


Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using or mail your article to See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.