珠海网站制作设计,Wordpress手机 菜单,wordpress+播放列表,弄个盈利网站做什么一、实例前置 一个小闹钟应用 创建主窗口类 首先我们创建了一个名为AlarmClock的类#xff0c;它继承自QMainWindow。这个类将包含我们的GUI组件和逻辑。
from Alarm_clock import Ui_MainWindowclass AlarmClock(QMainWindow):def __init__(self):super().__init__()# 初始化…一、实例前置 一个小闹钟应用 创建主窗口类 首先我们创建了一个名为AlarmClock的类它继承自QMainWindow。这个类将包含我们的GUI组件和逻辑。
from Alarm_clock import Ui_MainWindowclass AlarmClock(QMainWindow):def __init__(self):super().__init__()# 初始化 UIself.ui Ui_MainWindow()self.ui.setupUi(self)
二、创建UI界面 预先设计好的UI文件Alarm_clock.ui 这里我们使用pyside6-uic把一个预先设计好的UI文件Alarm_clock.ui转成py格式然后通过from import导入这个文件定义了我们的界面布局
三、QtWidgets
在 PyQt 中QtWidgets是一个非常重要的模块它提供了大量用于构建图形用户界面GUI的部件Widgets类。这些部件包括但不限于按钮QPushButton、标签QLabel、文本框QLineEdit、布局管理器如QVBoxLayout、QHBoxLayout等是创建交互式 GUI 应用程序的基础构建块。
①QApplication
QApplication是整个应用程序的核心管理类。它负责管理应用程序的生命周期包括初始化、事件循环处理、资源管理等诸多关键事务。一个 PyQt 应用程序通常需要创建一个QApplication对象作为起点来启动整个应用程序的运行。 import sysfrom PyQt6.QtWidgets import QApplicationapp QApplication(sys.argv)# 在这里可以通过sys.argv来获取命令行参数并根据参数进行应用程序的初始化设置
②QMainWindow
在 PyQt 中QMainWindow是一个用于创建主窗口类型应用程序的关键类。它提供了一个完整的框架来构建包含菜单栏、工具栏、状态栏和中心部件等典型布局的主窗口应用程序为复杂的图形用户界面GUI应用程序提供了一种标准的组织结构。 from PyQt6.QtWidgets import QMainWindow, QApplicationimport sysapp QApplication(sys.argv)main_window QMainWindow()main_window.show()sys.exit(app.exec())
四、PyQt时间事件处理
①QTimer
在 PyQt 中QTimer是一个用于实现定时器功能的类。它允许你在指定的时间间隔后触发一个信号这个信号可以连接到一个槽函数从而实现周期性的操作或者在延迟一段时间后执行某个操作。这在很多场景下非常有用比如实现动画效果、定期更新数据显示、自动保存功能等。
例创建用于检查闹钟的定时器
self.timer QTimer(self)
self.timer.timeout.connect(self.check_alarm)
self.alarm_time None # 初始化闹钟时间
例创建滚动动画的定时器
self.scroll_timer QTimer(self)
self.scroll_timer.timeout.connect(self.scroll_footer_label)
②QTime
在 PyQt 中QTime类主要用于表示时间。它提供了一种方便的方式来处理时间相关的操作如获取当前时间、设置特定时间、比较时间大小、进行时间的加减运算等。这在很多需要涉及时间处理的应用场景中非常有用例如日程安排应用、闹钟应用、时间记录工具等。
例设置初始时间显示
self.ui.timeLabel.setText(QTime.currentTime().toString(HH:mm:ss))
例 判断用户输入的时间格式
self.alarm_time QTime.fromString(input_time, HH:mm:ss)
if not self.alarm_time.isValid():self.alarm_time QTime.fromString(input_time, H:m:s)
if not self.alarm_time.isValid():self.alarm_time QTime.fromString(input_time, HHmmss)
if not self.alarm_time.isValid():self.alarm_time QTime.fromString(input_time, Hms)
if not self.alarm_time.isValid():self.ui.Alarm_display.setText(时间格式无效)return
五、PyQt事件机制思维导图 PyQt事件机制思维导图 六、完整代码
import sys
from PySide6.QtWidgets import QApplication, QMainWindow, QLabel
from PySide6.QtCore import QTimer, QTime, Qt
from PySide6.QtGui import QPainter, QLinearGradient, QColor, QFont, QMouseEvent, QIcon
from PySide6.QtMultimedia import QMediaPlayer, QAudioOutput
from Alarm_clock import Ui_MainWindowclass GradientTextLabel(QLabel):def __init__(self, text, parentNone):super().__init__(text, parent)self.setFont(QFont(Arial, 40, QFont.Weight.Bold))self.setStyleSheet(background-color: transparent;) # 背景透明def paintEvent(self, event):painter QPainter(self)painter.setRenderHint(QPainter.Antialiasing)# 创建一个线性渐变红到紫带透明度gradient QLinearGradient(0, 0, self.width(), 0)gradient.setColorAt(0.0, QColor(100, 0, 200, 200)) # 紫色透明度78%gradient.setColorAt(1.0, QColor(255, 0, 0, 200)) # 红色透明度78%# 设置文字渐变pen painter.pen()pen.setBrush(gradient) # 使用渐变作为笔刷pen.setWidth(2) # 设置笔宽度painter.setPen(pen)# 绘制文字painter.drawText(self.rect(), Qt.AlignCenter, self.text())painter.end()class AlarmClock(QMainWindow):def __init__(self):super().__init__()# 初始化 UIself.ui Ui_MainWindow()self.ui.setupUi(self)# 设置应用图标self.setWindowIcon(QIcon(G:\longz\人工智能作业\PythonQt\闹钟界面/蓝色风信子_爱给网_aigei_com.ico))# 开始隐藏footerLabelself.ui.footerLabel.hide()# 设置时间标签为渐变文字标签self.gradient_label GradientTextLabel(parentself)self.gradient_label.setGeometry(self.ui.timeLabel.geometry()) # 保持位置一致self.ui.timeLabel.deleteLater() # 删除原来的 timeLabelself.ui.timeLabel self.gradient_label # 替换为渐变标签# 设置初始时间self.ui.timeLabel.setText(QTime.currentTime().toString(HH:mm:ss))# 固定窗口大小为 800x600self.setFixedSize(800, 600)# 启动时间更新定时器始终更新self.time_timer QTimer(self)self.time_timer.timeout.connect(self.update_time_display)self.time_timer.start(1000) # 每秒更新一次# 纯净窗口self.setWindowFlag(Qt.WindowType.FramelessWindowHint)# 定时器self.timer QTimer(self)self.timer.timeout.connect(self.check_alarm)self.alarm_time None# 滚动动画self.scroll_timer QTimer(self)self.scroll_timer.timeout.connect(self.scroll_footer_label)self.footer_label_start_pos self.ui.footerLabel.geometry().x() # 起始位置self.footer_label_end_pos -self.ui.footerLabel.width() 800 # 结束位置# 音乐播放器self.media_player QMediaPlayer(self)self.audio_output QAudioOutput(self)self.media_player.setAudioOutput(self.audio_output)self.media_player.setSource(G:/longz/人工智能作业/PythonQt/闹钟界面/Scott Lloyd Shelly - Terraria Soundtrack - 01 Overworld Day.mp3)# 连接按钮点击事件self.ui.pushButton.clicked.connect(self.handle_button_click)# 处理按钮点击事件def handle_button_click(self):if self.ui.pushButton.text() 启动:self.start_alarm_timer()elif self.ui.pushButton.text() 停止:self.stop_alarm()# 停止闹钟def stop_alarm(self):# 停止定时器、滚动动画和音乐播放self.timer.stop()self.scroll_timer.stop()self.media_player.stop()self.ui.footerLabel.hide()# 重置按钮和状态显示self.ui.Alarm_display.setText(闹钟已停止)self.ui.pushButton.setText(启动)# 启动闹钟def start_alarm_timer(self):input_time self.ui.Alarm_input.text()if not input_time:self.ui.Alarm_display.setText(请输入时间)return# 支持多种时间格式self.alarm_time QTime.fromString(input_time, HH:mm:ss)if not self.alarm_time.isValid():self.alarm_time QTime.fromString(input_time, H:m:s)if not self.alarm_time.isValid():self.alarm_time QTime.fromString(input_time, HHmmss)if not self.alarm_time.isValid():self.alarm_time QTime.fromString(input_time, Hms)if not self.alarm_time.isValid():self.ui.Alarm_display.setText(时间格式无效)return# 格式化时间并显示formatted_time self.alarm_time.toString(HH:mm:ss)self.ui.Alarm_input.setText(formatted_time)# 启动定时器self.timer.start(1000)self.ui.Alarm_display.setText(闹钟已启动)self.ui.pushButton.setText(停止)# 更新时间显示def update_time_display(self):current_time QTime.currentTime()self.ui.timeLabel.setText(current_time.toString(HH:mm:ss))# 检查闹钟是否到达时间def check_alarm(self):current_time QTime.currentTime()if self.alarm_time and current_time self.alarm_time and current_time self.alarm_time.addSecs(600):self.ui.Alarm_display.setText(闹钟响了)self.ui.footerLabel.show()if not self.scroll_timer.isActive():self.scroll_timer.start(30) # 滚动动画if self.media_player.mediaStatus() ! QMediaPlayer.MediaStatus.LoadingMedia:self.media_player.play()else:# 停止定时器、滚动动画和音乐播放self.scroll_timer.stop()self.media_player.stop()self.ui.footerLabel.hide()# 状态显示self.ui.Alarm_display.setText(闹钟已结束)# 滚动 footerLabeldef scroll_footer_label(self):current_geometry self.ui.footerLabel.geometry()x current_geometry.x() - 5 # 每次向左移动 5 像素# 如果超出左边界将其重新设置到右边界if x self.footer_label_end_pos:x self.footer_label_start_posself.ui.footerLabel.setGeometry(x, current_geometry.y(), current_geometry.width(), current_geometry.height())def mousePressEvent(self, event: QMouseEvent):self.px event.pos().x()self.py event.pos().y()def mouseMoveEvent(self, event: QMouseEvent):if event.buttons() Qt.MouseButton.LeftButton:self.move(int(event.globalPosition().x()) - self.px, int(event.globalPosition().y()) - self.py)if __name__ __main__:app QApplication(sys.argv)window AlarmClock()window.setWindowFlag(Qt.WindowType.FramelessWindowHint)window.setAttribute(Qt.WidgetAttribute.WA_TranslucentBackground)window.show()sys.exit(app.exec())