文件名:showtimeQTimer.py
库:PyQt5(QtCore.QTimer, QtCore.QDateTime),time
实现代码如下:
# coding :utf-8
# @Time :2022-01-10 23:24
# @Author :Kevin
# @Software :PyCharm
# @File :showtimeQTimer.py
import sys
import time
from PyQt5 import QtCore
from PyQt5.QtWidgets import QLabel, QApplication, QMainWindow, QLCDNumber, QWidget, QGridLayout
class ShowtimeQTimer(QMainWindow):
def __init__(self):
super().__init__()
self.init_ui()
def init_ui(self):
self.setWindowTitle('test')
self.resize(400,300)
self.label0 = QLabel(self)
self.lcdNum = QLCDNumber(self)
self.lcdNum.setDigitCount(8)
self.layout = QGridLayout()
self.widget = QWidget()
self.widget.setLayout(self.layout)
self.setCentralWidget(self.widget)
self.layout.addWidget(self.label0,0,0,1,2)
self.layout.addWidget(self.lcdNum,1,0,1,2)
timer = QtCore.QTimer(self)
timer.timeout.connect(self.showtime_slot2) # 槽函数showtime_slot/showtime_slot2
timer.start(1000)
def showtime_slot(self):
str_time = time.strftime("%Y-%m-%d %X",time.localtime())
self.label0.setText(str_time)
self.lcdNum.display(str_time)
def showtime_slot2(self):
time0 = QtCore.QDateTime.currentDateTime()
str_time = time0.toString('yyyy-mm-dd hh:mm:ss')
self.label0.setText(str_time)
self.lcdNum.display(str_time)
if __name__ == '__main__':
app = QApplication(sys.argv)
showtimeWin = ShowtimeQTimer()
showtimeWin.show()
sys.exit(app.exec_())