您好,登錄后才能下訂單哦!
小編給大家分享一下PyQt5如何實(shí)現(xiàn)下載進(jìn)度條效果,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!
界面文件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(300, 56) 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) Dialog.setSizeGripEnabled(True) self.gridLayout = QtWidgets.QGridLayout(Dialog) self.gridLayout.setSizeConstraint(QtWidgets.QLayout.SetDefaultConstraint) self.gridLayout.setObjectName("gridLayout") self.progressBar = QtWidgets.QProgressBar(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.label, 0, 0, 1, 1) self.retranslateUi(Dialog) QtCore.QMetaObject.connectSlotsByName(Dialog) def retranslateUi(self, Dialog): _translate = QtCore.QCoreApplication.translate Dialog.setWindowTitle(_translate("Dialog", "Dialog")) self.label.setText(_translate("Dialog", "客戶端更新下載中...")) if __name__ == "__main__": import sys app = QtWidgets.QApplication(sys.argv) Dialog = QtWidgets.QDialog() ui = Ui_download() ui.setupUi(Dialog) Dialog.show() sys.exit(app.exec_())
實(shí)現(xiàn)文件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_download import Ui_download import os import sys import requests class downloadThread(QThread): download_proess_signal = pyqtSignal(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 def run(self): try: f = requests.get(self.download_url, stream=True) offset = 0 for chunk in f.iter_content(chunk_size=self.buffer): if not chunk: break self.fileobj.seek(offset) self.fileobj.write(chunk) offset = offset + len(chunk) proess = offset / int(self.filesize) * 100 self.download_proess_signal.emit(int(proess)) self.fileobj.close() self.exit(0) except Exception as e: print(e) class download(QDialog, Ui_download): """ 下載類實(shí)現(xiàn) """ def __init__(self, download_url, auto_close=True, parent=None): """ Constructor @download_url:下載地址 @auto_close:下載完成后時(shí)候是否需要自動(dòng)關(guān)閉 """ super(download, self).__init__(parent) self.setupUi(self) self.progressBar.setValue(0) self.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=True).headers['Content-Length'] path = os.path.join("update", os.path.basename(self.download_url)) self.fileobj = open(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 __name__ == '__main__': app = QtWidgets.QApplication(sys.argv) ui = download() ui.show() sys.exit(app.exec_())
比較通用的一個(gè)下載模塊,初始化調(diào)用的時(shí)候只需要傳入要下載的地址就行,下載操作采取異步,以防阻塞UI,確保程序目錄下?lián)碛衭pdate目錄存在,默認(rèn)我是將要更新的文件放在這個(gè)目錄下面,還有優(yōu)化的地方希望大家可以指出。
運(yùn)行后效果:
以上是“PyQt5如何實(shí)現(xiàn)下載進(jìn)度條效果”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。