download from internet using pyqt5

  البايثون في لتنزيل ملف من الانترنت

import urllib.request    #import liberary of download

urllib.request.urlretrieve(رابط الملفمسار الحفظ)  #code of download

Example:

from PyQt5.QtWidgets import *
from PyQt5.QtCore import Qt
import sys
import urllib.request

class Firstapp(QWidget):
    def __init__(self):
        QWidget.__init__(self)
        # super(Firstapp, self).__init__()
        self.ui()

    def ui(self):
        layout = QFormLayout()

        self.li_1 = QLineEdit()
        self.li_2 = QLineEdit()
        self.btn = QPushButton("Download")
        self.prog = QProgressBar()

        self.li_1.setPlaceholderText("url")
        self.li_2.setPlaceholderText("save location")
        self.prog.setValue(0)
        self.prog.setAlignment(Qt.AlignCenter)

        layout.addWidget(self.li_1)
        layout.addWidget(self.li_2)
        layout.addWidget(self.prog)
        layout.addWidget(self.btn)
        layout.setVerticalSpacing(30)
        self.setLayout(layout)
        self.setWindowTitle("Download app")
        self.setFixedSize(500,300)
        self.setFocus( )

        self.btn.clicked.connect(self.download)

    def download(self):
        l_url =  self.li_1.text()
        l_savelocation = self.li_2.text()

# enter the file path followed by its name.extention
        urllib.request.urlretrieve(l_url, l_savelocation, self.report)

QMessageBox.information(self"Information""download completed")

 

    # this function to calculate download percentage and hint for finishing

def report(self, bn, bs, ts):
         res = bn * bs
         if ts > :
             percent = (res * 100 ) / ts
             self.prog.setValue(int(percent))
             if int(percent)==100:
                 self.btn.setText("download completed")

if __name__ == "__main__":
        app = QApplication(sys.argv)
        widget = Firstapp()
        widget.show()
        app.exec()

Comments

Popular posts from this blog

QFormLayoutشرح ال

QGridLayoutشرح ال