1.未开启闹钟:
2.
启动闹钟后: 可以通过停止按钮,停止闹钟
3.达到闹钟时间时:
代码展示:
import sys
from PyQt6 import uic
from PyQt6.QtCore import QTime, QTimer, QDateTime
from PyQt6.QtWidgets import QWidget, QApplication, QPushButton, QLabel, QLineEdit, QTimeEdit
class MyWidget(QWidget):
def __init__(self):
super().__init__()
ui = uic.loadUi("./task.ui", self)
self.pushButton:QPushButton = ui.pushButton
self.label1:QLabel = ui.label1
self.label2:QLabel = ui.label2
self.label3:QLabel = ui.label3
self.timeEdit:QTimeEdit = ui.timeEdit
#用于记录系统时间的时间事件触发
self.timer = QTimer()
self.timer.timeout.connect(self.sys_timeout_slot)
self.timer.start()
self.pushButton.clicked.connect(self.btn_slot)
def sys_timeout_slot(self):
current_time = QDateTime.currentDateTime().toString("yyyy-MM-dd hh:mm:ss")
self.label1.setText(current_time)
def btn_slot(self):
if self.pushButton.text() == '启动':
self.timerId = self.startTimer(1000)
self.pushButton.setText('停止')
else:
self.killTimer(self.timerId)
self.pushButton.setText('启动')
def timerEvent(self, a0):
sys_time = QTime.currentTime()
time = sys_time.toString("hh:mm:ss")
timeForClock = sys_time.toString("hh:mm")
self.label2.setText(time)
if timeForClock == self.timeEdit.text():
self.label3.setText('懒虫起床啦!好好学习,天天向上!')
if __name__ == '__main__':
app = QApplication(sys.argv)
myWidget = MyWidget()
myWidget.show()
sys.exit(app.exec())