倒计数+滚动字幕 Qt版
使用PyQt5重新设计的完整代码,实现了与网页版相同的功能。AI生成,很神奇?
下载链接:https://download.youkuaiyun.com/download/carcar2004/91181085
import sys
from PyQt5.QtWidgets import (QApplication, QMainWindow, QWidget, QVBoxLayout, QHBoxLayout,
QLineEdit, QPushButton, QLabel)
from PyQt5.QtCore import Qt, QTimer, QPropertyAnimation, QRect
from PyQt5.QtGui import QFont, QColor, QPalette
from PyQt5.QtMultimedia import QSound
class CountdownApp(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle("全屏文字滚动带倒计时")
self.setStyleSheet("background-color: black;")
# 主窗口设置
self.showFullScreen()
self.setCursor(Qt.BlankCursor)
# 初始化UI
self.init_ui()
# 初始化变量
self.animation_timer = QTimer()
self.countdown_timer = QTimer()
self.flash_timer = QTimer()
self.remaining_seconds = 0
self.position = 0
self.speed = 2
self.is_playing = False
self.flash_count = 0
# 加载声音
self.bell_sound = QSound("alarm.wav") # 需要准备一个alarm.wav文件
# 连接信号槽
self.start_btn.clicked.connect(self.start_scrolling)
self.text_input.returnPressed.connect(self.start_scrolling)
self.animation_timer.timeout.connect(self.animate_text)
self.countdown_timer.timeout.connect(self.update_countdown)
self.flash_timer.timeout.connect(self.flash_background)
def init_ui(self):
# 主控件
self.central_widget = QWidget()
self.setCentralWidget(self.central_widget)
# 主布局
self.main_layout = QVBoxLayout(self.central_widget)
self.main_layout.setContentsMargins(0, 0, 0, 0)
self.main_layout.setSpacing(0)
# 控制面板
self.control_panel = QWidget()
self.control_panel.setStyleSheet("background-color: rgba(0, 0, 0, 200);")
self.control_layout = QVBoxLayout(self.control_panel)
self.control_layout.setContentsMargins(15, 15, 15, 15)
self.control_layout.setSpacing(10)
# 文本输入行
self.text_input = QLineEdit()
self.text_input.setPlaceholderText("请输入要滚动的文字")
self.text_input.setStyleSheet("""
QLineEdit {
background-color: #222;
color: white;
border: 1px solid #444;
border-radius: 25px;
padding: 12px 15px;
font-size: 16px;
}
""")
# 倒计时输入行
self.timer_layout = QHBoxLayout()
self.timer_input = QLineEdit()
self.timer_input.setPlaceholderText("倒计时秒数")
self.timer_input.setText("10")
self.timer_input.setValidator(QtGui.QIntValidator(1, 999))
self.timer_input.setStyleSheet("""
QLineEdit {
background-color: #222;
color: white;
border: 1px solid #444;
border-radius: 25px;
padding: 12px 15px;
font-size: 16px;
}
""")
self.start_btn = QPushButton("开始")
self.start_btn.setStyleSheet("""
QPushButton {
background-color: #4CAF50;
color: white;
border: none;
border-radius: 25px;
padding: 0 20px;
min-width: 80px;
font-size: 16px;
}
QPushButton:hover {
background-color: #45a049;
}
""")
self.timer_layout.addWidget(self.timer_input)
self.timer_layout.addWidget(self.start_btn)
self.control_layout.addWidget(self.text_input)
self.control_layout.addLayout(self.timer_layout)
# 滚动文本区域
self.scrolling_area = QWidget()
self.scrolling_area.setStyleSheet("background-color: transparent;")
# 倒计时显示
self.countdown_label = QLabel()
self.countdown_label.setAlignment(Qt.AlignCenter)
self.countdown_label.setStyleSheet("""
QLabel {
color: #55ff55;
font-size: 100px;
text-shadow: 0 0 10px #55ff55;
}
""")
self.countdown_label.hide()
# 滚动文本
self.scrolling_text = QLabel()
self.scrolling_text.setAlignment(Qt.AlignCenter)
self.scrolling_text.setStyleSheet("""
QLabel {
color: white;
text-shadow: 0 0 5px rgba(255, 255, 255, 0.7);
}
""")
# 设置布局
self.scrolling_layout = QVBoxLayout(self.scrolling_area)
self.scrolling_layout.addWidget(self.countdown_label, 0, Qt.AlignCenter)
self.scrolling_layout.addWidget(self.scrolling_text, 0, Qt.AlignCenter)
self.main_layout.addWidget(self.control_panel)
self.main_layout.addWidget(self.scrolling_area, 1)
# 控制面板动画
self.panel_animation = QPropertyAnimation(self.control_panel, b"geometry")
self.panel_animation.setDuration(300)
# 设置字体
font = QFont("Microsoft YaHei", 15)
self.scrolling_text.setFont(font)
def start_scrolling(self):
text = self.text_input.text().strip()
if not text:
return
self.remaining_seconds = int(self.timer_input.text())
# 停止之前的动画
self.stop_all_animations()
# 设置滚动文本
self.scrolling_text.setText(text)
self.update_font_size()
# 显示倒计时
self.countdown_label.setText(str(self.remaining_seconds))
self.update_countdown_color()
self.countdown_label.show()
# 开始动画
self.position = self.width()
self.is_playing = True
# 开始倒计时
self.countdown_timer.start(1000)
# 开始文字滚动
self.animation_timer.start(16) # 约60fps
# 隐藏控制面板
self.hide_control_panel()
def update_countdown(self):
self.remaining_seconds -= 1
self.countdown_label.setText(str(self.remaining_seconds))
self.update_countdown_color()
if self.remaining_seconds <= 0:
self.countdown_timer.stop()
self.on_countdown_complete()
def update_countdown_color(self):
if self.remaining_seconds > 5:
self.countdown_label.setStyleSheet("""
QLabel {
color: #55ff55;
text-shadow: 0 0 10px #55ff55;
}
""")
else:
if self.remaining_seconds % 2 == 0:
self.countdown_label.setStyleSheet("""
QLabel {
color: #ff5555;
text-shadow: 0 0 10px #ff5555;
}
""")
else:
self.countdown_label.setStyleSheet("""
QLabel {
color: #ffff55;
text-shadow: 0 0 10px #ffff55;
}
""")
def on_countdown_complete(self):
# 播放铃声
self.bell_sound.play()
# 开始背景闪烁
self.flash_count = 0
self.flash_timer.start(500) # 每500ms闪烁一次
# 3秒后停止所有效果并显示控制面板
QTimer.singleShot(3000, self.end_flashing)
def flash_background(self):
self.flash_count += 1
if self.flash_count <= 6: # 闪烁3秒(6次)
if self.flash_count % 2 == 1:
self.setStyleSheet("background-color: #444;")
else:
self.setStyleSheet("background-color: black;")
else:
self.flash_timer.stop()
self.setStyleSheet("background-color: black;")
def end_flashing(self):
self.flash_timer.stop()
self.setStyleSheet("background-color: black;")
self.countdown_label.hide()
self.show_control_panel()
def animate_text(self):
self.position -= self.speed
text_width = self.scrolling_text.fontMetrics().width(self.scrolling_text.text())
# 如果文字完全滚出屏幕,重置位置
if self.position < -text_width:
self.position = self.width()
# 更新文字位置
self.scrolling_text.move(self.position,
(self.height() - self.scrolling_text.height()) // 2)
def update_font_size(self):
# 根据屏幕方向调整字体大小
if self.height() > self.width(): # 竖屏
font_size = min(self.width(), self.height()) * 0.15
else: # 横屏
font_size = min(self.width(), self.height()) * 0.15
font = self.scrolling_text.font()
font.setPointSize(int(font_size))
self.scrolling_text.setFont(font)
def hide_control_panel(self):
self.panel_animation.setStartValue(self.control_panel.geometry())
self.panel_animation.setEndValue(QRect(0, -self.control_panel.height(),
self.width(), self.control_panel.height()))
self.panel_animation.start()
def show_control_panel(self):
self.panel_animation.setStartValue(self.control_panel.geometry())
self.panel_animation.setEndValue(QRect(0, 0, self.width(), self.control_panel.height()))
self.panel_animation.start()
def stop_all_animations(self):
self.animation_timer.stop()
self.countdown_timer.stop()
self.flash_timer.stop()
self.is_playing = False
self.setStyleSheet("background-color: black;")
self.countdown_label.hide()
def mousePressEvent(self, event):
# 点击屏幕切换控制面板显示
if self.control_panel.y() < 0: # 面板隐藏
self.show_control_panel()
else: # 面板显示
self.hide_control_panel()
def resizeEvent(self, event):
# 窗口大小改变时更新字体大小和文字位置
if hasattr(self, 'scrolling_text') and self.is_playing:
self.update_font_size()
self.scrolling_text.move(self.position,
(self.height() - self.scrolling_text.height()) // 2)
if __name__ == "__main__":
app = QApplication(sys.argv)
window = CountdownApp()
sys.exit(app.exec_())
功能说明:
- 全屏显示:应用启动后自动全屏显示
- 倒计时功能:
- 大于5秒时显示绿色
- 小于等于5秒时红黄交替闪烁
- 文字滚动:输入的文字会从右向左平滑滚动
- 背景闪烁:倒计时结束后背景闪烁3秒
- 声音提示:倒计时结束时播放铃声
- 控制面板:
- 点击开始后自动隐藏
- 倒计时结束3秒后自动显示
- 点击屏幕任意位置可切换显示/隐藏
使用说明:
- 在文本框中输入要滚动的文字
- 设置倒计时秒数(默认为10秒)
- 点击"开始"按钮或按Enter键
- 控制面板会自动隐藏,开始倒计时和文字滚动
- 倒计时结束后会播放铃声并闪烁背景
- 3秒后控制面板自动显示
- 点击屏幕任意位置可手动显示/隐藏控制面板
注意事项:
- 需要准备一个名为"alarm.wav"的铃声文件放在同一目录下
- 使用了PyQt5库,需要先安装:
“pip install PyQt5” - 代码中已处理横竖屏切换时的布局调整
这个PyQt5版本实现了与网页版相同的所有功能,并针对桌面环境进行了优化,具有更好的性能和更流畅的动画效果。