def paintEvent(self, event): sineTable = [ 0, 38, 71, 92, 100, 92, 71, 38, 0, -38, -71, -92, -100, -92, -71, -38 ] metrics = QFontMetrics(self.font()) x = (self.width() - metrics.horizontalAdvance(self.text)) / 2 y = (self.height() + metrics.ascent() - metrics.descent()) / 2 color = QColor() painter = QPainter(self) for i in range(len(self.text)): index = (self.step + i) % 16 color.setHsv((15 - index) * 16, 255, 191) painter.setPen(color) painter.drawText(x, y - ((sineTable[index] * metrics.height()) / 400), str(self.text[i])) x += metrics.horizontalAdvance(self.text[i])
class scrollTextLabel(QLabel): deletesig = Signal() def __init__(self, text, Rect, scale, speed, line, color, bold, parent=None): super(scrollTextLabel, self).__init__(parent) if color == 'red': self.color = QColor(231, 0, 18, 255) # red elif color == 'white': self.color = QColor(255, 255, 246, 255) # white elif color == 'Grass': self.color = QColor(144, 195, 32, 255) # Grass elif color == 'Blue': self.color = QColor(0, 160, 234, 255) # Blue if bold: self.font = QFont("Helvetica", scale, QFont.Bold) # 20 25 30 粗體 else: self.font = QFont("Helvetica", scale) self.txt = text self.speed = speed # between 50 ~ 120 self.setAttribute(Qt.WA_TranslucentBackground) self.setWindowFlags( Qt.FramelessWindowHint | Qt.WindowStaysOnTopHint | Qt.WindowCloseButtonHint) # 隱藏 FramelessWindow self.metrics = QFontMetrics(self.font) self.setFixedWidth(self.metrics.width(self.txt) + 10) self.setFixedHeight(self.metrics.height() + 5) # self.move(Rect.x() + Rect.width() * 0.97, Rect.y() + 50 * line) self.setFocusPolicy(Qt.NoFocus) self.hide() self.anim = QPropertyAnimation(self, 'pos') self.anim.setDuration(self.speed * 100) self.anim.setStartValue(QPoint(Rect.x() + Rect.width() * 0.95, Rect.y() + 50 * line)) self.anim.setEndValue(QPoint(-self.width() + Rect.x(), Rect.y() + 50 * line)) self.anim.setEasingCurve(QEasingCurve.Linear) self.show() self.repaint() self.anim.start() self.anim.finished.connect(self.sendslot) def sendslot(self): self.hide() def paintEvent(self, event): painter = QPainter(self) painter.save() path = QPainterPath() painter.setFont(self.font) painter.setRenderHint(QPainter.Antialiasing) pen = QPen(QColor(0, 0, 0, 230)) pen_width = 3 pen.setWidth(pen_width) len = self.metrics.width(self.txt) w = self.width() px = (len - w) / 2 if px < 0: px = -px py = (self.height() - self.metrics.height()) / 2 + self.metrics.ascent() if py < 0: py = -py path.addText(px + 2, py + 2, self.font, self.txt) painter.strokePath(path, pen) painter.drawPath(path) painter.fillPath(path, QBrush(self.color)) painter.restore()