示例代码(PyQt5):from PyQt5.QtWidgets import QApplication, QWidget, QMessageBox, QPushButton, QVBoxLayout
from PyQt5.QtCore import QTimer
import sys
import winsound
classReminderApp(QWidget):
def__init__(self):
super().__init__()
self.initUI()
definitUI(self):
self.setWindowTitle("PyQt5定时提醒")
self.setGeometry(100, 100, 300, 200)
# 创建定时器
self.timer = QTimer(self)
self.timer.timeout.connect(self.show_reminder) # 定时触发的函数
# 按钮
btn = QPushButton("设置5秒后提醒", self)
btn.clicked.connect(lambda: self.timer.start(5000)) # 5000毫秒=5秒
# 布局
layout = QVBoxLayout()
layout.addWidget(btn)
self.setLayout(layout)
defshow_reminder(self):
self.timer.stop() # 停止定时器
winsound.Beep(1000, 1000)
QMessageBox.information(self, "提醒", "时间到!该休息了~")
if __name__ == "__main__":
app = QApplication(sys.argv)
window = ReminderApp()
window.show()
sys.exit(app.exec_())