1 import sys 2 from PyQt5.QtCore import Qt, QTimer 3 from PyQt5.QtWidgets import QApplication, QWidget, QProgressBar, QPushButton, QHBoxLayout, QVBoxLayout 4 5 6 class Demo(QWidget): 7 def __init__(self): 8 super(Demo, self).__init__() 9 self.progressbar = QProgressBar(self) #实例化进度条 10 #self.progressbar.setOrientation(Qt.Vertical) #设置进度条为垂直方向,默认是水平的 11 self.progressbar.setMinimum(0) # 设置进度条的最小值 12 self.progressbar.setMaximum(100) #设置进度条的最大值 13 # self.progressbar.setRange(0, 100) #设置进度条的范围 14 15 self.step = 0 16 17 self.timer = QTimer(self) 18 self.timer.timeout.connect(self.update_func) 19 20 self.ss_button = QPushButton(‘Start‘, self) 21 self.ss_button.clicked.connect(self.start_stop_func) 22 self.reset_button = QPushButton(‘Reset‘, self) 23 self.reset_button.clicked.connect(self.reset_func) 24 25 self.h_layout = QHBoxLayout() 26 self.v_layout = QVBoxLayout() 27 28 self.h_layout.addWidget(self.ss_button) 29 self.h_layout.addWidget(self.reset_button) 30 self.v_layout.addWidget(self.progressbar) 31 self.v_layout.addLayout(self.h_layout) 32 33 self.setLayout(self.v_layout) 34 35 def start_stop_func(self): 36 if self.ss_button.text() == ‘Start‘: 37 self.ss_button.setText(‘Stop‘) 38 self.timer.start(100) 39 else: 40 self.ss_button.setText(‘Start‘) 41 self.timer.stop() 42 43 def update_func(self): 44 self.step += 1 45 self.progressbar.setValue(self.step) #设置进度条的值 46 47 if self.step >= 100: 48 self.ss_button.setText(‘Start‘) 49 self.timer.stop() 50 self.step = 0 51 52 def reset_func(self): 53 self.progressbar.reset() #重置进度条 54 self.ss_button.setText(‘Start‘) 55 self.timer.stop() 56 self.step = 0 57 58 59 if __name__ == ‘__main__‘: 60 app = QApplication(sys.argv) 61 demo = Demo() 62 demo.show() 63 sys.exit(app.exec_())
原文地址:https://www.cnblogs.com/liming19680104/p/10359970.html
时间: 2024-10-16 20:42:38