PyQt5 implements download progress bar effect

不言
Release: 2018-04-19 11:05:57
Original
3045 people have browsed it

This article mainly introduces the download progress bar effect of PyQt5 in detail. It has certain reference value. Interested friends can refer to it.

The reason is because the company wants to develop an automatic login The assistant tool of a certain website is provided to customers and requires the use of selenium, so I chose the pyqt5 method to develop this C/S architecture client

In the process, I need to use the automatic update function, so I Write a download progress plug-in to share with everyone. My programming skills are a bit different, so don’t be offended.

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(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_())
Copy after login


Implementation 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_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): 
  """ 
  下载类实现 
  """ 
  def __init__(self, download_url, auto_close=True, parent=None): 
    """ 
    Constructor 
     
    @download_url:下载地址 
    @auto_close:下载完成后时候是否需要自动关闭 
    """ 
    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_())
Copy after login


A relatively common download module. When initializing the call, you only need to pass in the address to be downloaded. The download operation is asynchronous to prevent blocking the UI and ensure the program directory. There is an update directory under it. By default, I put the files to be updated under this directory. I hope you can point out the optimization areas.

The effect after running:

Related recommendations:

PyQt5 must learn the progress bar effect every day

PyQt5 Must Learn Every Day QSplitter Implements Window Separation

PyQt5 Must Learn Every Day Tool Tip Function


The above is the detailed content of PyQt5 implements download progress bar effect. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!