如何使计时器/时钟移动几秒钟?

这是我的代码,当我运行它时,它的作用不像时钟。它所做的一切都会返回一个字符串。但是我想要的是时间在移动。帮助将不胜感激,谢谢您

def DateTime(self):
    dateandtime = QDialog()

    dateandtime.setWindowTitle("Date and Time")

    hbox = QHBoxLayout()

    datetime = qdateTime.currentDateTime()
    show = datetime.toString(Qt.DefaultLocaleLongDate)

    label = QLabel(show)
    label.setfont(QtGui.QFont("Arial",10))
    hbox.addWidget(label)

    dateandtime.setLayout(hbox)
hijbly 回答:如何使计时器/时钟移动几秒钟?

应该有一个更优雅的方法,但是找到一个带有计时器的示例:

from PyQt5.QtWidgets import QDialog,QHBoxLayout,QLabel,QApplication
from PyQt5.QtCore import QDateTime,Qt,QTimer
from PyQt5 import QtGui
import sys


class Dialog(QDialog):
    def __init__(self):
        QDialog.__init__(self)
        datetime = QDateTime.currentDateTime()
        show = datetime.toString(Qt.DefaultLocaleLongDate)
        self.label = QLabel(show)
        self.DateTime()

    def DateTime(self):
        # dateandtime = QDialog()
        self.setWindowTitle("Date and Time")
        hbox = QHBoxLayout()
        self.label.setFont(QtGui.QFont("Arial",10))
        hbox.addWidget(self.label)

        self.setLayout(hbox)

    def tick(self):
        datetime = QDateTime.currentDateTime().toString(Qt.DefaultLocaleLongDate)
        self.label.setText(datetime)


app = QApplication(sys.argv)
dialog = Dialog()
dialog.show()
timer = QTimer()
timer.timeout.connect(dialog.tick)
timer.start(1000)
app.exec_()
本文链接:https://www.f2er.com/3163061.html

大家都在问