欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

PyQt5实现下载进度条效果

程序员文章站 2023-08-12 09:21:36
起因是因为公司要开发一款自动登录某网站的助手工具提供给客户使用,要使用到selenium,所以选择了pyqt5的方式来开发这个c/s架构的客户端 在过程中要用到自动更新的...

起因是因为公司要开发一款自动登录某网站的助手工具提供给客户使用,要使用到selenium,所以选择了pyqt5的方式来开发这个c/s架构的客户端

在过程中要用到自动更新的功能,所以自己写一个下载进度的插件给大家分享,本人编程水平有点菜,不要见怪。

界面文件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_()) 

实现文件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_()) 

比较通用的一个下载模块,初始化调用的时候只需要传入要下载的地址就行,下载操作采取异步,以防阻塞ui,确保程序目录下拥有update目录存在,默认我是将要更新的文件放在这个目录下面,还有优化的地方希望大家可以指出。

运行后效果:

PyQt5实现下载进度条效果

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。