使用pyqt绘制一个爱心!

使用pyqt绘制一个爱心!

介绍

  • 使用pyqt绘制一个爱心!

效果

代码

python 复制代码
import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QWidget
from PyQt5.QtGui import QPainter, QPen, QBrush, QColor
from PyQt5.QtCore import Qt, QPointF
import numpy as np

class HeartWidget(QWidget):
    def __init__(self):
        super().__init__()
        self.setWindowTitle('Heart Shape with PyQt5')
        self.setGeometry(100, 100, 800, 600)

    def paintEvent(self, event):
        painter = QPainter(self)
        painter.setRenderHint(QPainter.Antialiasing)

        # Generate heart shape coordinates
        t = np.linspace(0, 2 * np.pi, 1000)
        x = 16 * np.sin(t)**3
        y = -(13 * np.cos(t) - 5 * np.cos(2 * t) - 2 * np.cos(3 * t) - np.cos(4 * t))

        # Normalize coordinates
        x = (x - min(x)) / (max(x) - min(x)) * self.width()
        y = (y - min(y)) / (max(y) - min(y)) * self.height()

        # Set gradient colors
        colors = [QColor.fromHsvF(i / len(t), 1.0, 1.0) for i in range(len(t))]

        # Draw heart shape
        for i in range(len(t) - 1):
            painter.setPen(QPen(colors[i], 2))
            painter.drawLine(QPointF(x[i], y[i]), QPointF(x[i + 1], y[i + 1]))

        # Fill heart shape with gradient colors
        painter.setPen(Qt.NoPen)
        painter.setBrush(QBrush(Qt.SolidPattern))
        for i in range(len(t) - 1):
            painter.setBrush(QBrush(colors[i]))
            painter.drawPolygon(QPointF(x[i], y[i]), QPointF(x[i + 1], y[i + 1]), QPointF(self.width() / 2, self.height() / 2))

class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()
        self.setWindowTitle('Heart Shape with PyQt5')
        self.setGeometry(100, 100, 800, 600)
        self.setCentralWidget(HeartWidget())

if __name__ == '__main__':
    app = QApplication(sys.argv)
    window = MainWindow()
    window.show()
    sys.exit(app.exec_())
相关推荐
开心-开心急了14 小时前
pyside6实现win10自动切换主题
开发语言·python·pyqt·pyside
梨轻巧5 天前
pyside6的历史发展、Qt 介绍、PyQt 和 pyside6对比
qt·pyqt
开心-开心急了7 天前
PySide6 实现win10 手动与自动切换主题 借助系统托盘
pyqt·1024程序员节·pyside
mortimer9 天前
用 PySide6 打造可视化 ASS 字幕样式编辑器:从需求到实现
python·ffmpeg·pyqt
没有感情的robot11 天前
pyqt实现简易中英文翻译工具
pyqt
mortimer14 天前
用PySide6 构建一个响应式视频剪辑工具:多线程与信号机制实战
python·ffmpeg·pyqt
深兰科技15 天前
深兰科技法务大模型亮相,推动律所文书处理智能化
人工智能·scrapy·beautifulsoup·scikit-learn·pyqt·fastapi·深兰科技
叶子丶苏17 天前
第八节_PySide6基本窗口控件_按钮类控件(QAbstractButton)
python·pyqt
龙腾AI白云18 天前
大模型-7种大模型微调方法 上
scrapy·scikit-learn·pyqt
开心-开心急了19 天前
PySide6 使用搜索引擎搜索 多类实现 更新1次
python·pyqt·pyside