PyQt5–QProgressBar

  • 2020 年 1 月 19 日
  • 筆記

 1 # -*- coding:utf-8 -*-   2 '''   3 Created on Sep 20, 2018   4   5 @author: SaShuangYiBing   6   7 Comment:   8 '''   9 import sys  10 from PyQt5.QtCore import QBasicTimer  11 from PyQt5.QtWidgets import QApplication,QWidget,QProgressBar,QPushButton  12  13 class New_test(QWidget):  14     def __init__(self):  15         super().__init__()  16         self.initUI()  17  18     def initUI(self):  19         self.pbar = QProgressBar(self)  20         self.pbar.setGeometry(30,40,200,25)  21  22         self.btn = QPushButton('Start',self)  23         self.btn.move(75,80)  24         self.btn.clicked.connect(self.doAction)  25  26         self.timer = QBasicTimer()  27         self.step = 0  28  29         self.setGeometry(300,300,280,170)  30         self.setWindowTitle('QProgress Bar')  31         self.show()  32  33     def timerEvent(self, e):  34         if self.step >= 100:  35             self.timer.stop()  36             self.btn.setText('Finished')  37             return  38         self.step += 1  39         self.pbar.setValue(self.step)  40  41     def doAction(self,e):  42         if self.timer.isActive():  43             self.timer.stop()  44             self.btn.setText('Start')  45         else:  46             self.timer.start(100,self)  47             self.btn.setText('Stop')  48  49 if __name__ == '__main__':  50     app = QApplication(sys.argv)  51     ex = New_test()  52     sys.exit(app.exec_())