PyQt5最全84 信号与槽之多线程更新UI数据
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
import sys
import time
class BackendThread(QThread):
"""
多线程更新UI数据
"""
update_date = pyqtSignal(str)
def run(self):
while True:
date = QDateTime.currentDateTime()
currentTime = date.toString('yyyy-MM-dd hh:mm:ss')
self.update_date.emit(str(currentTime))
time.sleep(1)
class ThreadUpdateUI(QDialog):
def __init__(self):
QDialog.__init__(self)
self.setWindowTitle('多线程更新UI数据')
self.resize(400, 100)
self.input = QLineEdit(self)
self.input.resize(400, 100)
self.initUI()
def initUI(self):
self.backend = BackendThread()
self.backend.update_date.connect(self.handleDisplay)
self.backend.start()
def handleDisplay(self, date):
self.input.setText(date)
if __name__ == '__main__':
app = QApplication(sys.argv)
example = ThreadUpdateUI()
print(example.__doc__)
example.show()
sys.exit(app.exec_())
结果
每隔一秒发一个信号,不断更新文本信息
|