Ejemplo n.º 1
0
class MyClass(QWidget):
    def __init__(self):
        super().__init__()
        self.initUI()

    def initUI(self):
        self.resize(300, 200)
        # 载入进度条控件
        self.verticalLayout = QVBoxLayout()
        self.pgb = QProgressBar(self)
        self.verticalLayout.addWidget(self.pgb, 0, 0, 1, 1)
        #self.pgb.move(50, 50)
        #self.pgb.resize(250, 20)

        # 配置一个值表示进度条的当前进度
        self.pv = 0

        # 申明一个时钟控件
        self.timer1 = QBasicTimer()

        # 设置进度条的范围
        self.pgb.setMinimum(0)
        self.pgb.setMaximum(100)
        self.pgb.setValue(self.pv)
        # 载入按钮
        self.btn = QPushButton("开始", self)
        self.btn.move(50, 100)
        self.btn.clicked.connect(self.myTimerState)
        self.show()

    def myTimerState(self):
        if self.pv == 100:
            self.close()
        if self.timer1.isActive():
            self.timer1.stop()
            self.btn.setText("开始")
        else:
            self.timer1.start(100, self)
            self.btn.setText("停止")

    def timerEvent(self, e):
        if self.pv == 100:
            self.timer1.stop()
            self.btn.setText("完成")
        else:
            self.pv += 1
            self.pgb.setValue(self.pv)
Ejemplo n.º 2
0
class MyWidget(QWidget):
    def __init__(self):
        super().__init__()
        # 设置属性
        self.resize(400, 300)
        self.setWindowTitle("测试Qt进度条和aria2的联合")
        # 注册组件
        self.labelUrl = QLabel("下载链接")
        self.lineEditorUrl = QLineEdit(
            "https://download.cnki.net/CAJViewer-x86_64-buildubuntu1604-210401.AppImage"
        )
        self.labelProgress = QLabel("下载进度")
        self.downloadProgress = QProgressBar()
        self.textResult = QTextBrowser()
        self.buttonStart = QPushButton("开始")
        self.buttonPause = QPushButton("暂停")
        self.buttonUnpause = QPushButton("继续")
        self.buttonRemove = QPushButton("移除")
        self.buttonDebug = QPushButton("Debug")
        # 设置组件
        self.lineEditorUrl.setClearButtonEnabled(True)
        self.buttonStart.setIcon(self.style().standardIcon(
            QStyle.SP_MediaPlay))
        self.buttonPause.setIcon(self.style().standardIcon(
            QStyle.SP_MediaPause))
        self.buttonUnpause.setIcon(self.style().standardIcon(
            QStyle.SP_MediaPlay))
        self.buttonRemove.setIcon(self.style().standardIcon(
            QStyle.SP_MediaStop))
        # self.buttonPause.setDisabled(True)
        # self.buttonStop.setDisabled(True)
        # 添加组件
        self.layout = QGridLayout(self)
        self.layout.addWidget(self.labelUrl, 0, 0, 1, 1)
        self.layout.addWidget(self.lineEditorUrl, 0, 1, 1, 5)
        self.layout.addWidget(self.labelProgress, 1, 0, 1, 1)
        self.layout.addWidget(self.downloadProgress, 1, 1, 1, 5)
        self.layout.addWidget(self.textResult, 2, 0, 2, 5)
        self.layout.addWidget(self.buttonStart, 4, 0, 1, 1)
        self.layout.addWidget(self.buttonPause, 4, 1, 1, 1)
        self.layout.addWidget(self.buttonUnpause, 4, 2, 1, 1)
        self.layout.addWidget(self.buttonRemove, 4, 3, 1, 1)
        self.layout.addWidget(self.buttonDebug, 4, 4, 1, 1)
        # 添加组件功能

        # class函数

    @Slot(str)
    def updateProgress(self, value):
        self.downloadProgress.setMaximum(int(value['totalLength']))
        self.downloadProgress.setValue(
            int(value['files'][0]['completedLength']))
        if value["status"] == "complete":
            self.downloadProgress.setValue(int(value['totalLength']))

    shutdown_aria2 = Signal()

    @Slot()
    def clearProgress(self):
        self.downloadProgress.setValue(0)

    def closeEvent(self, event):
        if QMessageBox.question(self, "退出程序", "确定退出吗?") == QMessageBox.No:
            event.ignore()
        else:
            event.accept()
            self.shutdown_aria2.emit()
