凌的博客

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

python

25. PyQt6 QProgressBar

2023-10-17 python 449
import sys
from PyQt6.QtWidgets import QApplication, QMainWindow, QProgressBar, QPushButton
from PyQt6.QtGui import QGuiApplication
from PyQt6.QtCore import QBasicTimer


class App(QMainWindow):

    def __init__(self):
        super().__init__()
        self.step = None
        self.timer = None
        self.btn = None
        self.pbar = None
        self.initUI()

    def initUI(self):
        self.setWindowTitle("PyQt6 QProgressBar")

        self.pbar = QProgressBar(self)
        self.pbar.setGeometry(30, 40, 200, 25)

        self.btn = QPushButton("开始", self)
        self.btn.move(40, 80)
        self.btn.clicked.connect(self.doAction)

        self.timer = QBasicTimer()
        self.step = 0

        self.setGeometry(100, 100, 400, 300)
        self.center()

    def timerEvent(self, e):
        if self.step > 100:
            self.timer.stop()
            self.btn.setText("完毕")
            return

        self.step = self.step + 1
        self.pbar.setValue(self.step)

    def doAction(self):
        if self.timer.isActive():
            self.timer.stop()
            self.btn.setText("开始")
        else:
            self.timer.start(100, self)
            self.btn.setText("结束")

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


if __name__ == "__main__":
    app = QApplication(sys.argv)
    cls_app = App()
    cls_app.show()
    sys.exit(app.exec())

image.png

文章评论

0条评论