构造线程的信息函数,定时发送信号, 将时间信息器绑定函数,实现定时执行的操作
Counter.py
""" 使用线程类(QThread)编写计数器 QThread def run(self): while True: self.sleep(1) if sec == 5: break; QLCDNumber WorkThread(QThread) 用到自定义信号 """ from PyQt5.QtWidgets import * from PyQt5.QtCore import QTimer, QDateTime from PyQt5.QtGui import * from PyQt5.QtCore import * import sys sec = 0 class WorkThread(QThread): timer = pyqtSignal() #每隔一秒发送一次信号 end = pyqtSignal() #计数完成后发送一次信号 def run(self): while True: self.sleep(1) #休眠一秒 if sec == 5: self.end.emit() #发送end信号 break self.timer.emit() #发送timer信号 class Counter(QWidget): def __init__(self): super(Counter, self).__init__() self.setWindowTitle("使用线程类(QThread)编写计数器") self.resize(300, 120) layout = QVBoxLayout() self.lcdNumber = QLCDNumber() layout.addWidget(self.lcdNumber) button = QPushButton('开始计数') layout.addWidget(button) self.workThread = WorkThread() #将时间 self.workThread.timer.connect(self.countTime) self.workThread.end.connect(self.end) button.clicked.connect(self.work) self.setLayout(layout) #定时显示数字 def countTime(self): global sec sec += 1 self.lcdNumber.display(sec) #结束后做窗口的显示 def end(self): QMessageBox.information(self, "消息", "计数结束", QMessageBox.Ok) def work(self): self.workThread.start() #将线程进行启动 if __name__ == "__main__": app = QApplication(sys.argv) main = Counter() main.show() sys.exit(app.exec_())