Important: Please read the Qt Code of Conduct -

Progressbar in PYQT5 with Python

  • Dear All,

    I have created a desktop application by PYQT5 and python 3.7, to download  a video by clicking the download button and save it locally in the PC. 
     The code will fetch the video link from **(lineEdit.text())** which is labeled "URL" and save it in the local directory in **(lineEdit_2.text)** which is labeled "SAVE AS". If the download stops for any reason, it will be resumed again by press the start  download button.
    The  question is, I want to connect the start button with a Progressbar and displaying the loading in percentage, from start downloading 1% to finish 100%. Please see the code below and the picture.

    The code

     def curl_progress(self,total, existing,upload_t,upload_d):
                frac = float(existing)/float(total)
            except ZeroDivisionError:
                frac =0
            print("Downloaded %d/%d (%0.2f%%)" % (existing, total, frac))
            if frac ==1.0:
                self.textBrowser.append(("Downloaded %d/%d (%0.2f%%)" % (existing, total, frac)))
        def curl_limit_rate(self,rate_limit):
            url= self.lineEdit.text()
            save_location = self.lineEdit_2.text()
            c = pycurl.Curl()
            #For SSL certification
            c.setopt(pycurl.CAINFO, certifi.where())
            c.setopt(c.URL, url)
            c.setopt(c.MAX_RECV_SPEED_LARGE, rate_limit)
            if os.path.exists(save_location):
                file_id = open(save_location, "ab")
                c.setopt(c.RESUME_FROM, os.path.getsize(save_location))
                file_id = open(save_location, "wb")
            c.setopt(c.WRITEDATA, file_id)
            c.setopt(c.NOPROGRESS, 0)
            c.setopt(c.PROGRESSFUNCTION, self.curl_progress)

    The picture


    Many thanks in advance,

  • @monamour
    So in outline:

    self.pb = QProgressBar(self)
    self.pb.setRange(0, 100)
    frac = float(existing)/float(total)
    self.pb.setValue(frac * 100)

    That's the principle. I cannot recall whether QProgressBar is one which updates itself without needing to go into an event loop, if not then you'll need a QCoreApplication.processEvents() somewhere, but I suspect your "video download curl progress update" already has that so you won't need your own?

  • @JonB Thanks so much, it solved.

Log in to reply