作业to时间事件

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())
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值