pyqt progress bar creation and closing

Make a progress bar for the detection system

核心代码如下:
from PyQt5 import QtCore, QtGui, QtWidgets
import sys
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
import probar

class Ui_MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()
    def setupUi(self, MainWindow):

        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(400, 300)
        MainWindow.setStyleSheet("background-color: rgb(190, 193, 245);")
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.progressBar = QtWidgets.QProgressBar(self.centralwidget)
        self.progressBar.setGeometry(QtCore.QRect(10, 220, 380, 20))
        self.progressBar.setStyleSheet("\n"
"QProgressBar{\n"
"border:1px solid  #ffffff;\n"
"height:10;\n"
"text-align:right;\n"
"border-radius:7px;}\n"
" \n"
"\n"
"QProgressBar::chunk{\n"
"border-radius:6px;\n"
"border:1px solid #5865d0;\n"
"background-color:#5865d0;\n"
"}")

        # self.progressBar.setProperty("value", 0)
        self.progressBar.setObjectName("progressBar")
        self.timer = QBasicTimer()

        self.label = QtWidgets.QLabel(self.centralwidget)
        self.label.setGeometry(QtCore.QRect(170, 40, 70, 100))
        self.label.setText("")
        self.label.setPixmap(QtGui.QPixmap("image/logo2.png"))
        self.label.setObjectName("label")
        self.label_2 = QtWidgets.QLabel(self.centralwidget)
        self.label_2.setGeometry(QtCore.QRect(90, 180, 310, 30))
        font = QtGui.QFont()
        font.setFamily("Poor Richard")
        font.setPointSize(11)
        self.label_2.setFont(font)
        self.label_2.setStyleSheet("\n"
"color: rgb(77, 100, 198);")
        self.label_2.setObjectName("label_2")
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 400, 20))
        self.menubar.setObjectName("menubar")
        MainWindow.setMenuBar(self.menubar)
        self.statusbar = QtWidgets.QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)

        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)


    def retranslateUi(self, MainWindow):

        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "loading"))
        self.label_2.setText(_translate("MainWindow", "Welcome to intelligent detection system\n"
""))

    def timerEvent(self, e):

        if self.step >= 100:
            self.step = 0
            self.pbar.setValue(self.step)
            self.timer.stop()
            return
        self.step = self.step + 1
        self.pbar.setValue(self.step)

    def doAction(self, value):
        print("do action")
        if self.timer.isActive():
            self.timer.stop()
        else:
            self.timer.start(100, self)



if __name__=='__main__':
    QtCore.QCoreApplication.setAttribute(QtCore.Qt.AA_EnableHighDpiScaling)
    app=QApplication(sys.argv)
    mainWindow = QMainWindow()
    ui = probar.Ui_MainWindow()
    ui.setupUi(mainWindow)
    mainWindow.show()
    for i in range(100):
        # time.sleep(0.05)
        ui.progressBar.setValue(i + 1)  # 更新进度条的值
        QThread.msleep(int(0.5 * 100))  # 模拟文件传送,进度条可以一点点增加,而不是一下增加很多,也可以不需要
        QApplication.processEvents()  # 实时显示
        if(i == 100):
            sys.exit(app.exec_())

operation result

Insert picture description here

The problem with the progress bar

Style adjustment

Picture setting:
self.label.setPixmap(QtGui.QPixmap(“image/logo2.png”))
Waiting for text setting:
self.label_2.setText(_translate(“MainWindow”, “Welcome to intelligent detection system\n”
“”) )

Automatically without touching:

 for i in range(100):
        # time.sleep(0.05)
        ui.progressBar.setValue(i + 1)  # 更新进度条的值
        QThread.msleep(int(0.5 * 100))  # 模拟文件传送,进度条可以一点点增加,而不是一下增加很多,也可以不需要
        QApplication.processEvents()  # 实时显示
        if(i == 100):
            sys.exit(app.exec_())#当进度达到100时自动关闭窗口

The button triggers the progress bar:

from PyQt5.QtWidgets import QApplication, QProgressBar, QPushButton
from PyQt5 import QtWidgets
from PyQt5.QtCore import Qt, QBasicTimer
 
class ProgressBar(QtWidgets.QWidget):
    def __init__(self, parent= None):
        QtWidgets.QWidget.__init__(self)
        
        self.setGeometry(300, 300, 250, 150)
        self.setWindowTitle('ProgressBar')
        self.pbar = QProgressBar(self)
        self.pbar.setGeometry(30, 40, 200, 25)
        
        self.button = QPushButton('Start', self)
        self.button.setFocusPolicy(Qt.NoFocus)
        self.button.move(40, 80)
        
        self.button.clicked.connect(self.onStart)
        self.timer = QBasicTimer()
        self.step = 0
        
    def timerEvent(self, event):
        if self.step >=100:
            self.timer.stop()
            return
        self.step = self.step + 1
        self.pbar.setValue(self.step)
        
    def onStart(self):
        if self.timer.isActive(): 
            self.timer.stop()
            self.button.setText('Start')
        else:
            self.timer.start(100, self)
            self.button.setText('Stop')
        
 
if __name__ == "__main__":
    import sys
    app = QApplication(sys.argv)
    qb = ProgressBar()
    qb.show()
    sys.exit(app.exec_())

Here is a quote. In this example, we created a horizontal progress bar and a button. Buttons are used to start or stop the progress.

self.pbar= QProgressBar(self)
creates a progress bar.
self.timer= QBasicTimer()
creates a timer object.
self.timer.start(100,self)
needs to activate the progress bar, we need to start the timer using the timer's start() method. The first parameter of this method is the timeout period. The second parameter represents the recipient of the timeout event triggered by the timer after the current timeout period expires.

 def  timerEvent(self, event):

                   if self.step >=100:

                           self.timer.stop()

                           return

                   self.step = self.step + 1

                   self.pbar.setValue(self.step)

Every QObject object or its sub-objects has the QObject.timerEvent method. In this example, in order to respond to the timer's timeout event, we need to use the above code to rewrite the timerEvent method of the progress bar.

Guess you like

Origin blog.csdn.net/lockhou/article/details/113432271