赞
踩
好的,我会尽力给出一个 PyQt 的元旦快乐代码:
```python import random from PyQt5.QtGui import * from PyQt5.QtWidgets import * from PyQt5.QtCore import *
class NewYearGreeting(QWidget): def init(self): super().init()
- # 设置窗口标题和大小
- self.setWindowTitle('元旦快乐')
- self.setFixedSize(400, 300)
-
- # 创建一个文本标签显示 "元旦快乐"
- self.label = QLabel('元旦快乐', self)
- self.label.setStyleSheet('font-size: 36pt; font-weight: bold; color: red')
- self.label.move(130, 50)
-
- # 创建一个按钮,点击后会出现烟花效果
- self.button = QPushButton('烟花', self)
- self.button.move(150, 200)
- self.button.clicked.connect(self.showFireworks)
-
- # 用来存储烟花的列表
- self.fireworks = []
-
- # 设置计时器,每 10 毫秒更新一次烟花
- self.timer = QTimer(self)
- self.timer.timeout.connect(self.updateFireworks)
- self.timer.start(10)
-
- def showFireworks(self):
- # 在窗口的随机位置添加一个烟花
- x = random.randint(0, 400)
- y = random.randint(0, 300)
- self.fireworks.append((x, y))
-
- def updateFireworks(self):
- # 更新所有烟花的位置
- for i in range(len(self.fireworks)):
- x, y = self.fireworks[i]
- y -= 1
- self.fireworks[i] = (x, y)
-
- # 删除已经飞出窗口的烟花
- self.fireworks = [(x, y) for x, y in self.fireworks if y >= 0]
-
- # 重新绘制窗口
- self.update()
-
- def paintEvent(self, event):
- # 绘制烟花
- painter = QPainter(self)
- painter.setRenderHint(QPainter.Antialiasing)
- for
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。