Python PyQt5 GUI Download Image Files From URL with Progressbar Animation Using urllib Library Full Project For Beginners

 

Welcome folks today in this post we will be showing progressbar animation in pyqt5 when downloading files in python. All the full source code of the application is shown below

 

 

Get Started

 

 

In order to get started we need to install the following library using the pip command as shown below

 

pip install urllib

 

pip install pyqt5

 

After installing these libraries make an app.py file and copy paste the following code

 

app.py

 

 

# importing libraries 
import urllib.request 
from PyQt5.QtWidgets import *
import sys 

class GeeksforGeeks(QWidget): 

    def __init__(self): 
        super().__init__() 

        # calling a defined method to initialize UI 
        self.init_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 
        self.button.clicked.connect(self.Download) 

        # setting window geometry 
        self.setGeometry(310, 310, 280, 170) 

        # setting window action 
        self.setWindowTitle("GeeksforGeeks") 

        # showing all the widgets 
        self.show() 

    # 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 
            self.progressBar.setValue(download_percentage) 
            QApplication.processEvents() 

    # method to download any file using urllib 
    def Download(self): 

        # specify the url of the file which is to be downloaded 
        down_url = 'https://images.unsplash.com/photo-1613929905911-96040610a54d?ixid=MXwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHw%3D&ixlib=rb-1.2.1&auto=format&fit=crop&w=701&q=80' # specify download url here 

        # specify save location where the file is to be saved 
        save_loc = 'profile.jpg'

        # 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 
    sys.exit(App.exec())

 

See also  Python 3 Glob Library Script to Delete all Files and Folders Inside a Directory or Folder Full Project For Beginners

 

Now if you execute the above script by typing the below command

 

python app.py

 

So you will see it will download the image file with progressbar animation from the given path and store it as profile.jpg in my root directory as shown below

 

 

 

Leave a Reply