PYQT5 Implement download progress bar effect

Source: Internet
Author: User
This article is mainly for everyone to introduce the PYQT5 implementation download progress bar effect, with a certain reference value, interested in small partners can refer to

The reason is because the company to develop an automatic login to a website of the Assistant tool for customers to use, to use to selenium, so chose the pyqt5 way to develop this C/s architecture of the client

In the process to use the Automatic Update function, so you write a download progress of the plug-in to everyone to share, I programming level a bit of food, do not take offense.

Interface file ui_download.py


#-*-Coding:utf-8-*-from PyQt5 import Qtcore, Qtgui, qtwidgets from pyqt5.qt Import Qt class Ui_download (object): def setupui (self, Dialog): Dialog.setwindowflags (Qt.framelesswindowhint) dialog.setobjectname ("Dialog") Dialog . Resize (+) dialog.setfixedsize (Dialog.width (), Dialog.height ()) Sizepolicy = Qtwidgets.qsizepolicy (QtWidgets . Qsizepolicy.fixed, QtWidgets.QSizePolicy.Fixed) sizepolicy.sethorizontalstretch (0) Sizepolicy.setverticalstretch ( 0) Sizepolicy.setheightforwidth (Dialog.sizepolicy (). Hasheightforwidth ()) Dialog.setsizepolicy (SizePolicy) Dial Og.setsizegripenabled (True) self.gridlayout = Qtwidgets.qgridlayout (Dialog) self.gridLayout.setSizeConstraint (Qtwi Dgets. Qlayout.setdefaultconstraint) self.gridLayout.setObjectName ("GridLayout") Self.progressbar = Qtwidgets.qprogressba R (Dialog) Self.progressBar.setProperty ("value", 0) self.progressBar.setAlignment (qtcore.qt.alignleading| Qtcore.qt.alignleft| Qtcore.qT.alignvcenter) self.progressBar.setObjectName ("ProgressBar") Self.gridLayout.addWidget (Self.progressbar, 1, 0, 1, 1) Self.label = Qtwidgets.qlabel (Dialog) self.label.setObjectName ("label") Self.gridLayout.addWidget (Self.lab El, 0, 0, 1, 1) self.retranslateui (Dialog) QtCore.QMetaObject.connectSlotsByName (Dialog) def retranslateui (sel     F, Dialog): _translate = QtCore.QCoreApplication.translate dialog.setwindowtitle (_translate ("Dialog", "Dialog")) Self.label.setText (_translate ("Dialog", "Client update download ...")) if __name__ = = "__main__": Import sys app = QTWIDGETS.QAP Plication (sys.argv) Dialog = Qtwidgets.qdialog () UI = Ui_download () ui.setupui (Dialog) dialog.show () Sys.exit (AP P.EXEC_ ())


Implementing File download.py


#-*-Coding:utf-8-*-"" "Module implementing Dialog." "" From Pyqt5.qtcore import Qthread, pyqtsignal from pyqt5.qtwidgets import qdialog from PyQt5 import qtwidgets from ui_downl Oad Import ui_download import OS import sys import requests class Downloadthread (qthread): Download_proess_signal = P     yqtsignal (int) def __init__ (self, download_url, filesize, fileobj, buffer): Super (Downloadthread, self). __init__ () Self.download_url = Download_url Self.filesize = filesize Self.fileobj = fileobj Self.buffer = Buffer D EF run: try:f = Requests.get (Self.download_url, stream=true) offset = 0 for chunk in F.iter_c Ontent (Chunk_size=self.buffer): If not chunk:break self.fileobj.seek (offset) Self.fileo Bj.write (chunk) offset = offset + len (chunk) proess = Offset/int (self.filesize) * SELF.DOWNL   Oad_proess_signal.emit (int (proess)) Self.fileobj.close () self.exit (0)  Except Exception as E:print (e) class download (Qdialog, ui_download): "" "Download Class Implementation" "" Def __init__ (self, D Ownload_url, Auto_close=true, Parent=none): "" "Constructor @download_url: Download address @auto_close: When the download is complete, you need to To turn off the "" "Super (Download, self) automatically. __init__ (parent), self.setupui (self) self.progressBar.setValue (0). Downloadthread = None Self.download_url = Download_url Self.filesize = None Self.fileobj = None self.auto_ Close = Auto_Close self.download () def download (self): Self.filesize = Requests.get (Self.download_url, stream=t Rue). headers[' content-length '] path = Os.path.join ("Update", Os.path.basename (self.download_url)) Self.fileobj = O     Pen (path, ' WB ') Self.downloadthread = Downloadthread (Self.download_url, Self.filesize, Self.fileobj, buffer=10240)    Self.downloadThread.download_proess_signal.connect (Self.change_progressbar_value) Self.downloadThread.start () def Change_progressbar_value(self, value): Self.progressBar.setValue (value) if self.auto_close and value = = 100:self.close () if __nam e__ = = ' __main__ ': app = Qtwidgets.qapplication (sys.argv) UI = Download () ui.show () Sys.exit (App.exec_ ())


Comparison of a common download module, the initialization of the call only need to pass in the address to download the line, the download operation to take the asynchronous, to prevent blocking the UI, to ensure that the program directory has the update directory exists, by default I will update the file under this directory, and the optimization of the place I hope you can point out.

Post-run Effects:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.