首页 > 其他分享> > PyQt5基础学习-QThread(线程) 1.pyqtSignal(信号发射器) 2.pyqtSignal().emit(发送信号) 3.QLCDNumber(计数时间框) 4.QLCDNumber
PyQt5基础学习-QThread(线程) 1.pyqtSignal(信号发射器) 2.pyqtSignal().emit(发送信号) 3.QLCDNumber(计数时间框) 4.QLCDNumber
作者:互联网
构造线程的信息函数,定时发送信号, 将时间信息器绑定函数,实现定时执行的操作
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_())
标签:__,end,pyqtSignal,self,计数,sec,def,QLCDNumber,QThread 来源: https://www.cnblogs.com/hyq-lst/p/15865032.html