当前位置: 首页 > article >正文

PyQt基础——简单闹钟ui实现(图形化界面、定时器事件)

一、代码展示:

import sys

from PyQt6.QtCore import QTimer, QTime
from PyQt6.QtWidgets import QWidget, QApplication, QLabel, QPushButton, QLineEdit
from PyQt6 import uic
from PyQt6.QtTextToSpeech import QTextToSpeech


# 封装一个我的窗口类
class MyWidget(QWidget):
    def __init__(self):
        super().__init__()
        # 通过uic将ui界面加载到程序中来
        ui = uic.loadUi("./Form.ui",self)
        self.label:QLabel = ui.label
        self.pushButton:QPushButton = ui.pushButton
        self.lineEdit:QLineEdit = ui.lineEdit
        self.label_4:QLabel = ui.label_4

        self.timer = QTimer()
        self.timer.timeout.connect(self.timeout_slot)
        self.timer.start(1000)

        self.alarm_timer = QTimer()
        self.alarm_timer.timeout.connect(self.stop_alarm)
        self.pushButton.clicked.connect(self.pushButton_slot)
        self.alarm_time = None
        self.alarm_active = False
        self.alarm_sounding = False
        self.speecher = QTextToSpeech()
    def pushButton_slot(self):
        if self.pushButton.text() == "启动":
            input_time = self.lineEdit.text()
            try:
                self.alarm_time = QTime.fromString(input_time,"hh:mm:ss")
                if self.alarm_time.isValid():
                    self.pushButton.setText("关闭")
                    self.alarm_active = True
                    self.label_4.setText(f"现在是北京时间{input_time}")
                else:
                    self.label_4.setText("输入的时间格式不正确,请使用 hh:mm:ss")
            except ValueError:
                self.label_4.setText("输入的时间格式不正确,请使用 hh:mm:ss")
        else:
            if self.alarm_sounding:
                self.stop_alarm()
            else:
                self.pushButton.setText("启动")
                self.alarm_active = False
                self.label_4.setText("")

    def timeout_slot(self):
        sys_time = QTime.currentTime()
        t = sys_time.toString("hh:mm:ss")
        self.label.setText(t)
        if self.alarm_active and sys_time>=self.alarm_time and not self.alarm_sounding:
            self.alarm_active = False
            self.alarm_sounding = True
            self.alarm_timer.start(30000)
            self.start_alarm()

    def start_alarm(self):
        def speak_loop():
            if self.alarm_sounding:
                self.speecher.say(self.label_4.text())
                QTimer.singleShot(10000,speak_loop)
        speak_loop()

    def stop_alarm(self):
        self.alarm_timer.stop()
        self.alarm_sounding = False
        self.pushButton.setText("启动")
        self.label_4.setText("")

if __name__ == '__main__':
    app = QApplication(sys.argv)
    myWidget = MyWidget()
    myWidget.show()
    sys.exit(app.exec())

二、结果展示:


http://www.kler.cn/a/587318.html

相关文章:

  • C#的简单工厂模式、工厂方法模式、抽象工厂模式
  • Umi-OCR 全家桶
  • C++20 `<bit>` 中的整数 2 的幂运算和 `std::bit_cast`:由浅入深的探索
  • 定制开发开源 AI 智能名片 S2B2C 商城小程序源码在小程序直播营销中的应用与价值
  • matlab 量化交易投资策略研究
  • 基于 Verilog 的 4 位二进制计数器设计与实验:从理论到实践的深度剖析
  • 数据库版本问题导致的查询bug
  • 宇数科技激光雷达L2
  • 3ds Max 鼠标与快捷键组合操作指南
  • 【愚公系列】《高效使用DeepSeek》009-PPT大纲自动生成
  • DeepSeek:为教培小程序赋能,引领行业变革新潮流
  • DeepSeek对两个网页所描述的数据库高可用方案的分析与比较
  • 什么是网络协议
  • 炼丹师的魔法工坊:玩转 TensorFlow Keras Sequential 模型
  • 什么是 HTML?
  • Ubuntu 常用指令手册
  • PAT甲级(Advanced Level) Practice 1021 Deepest Root
  • HTML+CSS基础(了解水平)
  • 【QT】-一文读懂抽象类
  • golang从入门到做牛马:第二十一篇-Go语言错误处理:优雅的“故障排除”