通过回调在 QMainWindow 中显示进度条,boto3.s3

Showing a Progress Bar in a QMainWindow from a Callback, boto3.s3

我正在创建一个将文件夹上传到存储桶中的程序。现在我有了程序并且 UI 都设置好了 我只想添加一个进度条来显示正在上传的文件。

我想知道是否有办法使用 s3.upload_file(fileName, bucketName, objectName, Callback=ProgressPercentage(path.text())) 在我的 QMainWindow 上获得进度条。或者,如果我需要以不同的方式解决这个问题。

class ProgressPercentage(object):
    def __init__(self, filename):
        self._filename = filename
        self._size = float(os.path.getsize(filename))
        self._seen_so_far = 0
        self._lock = threading.Lock()

    def __call__(self, bytes_amount):
        # To simplify, assume this is hooked up to a single filename
        with self._lock:
            self._seen_so_far += bytes_amount
            percentage = (self._seen_so_far / self._size) * 100
            sys.stdout.write(
                "\r%s  %s / %s  (%.2f%%)" % (
                    self._filename, self._seen_so_far, self._size,
                    percentage))
            sys.stdout.flush()

这种情况下的逻辑是创建一个QObject,它有一个指示进度的信号,此外上传任务必须在辅助线程中执行,这样GUI就不会冻结:

import math
import os
import sys
import threading

import boto3

from PyQt5 import QtCore, QtWidgets


class S3Worker(QtCore.QObject):
    started = QtCore.pyqtSignal()
    finished = QtCore.pyqtSignal()
    percentageChanged = QtCore.pyqtSignal(int)

    def __init__(self, parent=None):
        super().__init__(parent)

        self._s3 = boto3.client("s3")

    @property
    def s3(self):
        return self._s3

    def upload(self, filename, bucketname, objectname):
        self._size = float(os.path.getsize(filename))
        self._seen_so_far = 0
        threading.Thread(
            target=self._execute, args=(filename, bucketname, objectname), daemon=True
        ).start()

    def _execute(self, fileName, bucketName, objectName):
        self.started.emit()
        self.s3.upload_file(fileName, bucketName, objectName, Callback=self._callback)
        self.finished.emit()

    def _callback(self, bytes_amount):
        self._seen_so_far += bytes_amount
        percentage = (self._seen_so_far / self._size) * 100
        self.percentageChanged.emit(math.floor(percentage))


class Widget(QtWidgets.QWidget):
    def __init__(self, parent=None):
        super().__init__(parent)

        self.filename_le = QtWidgets.QLineEdit()
        self.upload_btn = QtWidgets.QPushButton("Upload")
        self.percentage_pb = QtWidgets.QProgressBar()

        lay = QtWidgets.QGridLayout(self)
        lay.addWidget(QtWidgets.QLabel("filename:"))
        lay.addWidget(self.filename_le, 0, 1)
        lay.addWidget(self.upload_btn, 0, 2)
        lay.addWidget(self.percentage_pb, 1, 0, 1, 3)

        self.qs3 = S3Worker()

        self.upload_btn.clicked.connect(self.start_upload)
        self.qs3.started.connect(lambda: self.upload_btn.setEnabled(False))
        self.qs3.finished.connect(lambda: self.upload_btn.setEnabled(True))
        self.qs3.percentageChanged.connect(self.percentage_pb.setValue)

    def start_upload(self):
        filename = self.filename_le.text()
        if os.path.exists(filename):
            self.qs3.upload(filename, "mybucket", "foobject")


def main():
    app = QtWidgets.QApplication(sys.argv)

    w = Widget()
    w.show()
    sys.exit(app.exec_())

if __name__ == "__main__":
    main()