import time
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
import sys
class BackendThread(QThread):
update_date = pyqtSignal(str)
def run(self):
while True:
data = QDateTime.currentDateTime()
currentTime = data.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) # 将QLineEdit绑定到窗口
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, data):
self.input.setText(data)
if __name__ == '__main__':
app = QApplication(sys.argv)
main_win = ThreadUpdateUI()
main_win.show()
sys.exit(app.exec_())