PyQT5-QProgressBar

 1 """
 2     QProgressBar:进度条是用来展示任务进度的,它的滚动能够让用户了解到任务的进度, 提供了水平和垂直进度条,有最大值和最小值,默认是0-99
 3     Author:dengyexun
 4     DateTime:2018.11.22
 5 """
 6 from PyQt5.QtWidgets import QWidget, QApplication, QProgressBar, QPushButton
 7 from PyQt5.QtCore import QBasicTimer    # 调用时间类
 8 import sys
 9 
10 class Example(QWidget):
11 
12     def __init__(self):
13         super().__init__()
14 
15         self.initUI()
16 
17     def initUI(self):
18         # 初始化QProgressBar
19         self.pbar = QProgressBar(self)
20         self.pbar.setGeometry(30,40,200,30)
21 
22         # 初始化QPushButton
23         self.btn = QPushButton("start", self)
24         self.btn.move(40, 80)
25         self.btn.clicked.connect(self.doAction)
26 
27         # 初始化QBasicTimer
28         self.timer = QBasicTimer()
29         self.step = 0       # 用来计算时间
30 
31         # 主界面
32         self.setGeometry(300,300,270,180)
33         self.setWindowTitle("QProgressBar")
34         self.show()
35 
36     def timerEvent(self, e):
37         """
38         时间事件函数,传递时间数据进行处理
39         :param e:
40         :return:
41         """
42         if self.step > 100:
43             # 停止计时
44             self.timer.stop()
45             # button中设置文本为完成
46             self.btn.setText('Finished!')
47             # 程序结束
48             return
49         self.step = self.step + 1
50         # 进度条不是文本,设置值的时候用value处理
51         self.pbar.setValue(self.step)
52 
53     def doAction(self):
54         """
55         触发什么样的动作,用来控制开始和停止的
56         :return:
57         """
58         if self.timer.isActive():
59             # 计时器是活跃的
60             self.timer.stop()
61             self.btn.setText("start")
62         else:
63             self.timer.start(100, self)
64             self.btn.setText("stop")
65 
66 
67 if __name__ == '__main__':
68     app = QApplication(sys.argv)
69     ex = Example()
70     sys.exit(app.exec_())
原文地址:https://www.cnblogs.com/demo-deng/p/10000621.html