应该有一个更优雅的方法,但是找到一个带有计时器的示例:
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