| 78 | return super(WidgetCode, self).timerEvent(event) |
| 79 | |
| 80 | def paintEvent(self, event): |
| 81 | painter = QPainter(self) |
| 82 | painter.setRenderHint(QPainter.Antialiasing) |
| 83 | # 背景白色 |
| 84 | painter.fillRect(event.rect(), QBrush(Qt.white)) |
| 85 | # 绘制边缘虚线框 |
| 86 | painter.setPen(Qt.DashLine) |
| 87 | painter.setBrush(Qt.NoBrush) |
| 88 | painter.drawRect(self.rect()) |
| 89 | # 随机画条线 |
| 90 | for _ in range(3): |
| 91 | painter.setPen(QPen(QTCOLORLIST[qrand() % 5], 1, Qt.SolidLine)) |
| 92 | painter.setBrush(Qt.NoBrush) |
| 93 | painter.drawLine(QPoint(0, qrand() % self.height()), |
| 94 | QPoint(self.width(), qrand() % self.height())) |
| 95 | painter.drawLine(QPoint(qrand() % self.width(), 0), |
| 96 | QPoint(qrand() % self.width(), self.height())) |
| 97 | # 绘制噪点 |
| 98 | painter.setPen(Qt.DotLine) |
| 99 | painter.setBrush(Qt.NoBrush) |
| 100 | for _ in range(self.width()): # 绘制噪点 |
| 101 | painter.drawPoint(QPointF(qrand() % self.width(), qrand() % self.height())) |
| 102 | # super(WidgetCode, self).paintEvent(event) # 绘制文字 |
| 103 | # 绘制跳动文字 |
| 104 | metrics = QFontMetrics(self.font()) |
| 105 | x = (self.width() - metrics.width(self.text())) / 2 |
| 106 | y = (self.height() + metrics.ascent() - metrics.descent()) / 2 |
| 107 | for i, ch in enumerate(self.text()): |
| 108 | index = (self.step + i) % 16 |
| 109 | painter.setPen(TCOLORLIST[qrand() % 6]) |
| 110 | painter.drawText(x, y - ((SINETABLE[index] * metrics.height()) / 400), ch) |
| 111 | x += metrics.width(ch) |
| 112 | |
| 113 | |
| 114 | if __name__ == "__main__": |