本帖最后由 bjmtr 于 2026-5-3 09:02 编辑
[Python] 纯文本查看 复制代码 import sys
import time
import winsound
from PyQt5.QtWidgets import (QApplication, QMainWindow, QVBoxLayout, QHBoxLayout,
QPushButton, QLabel, QSpinBox, QWidget, QMessageBox, QComboBox)
from PyQt5.QtCore import QTimer, Qt
class SimpleTimer(QMainWindow):
def __init__(self):
super().__init__()
self.presets = [1, 5, 10, 15, 30] # 预设时间(分钟)
self.init_ui()
self.is_running = False
self.is_paused = False
self.remaining_seconds = 0
def init_ui(self):
self.setWindowTitle("⏰ 智能计时器")
self.setGeometry(100, 100, 400, 350)
central_widget = QWidget()
self.setCentralWidget(central_widget)
layout = QVBoxLayout(central_widget)
# 时间显示
self.time_label = QLabel("00:00")
self.time_label.setStyleSheet("font-size: 40px; font-weight: bold; color: #2E8B57; padding: 20px;")
self.time_label.setAlignment(Qt.AlignCenter)
layout.addWidget(self.time_label)
# 预设时间选择
preset_layout = QHBoxLayout()
preset_layout.addWidget(QLabel("预设时间:"))
self.preset_combo = QComboBox()
for preset in self.presets:
self.preset_combo.addItem(f"{preset} 分钟")
self.preset_combo.currentTextChanged.connect(self.preset_selected)
preset_layout.addWidget(self.preset_combo)
layout.addLayout(preset_layout)
# 自定义时间设置
time_layout = QHBoxLayout()
time_layout.addWidget(QLabel("自定义分钟:"))
self.minutes_spin = QSpinBox()
self.minutes_spin.setRange(1, 2147483647)
self.minutes_spin.setValue(5)
self.minutes_spin.valueChanged.connect(self.custom_time_changed)
time_layout.addWidget(self.minutes_spin)
layout.addLayout(time_layout)
# 按钮布局
button_layout = QHBoxLayout()
self.start_btn = QPushButton("▶️ 开始")
self.start_btn.setStyleSheet("background-color: #4CAF50; color: white; font-size: 14px; padding: 10px;")
self.start_btn.clicked.connect(self.start_timer)
button_layout.addWidget(self.start_btn)
self.pause_btn = QPushButton("⏸️ 暂停")
self.pause_btn.setStyleSheet("background-color: #FFA500; color: white; font-size: 14px; padding: 10px;")
self.pause_btn.clicked.connect(self.pause_timer)
self.pause_btn.setEnabled(False)
button_layout.addWidget(self.pause_btn)
self.stop_btn = QPushButton("⏹️ 停止")
self.stop_btn.setStyleSheet("background-color: #f44336; color: white; font-size: 14px; padding: 10px;")
self.stop_btn.clicked.connect(self.stop_timer)
self.stop_btn.setEnabled(False)
button_layout.addWidget(self.stop_btn)
layout.addLayout(button_layout)
# 添加预设按钮
add_preset_layout = QHBoxLayout()
self.add_preset_btn = QPushButton("➕ 添加当前时间为预设")
self.add_preset_btn.setStyleSheet("background-color: #2196F3; color: white; font-size: 12px; padding: 8px;")
self.add_preset_btn.clicked.connect(self.add_preset)
add_preset_layout.addWidget(self.add_preset_btn)
layout.addLayout(add_preset_layout)
# 定时器
self.timer = QTimer()
self.timer.timeout.connect(self.update_timer)
def preset_selected(self, text):
if text: # 检查是否为空
minutes = int(text.split()[0])
self.minutes_spin.setValue(minutes)
def custom_time_changed(self):
"""自定义时间改变"""
self.preset_combo.setCurrentIndex(-1) # 清除预设选择
def add_preset(self):
"""添加预设时间"""
minutes = self.minutes_spin.value()
if minutes not in self.presets:
self.presets.append(minutes)
self.presets.sort()
self.preset_combo.clear()
for preset in self.presets:
self.preset_combo.addItem(f"{preset} 分钟")
QMessageBox.information(self, "成功", f"已添加 {minutes} 分钟预设")
def start_timer(self):
"""开始计时"""
if not self.is_running:
minutes = self.minutes_spin.value()
self.remaining_seconds = minutes * 60
self.is_running = True
self.is_paused = False
self.update_button_states()
self.timer.start(1000)
self.update_display()
def pause_timer(self):
"""暂停/继续计时"""
if self.is_running:
self.is_paused = not self.is_paused
if self.is_paused:
self.pause_btn.setText("▶️ 继续")
self.pause_btn.setStyleSheet("background-color: #4CAF50; color: white; font-size: 14px; padding: 10px;")
else:
self.pause_btn.setText("⏸️ 暂停")
self.pause_btn.setStyleSheet("background-color: #FFA500; color: white; font-size: 14px; padding: 10px;")
self.update_button_states()
def stop_timer(self):
"""停止计时"""
self.is_running = False
self.is_paused = False
self.timer.stop()
self.reset_ui()
def update_timer(self):
"""更新计时器"""
if self.is_running and not self.is_paused:
if self.remaining_seconds > 0:
self.remaining_seconds -= 1
self.update_display()
else:
self.timer_finished()
def update_display(self):
"""更新显示"""
mins = self.remaining_seconds // 60
secs = self.remaining_seconds % 60
self.time_label.setText(f"{mins:02d}:{secs:02d}")
# 颜色变化提醒
if self.remaining_seconds <= 10:
self.time_label.setStyleSheet("font-size: 40px; font-weight: bold; color: #FF4444; padding: 20px;")
elif self.remaining_seconds <= 30:
self.time_label.setStyleSheet("font-size: 40px; font-weight: bold; color: #FFA500; padding: 20px;")
else:
self.time_label.setStyleSheet("font-size: 40px; font-weight: bold; color: #2E8B57; padding: 20px;")
def update_button_states(self):
"""更新按钮状态"""
self.start_btn.setEnabled(not self.is_running)
self.pause_btn.setEnabled(self.is_running)
self.stop_btn.setEnabled(self.is_running)
self.minutes_spin.setEnabled(not self.is_running)
self.preset_combo.setEnabled(not self.is_running)
self.add_preset_btn.setEnabled(not self.is_running)
def timer_finished(self):
"""计时完成"""
self.stop_timer()
QMessageBox.information(self, "时间到!", "⏰ 计时结束!")
# 播放提示音
for _ in range(3):
winsound.Beep(523, 300)
winsound.Beep(392, 300)
time.sleep(0.5)
def reset_ui(self):
"""重置界面"""
self.time_label.setText("00:00")
self.time_label.setStyleSheet("font-size: 40px; font-weight: bold; color: #2E8B57; padding: 20px;")
self.pause_btn.setText("⏸️ 暂停")
self.pause_btn.setStyleSheet("background-color: #FFA500; color: white; font-size: 14px; padding: 10px;")
self.update_button_states()
def main():
app = QApplication.instance()
if app is None:
app = QApplication(sys.argv)
window = SimpleTimer()
window.show()
window.raise_()
window.activateWindow()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
|