簡體   English   中英

如何使計時器/時鍾按秒移動?

[英]How do I make the timer/clock move by the seconds?

這是我的代碼,當我運行它時,它不像時鍾。 它所做的一切都返回一個字符串。 但我想要的是時間以秒為單位移動。 幫助將不勝感激謝謝

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)

應該有更優雅的方式,但是找一個帶有計時器的例子:

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_()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM