generally speaking ,QProgressBar  The application will do some delay operation , This will cause the main route card ui,PyQt Threads in can solve this problem perfectly , This example uses python Medium time The module delays , And put the delay operation in the sub thread to execute , realization Qt Of UI Real time refresh .
import sys import time from PyQt5.QtCore import QThread, pyqtSignal from
PyQt5.QtWidgets import QWidget, QPushButton, QProgressBar, QVBoxLayout,
QApplication class Thread(QThread): _signal = pyqtSignal(int) # Define the signal type as integer def
__init__(self): super(Thread, self).__init__() def __del__(self): self.wait()
def run(self): for i in range(100): time.sleep(0.1) self._signal.emit(i) # Transmit signal
class Example(QWidget): def __init__(self): super(Example, self).__init__()
self.setWindowTitle('QProgressBar') self.btn = QPushButton(' Start thread ')
self.btn.clicked.connect(self.btnFunc) # Connection slot function self.pbar = QProgressBar(self)
self.pbar.setValue(0) self.resize(300, 300) self.vbox = QVBoxLayout()
self.vbox.addWidget(self.pbar) self.vbox.addWidget(self.btn)
self.setLayout(self.vbox) self.show() def btnFunc(self): self.thread = Thread()
# Instantiation thread self.thread._signal.connect(self.signal_accept)
# Connect the signal defined in thread accumulation to the signal receiving function in this class self.thread.start()
# Start thread , The start thread calls the start method , This method calls the run function , So you don't have to call run function self.btn.setEnabled(False)
def signal_accept(self, msg): self.pbar.setValue(int(msg)) # Pass the thread's parameters into the progress bar if
self.pbar.value() == 99: self.pbar.setValue(0) self.btn.setEnabled(True) if
__name__ == "__main__": app = QApplication(sys.argv) ex = Example() ex.show()
sys.exit(app.exec_())
effect : 

 

Technology
©2019-2020 Toolsou All rights reserved,
Send love - A little romance for programmers VHDL—— Design of frequency divider Python Implementation of Hanoi Tower code It's over , Starting salary 30khtml+css+js Make a simple website home page QQ Login interface implementation Hill sorting of sorting algorithm ——c++ realization 【 Wechat applet learning 】 Netease music cloud code page implementation details Resume the 13th session python Blue Bridge Cup 2022 Solution to the 13th Blue Bridge Cup ( whole )