凌的博客

您现在的位置是:首页  > 学无止境 > python

python

PyQt5 多线程下载文件(优化)

2019-08-25python0

1.png

import sys

from PyQt5.QtWidgets import QWidget, QApplication, QLabel, QLineEdit, QPushButton, QProgressBar, QDesktopWidget, \
    QTextEdit,QMessageBox,QFileDialog
import time, requests, os
from PyQt5.QtCore import QThread, pyqtSignal,Qt
from PyQt5.QtGui import QIcon


class WorkThread(QThread):
    trigger = pyqtSignal(int)
    trigger2 = pyqtSignal(str)
    url = ""
    basedir = "./"

    def __init__(self):
        super(WorkThread, self).__init__()

    def run(self):
        print("thread run..")
        url = self.url
        print("down file:" + url)
        path = os.path.join(self.basedir,os.path.basename(url))
        start = time.time()
        size = 0
        response = requests.get(url, stream=True)  # stream 必须带上
        chunk_size = 1024  # 每次下载大小
        content_size = int(response.headers['content-length'])
        if response.status_code == 200:
            print("[文件大小]:%.2f MB" % (content_size / chunk_size / 1024))
            with open(path, "wb") as file:
                for data in response.iter_content(chunk_size=chunk_size):
                    file.write(data)
                    size += len(data)  # 已下载大小

                    num = int(size / content_size * 100)
                    self.trigger.emit(num)
                    # \r 指定第一个字符开始,搭配end属性完成覆盖进度条
                    print("\r" + "[下载进度]:%s%.2f%%" % (
                        ">" * int(size * 50 / content_size), float(size / content_size * 100)), end="")

            end = time.time()  # 结束时间
            self.trigger2.emit("下载完成!用时%.2f秒" % (end - start))
            print("\n" + "全部下载完成!用时%.2f秒" % (end - start))



class DownLoad(QWidget):
    def __init__(self):
        super().__init__()
        self.initUI()

    def initUI(self):

        #self.setWindowIcon(QIcon("web.png"))

        lbl = QLabel("请输入链接:", self)
        lbl.move(30, 30)
        lbl_xzd = QLabel("下载到:", self)
        lbl_xzd.move(30, 55)

        self.text_xz = QLineEdit("", self)
        self.text_xz.resize(350, 20)
        self.text_xz.move(100, 55)
        self.text_xz.setText(r"C:/Users/Administrator/Desktop")
        self.text_xz.setFocusPolicy(Qt.NoFocus)

        selbtn = QPushButton("选择下载目录", self)
        selbtn.move(460, 55)
        selbtn.clicked.connect(self.showDir)

        self.text = QLineEdit("", self)
        self.text.resize(350, 20)
        self.text.move(100, 28)
        self.text.textChanged.connect(self.changeText)

        self.textedit = QTextEdit("", self)
        self.textedit.resize(510, 200)
        self.textedit.move(100, 140)

        btn = QPushButton("下载", self)
        btn.move(460, 28)
        btn.clicked.connect(self.down)

        self.cancelbtn = QPushButton("重置", self)
        self.cancelbtn.move(540, 28)
        self.cancelbtn.clicked.connect(self.cancel)
        self.cancelbtn.setDisabled(True)

        self.pbar = QProgressBar(self)
        self.pbar.setGeometry(100, 80, 300, 25)

        self.lbl2 = QLabel("", self)
        self.lbl2.move(100, 110)

        self.setGeometry(300, 300, 650, 400)
        self.center()
        self.setWindowTitle("下载文件")

        # 创建线程
        self.workthread = WorkThread()
        self.workthread.trigger.connect(self.progressbar)
        self.workthread.trigger2.connect(self.downresult)

    def showDir(self):
        download_path = QFileDialog.getExistingDirectory(self,"浏览",r"C:/Users/Administrator/Desktop")
        self.text_xz.setText(download_path)

    def cancel(self):
        self.text.setText("")
        self.lbl2.setText("")
        self.pbar.setValue(0)

    def down(self):
        self.cancelbtn.setDisabled(True)
        self.workthread.url = self.text.text()
        if self.workthread.url == "":
            QMessageBox.information(self,"错误信息","URL不能为空")
            return
        self.workthread.start()

    def downresult(self,info):
        self.lbl2.setText(info)
        self.lbl2.adjustSize()

    def progressbar(self, num):
        self.pbar.setValue(num)
        if num == 100:
            info = time.strftime("%y/%M/%d %H:%M:%S", time.localtime()) + ":" + self.text.text() + "\n" + self.textedit.toPlainText()
            self.textedit.setText(info)
            self.cancelbtn.setDisabled(False)

    def changeText(self, text):
        if text == "":
            self.lbl2.setText("")
            self.pbar.setValue(0)




    def center(self):
        qr = self.frameGeometry()
        cp = QDesktopWidget().availableGeometry().center()
        qr.moveCenter(cp)
        self.move(qr.topLeft())


if __name__ == "__main__":
    app = QApplication(sys.argv)
    down = DownLoad()
    down.show()
    sys.exit(app.exec_())


如果本文对您有所帮助,请支持下本站哦!!!^_^

文章评论

0条评论