Ejemplo n.º 3
0
    def paintEvent(self, event: PySide6.QtGui.QPaintEvent) -> None:
        painter = QPainter(self)
        pixmap = self.movie.currentPixmap()
        pixmap = pixmap.scaled(self.my_size)
        painter.drawPixmap(0, 0, pixmap)


if __name__ == '__main__':
    app = QApplication()
    progressbar_value = 30
    path_to_gif = 'loading_gifs/1.gif'

    splash = MovieSplashScreen(path_to_gif)
    progressbar = QProgressBar(splash)
    progressbar.setMaximum(progressbar_value)
    progressbar.setTextVisible(False)
    progressbar.setGeometry(0,
                            splash.my_size.height() - 50,
                            splash.my_size.width(), 20)

    splash.show()

    for i in range(progressbar_value):
        progressbar.setValue(i)
        t = time.time()
        while time.time() < t + 0.1:
            app.processEvents()

    time.sleep(1)
class ProgressBar(QWidget):
    # Incoming progress updates
    set_progress = Signal(int)

    def __init__(self, show_time=True):
        """Opens a progress bar widget
        :type show_time: bool
        :param show_time: If the time should be shown below the progress bar"""
        super().__init__()
        # Setup parameters
        self._show_time: bool = show_time
        self._timer_start: float = 0

        # General layout
        self.setSizePolicy(QSizePolicy.Policy.Expanding, QSizePolicy.Policy.Fixed)
        self._layout = QVBoxLayout()
        self.setLayout(self._layout)
        # The progress bar itself
        self._progress_bar = QProgressBar(self)
        self._progress_bar.setMaximum(100)
        self._progress_bar.setGeometry(0, 0, 350, 25)
        self._progress_bar.setValue(0)
        self._layout.addWidget(self._progress_bar)
        # Setup time display
        self._time_wrapper = QHBoxLayout()
        self._time_expired_label = QLabel("")
        self._time_expired_label.setSizePolicy(QSizePolicy.Policy.Expanding, QSizePolicy.Policy.Fixed)
        self._time_left_label = QLabel("")
        self._time_left_label.setSizePolicy(QSizePolicy.Policy.Expanding, QSizePolicy.Policy.Fixed)
        self._time_wrapper.addWidget(self._time_expired_label)
        self._time_wrapper.addWidget(self._time_left_label)
        self._layout.addLayout(self._time_wrapper)
        # Set Size
        self.resize(500, 80)
        # Register progress update handler
        # noinspection PyUnresolvedReferences
        self.set_progress.connect(self._set_progress)

    def startTimer(self):
        """Start the internal timer"""
        self._timer_start = timeit.default_timer()

    def _set_progress(self, percentage_done):
        """Handle incoming progress updates
        :type percentage_done: int
        :param percentage_done: The progress to set, from 0 to 100
        """
        self._progress_bar.setValue(percentage_done)
        if self._show_time:
            time_elapsed, time_remaining = self._get_progress_times(percentage_done)
            self._time_expired_label.setText("Time elapsed: " + str(time_elapsed) + "s")
            self._time_left_label.setText("Time remaining: " + str(time_remaining) + "s")

    def _get_progress_times(self, percentage_done):
        """Get the elapsed and remaining time
        :type percentage_done: float
        :param percentage_done: How much progress has been made
        :return float, float: Time elapsed, Time remaining"""
        time_elapsed = math.floor(timeit.default_timer() - self._timer_start)
        if 0 == percentage_done:
            percentage_done = 1
        time_remaining = math.floor(time_elapsed * (100 / percentage_done)) - time_elapsed
        return time_elapsed, time_remaining

    def close_with_delay(self):
        """Close the progress bar widget with a short delay"""
        sleep(0.8)
        self.close()