from PyQt5.QtWidgets import QApplication, QWidget, QProgressBar, QPushButton
from PyQt5.QtCore import QBasicTimer
from PyQt5.QtGui import QIcon
import sys
class Example(QWidget):
def __init__(self):
super().__init__()
self.initUI()
self.btn.clicked.connect(self.Action)
def initUI(self):
self.pbar = QProgressBar(self)
self.pbar.setGeometry(50,50,200,25)
self.btn = QPushButton("运行 or 停止",self)
self.btn.move(105,90)
self.timer = QBasicTimer()
self.step = 0
self.setGeometry(300,300,300,180)
self.setWindowTitle("Demo for Lin")
self.show()
def timerEvent(self, *args, **kwargs):
if self.step >= 100:
self.timer.stop()
self.btn.setText("完成")
return
self.step = self.step+1
self.pbar.setValue(self.step)
def Action(self):
if self.timer.isActive():
self.timer.stop()
self.btn.setText("运行")
else:
self.timer.start(100,self)
self.btn.setText("停止")
if __name__=='__main__':
app = QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
运行结果: