71 lines
2.1 KiB
Python
71 lines
2.1 KiB
Python
import random
|
|
|
|
from PySide6.QtCore import QTimer, Qt, Signal
|
|
from PySide6.QtWidgets import QWidget, QVBoxLayout, QFrame
|
|
from qfluentwidgets import DisplayLabel, LargeTitleLabel
|
|
|
|
from module.picker.schema import PickerStudent
|
|
|
|
|
|
class Widget(QFrame):
|
|
|
|
def __init__(self, key: str, parent=None):
|
|
super().__init__(parent=parent)
|
|
# 必须给子界面设置全局唯一的对象名
|
|
self.setObjectName(key.replace(' ', '-'))
|
|
|
|
|
|
class RollingTextWidget(QWidget):
|
|
finishSignal = Signal()
|
|
|
|
def __init__(self, parent=None):
|
|
super().__init__(parent)
|
|
self.current_index = 0
|
|
self.items = []
|
|
|
|
self.soLabel = LargeTitleLabel("", self)
|
|
self.nameLabel = DisplayLabel("", self)
|
|
self.soLabel.setAlignment(Qt.AlignCenter)
|
|
self.nameLabel.setAlignment(Qt.AlignCenter)
|
|
|
|
self.layout = QVBoxLayout()
|
|
self.layout.addWidget(self.soLabel)
|
|
self.layout.addWidget(self.nameLabel)
|
|
self.setLayout(self.layout)
|
|
|
|
self.rolling_timer = QTimer(self)
|
|
self.rolling_timer.setInterval(50) # 滚动速度(毫秒)
|
|
self.rolling_timer.timeout.connect(self.update_text)
|
|
|
|
self.stop_timer = QTimer(self)
|
|
self.stop_timer.setSingleShot(True)
|
|
self.stop_timer.timeout.connect(self.stop_rolling)
|
|
|
|
def update_text(self):
|
|
# 每次显示下一个字符
|
|
self.current_index = (self.current_index + 1) % len(self.items)
|
|
stu = self.items[self.current_index]
|
|
self.soLabel.setText(stu.so)
|
|
self.nameLabel.setText(stu.name)
|
|
|
|
def start_rolling(self):
|
|
if not self.rolling_timer.isActive():
|
|
self.rolling_timer.start()
|
|
self.stop_timer.start(2000) # 2秒后停止滚动
|
|
|
|
def stop_rolling(self):
|
|
self.rolling_timer.stop()
|
|
self.finishSignal.emit()
|
|
|
|
def set_items(self, items: list[PickerStudent]):
|
|
self.items = items[:]
|
|
random.shuffle(self.items)
|
|
|
|
def show_result(self, student: PickerStudent):
|
|
self.soLabel.setText(student.so)
|
|
self.nameLabel.setText(student.name)
|
|
|
|
def clear_text(self):
|
|
self.soLabel.clear()
|
|
self.nameLabel.clear()
|