Writing in the front
A progress bar placed at the status bar. I wanted to put it in the main interface, but I did not find the relevant code.
code
# -*- coding: utf-8 -*- from PyQt5 import QtCore, QtWidgets from PyQt5.QtWidgets import QProgressBar, QLabel, QApplication, QMainWindow from PyQt5.QtCore import QBasicTimer import sys class Ui_MainWindow(QMainWindow): def __init__(self,parent=None): Super (UI_MainWindow,self).__init__(parent) self.setupui () # Here we can define some global variables used for the current class self.filePath = "def setupUi(self): Set window object name to self. SetObjectName (" mainWindow ") 600) button # define self. StartButton = QtWidgets. QPushButton (self) # button object name (not the button to display content) self. StartButton. SetObjectName (" Mr Pred)" # set button position self. StartButton. SetGeometry (QtCore. QRect (10, 300, 93, 28)) # Settings button to display the self. The startButton. The setText (" start "button # for binding events (click on the button will trigger) self. StartButton. Clicked. Connect (self) predict) If you want to click the button to start the progress bar, you can use the following line to bind events directly to the button. When you click the button, you can also bind events directly to the button. Began to go progress bar # self. StartButton. Clicked. Connect (self. OnStart) # define status bar self. The statusbar. = QtWidgets QStatusBar # (self) Sets the status bar to the current window status bar of the self. The setStatusBar # (self. The statusbar) sets the status bar object name self. The statusbar. SetObjectName (" statusbar ") # set the style of the status bar self.statusbar.setStyleSheet('QStatusBar::item {border: none; }') # define text label self.statusLabel = QLabel() # setText label display content self.statusLabel. SetText (" ready ") # define horizontal progressBar self.progressBar = QProgressBar() # Sets the range of the progress bar with 1 as the minimum and 2 as the maximum (you can make it larger, Such as 1000 self. ProgressBar. SetRange (# 0, 100) sets the progress bar the initial value of the self. The progressBar. SetValue (0) # set the timer (walk you need to use the progress bar, progress bar will not change, Self. Timer = qBasicTimer () self. Step = 0 # Stretch the state bar and stretch it self.statusbar.addPermanentWidget(self.startButton, stretch=0) self.statusbar.addPermanentWidget(self.statusLabel, stretch=2) self.statusbar.addPermanentWidget(self.progressBar, Stretch = 10) # other interface Settings self. RetranslateUi () QtCore. QMetaObject. ConnectSlotsByName (self) def retranslateUi (self) : _translate = QtCore. QCoreApplication. Translate # set the window title. Self setWindowTitle (_translate (" MainWindow." "TestLoading ") # Each QObject or its children has a QObject.TimerEvent method. # Override the timerEvent method of the progress bar in response to a timeout event. def timerEvent(self, event): if self.step >= 100: Self. The timer. The stop () # modify text label display contents self. StatusLabel. The setText (" predict completion ") # enable button self. The startButton. SetEnabled (True) # modify button to display content Self. StartButton. SetText (" start ") return steps # cumulative self. Step = self. Step + 1 # modify the value of the progress bar self. The progressBar. SetValue (self. Step) def onStart(self): # modify text label display self. StatusLabel. SetText (" please later ") # disable button. Self startButton. SetEnabled (False) # modify button to display content Self.startButton.setText (" Predicted...") ) # Activate the progress bar by starting the timer with its start() method. # Parameter 1: timeout time; Parameter 2: The object that receives the timeout event triggered by the timer after the timeout time is up. Self.timer.start (100, self) # handles specific business logic, such as calling deep learning models for predictions. Def predict (self) : self. OnStart () # # # # # # # # # # # # # # # # in print simulation model called # # # # # # # # # # # # # # # # # # # # print (' predict... ') if __name__ == '__main__': app = QApplication(sys.argv) ui = Ui_MainWindow() ui.show() sys.exit(app.exec_())
Effect diagram
If can’t see the renderings, please go to this page to see https://blog.csdn.net/qq_42772612/article/details/115557955
reference
Python3. X + PyQT5 implementation of the main window status bar (embedded) display progress bar
PyQT5: QProgressBar (26)
PYQT5 Tutorial -14- Progress Bar
The progress bar for PyQT5 in Python
Use of PyQT5 QProgressBar progress bars
PyQT5 – Use of MenuBar Toolbar StatusBar (QMenuBar, QToolbar, QStatusBar)
PyQT5 implements pop-up progress bars in other Windows
PyQT5 implements the statusBar (statusBar) display and hide function