PyQt5利用线程生成一个Label,实时显示当前时间。

vs 2017…

#调用模块
    from PyQt5 import QtCore, QtGui, QtWidgets
    from PyQt5.QtCore import QThread
    import time
    import datetime



class Ui_MainWindow(object):
    def setupUi(self, MainWindow): 
        ...
        ...
        self.label_6 = QtWidgets.QLabel(self.centralwidget)
        self.label_6.setGeometry(QtCore.QRect(630,520,121,20))
        self.label_6.setObjectName("label_6")
        self.dowork()
    def dowork(self):
        self.thread = Call_Thread()
        self.thread.update_text_singal.connect(self.update_text)
        self.thread.start()
    def update_text(self,text):
        self.label_6.setText(text)
class Call_Thread(QtCore.QThread):
    def __init__(self,parent = None):
        super(Call_Thread,self).__init__(parent)
    update_text_singal = QtCore.pyqtSignal(str)
    def run(self):
           while(True):
               now_time = datetime.datetime.now().strftime('%Y.%m.%d %H:%M:%S')
               time.sleep(1)
               self.update_text_singal.emit(now_time)

你可能感兴趣的:(代码)