PyQT5訊號重新整理時間

星空28發表於2024-06-08
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_())

相關文章