使用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_())
相关推荐
典典分享指南25 分钟前
用飞书 Wiki 搭内容作战室:从零搭建团队内容协作中枢
django·flask·numpy·pyqt
千里码aicood1 天前
pyqt-基于Qt的公共卫生间管理系统的设计与实现
pyqt
千里码aicood2 天前
pyqt基于pytorch的人脸交换系统设计(opencv)
人工智能·pyqt
楚莫识5 天前
Qt show()、raise()、activateWindow() 调用顺序区别
qt·pyqt
小灰灰搞电子9 天前
PyQt 实现蜂巢菜单源码分享
pyqt·鱼眼凸起
znnnk9 天前
【Python】GUI 开发从入门到实战(三):PyQt/PySide 进阶之路
开发语言·python·pyqt
民乐团扒谱机14 天前
【超详细】PyQt6 实现 AU 风格波形图编辑器:多级缩放与采样点逐级渲染,附全过程代码
开发语言·python·编辑器·pyqt·audition·时域·频谱图
典典分享指南15 天前
Excel SUMIFS 跨表汇总实战:多表条件求和的完整指南
django·flask·numpy·pyqt·fastapi
狮子雨恋18 天前
【无标题】
pyqt
微小冷20 天前
pyqt+matplotlib多进程图像刷新
pyqt·matplotlib·进程通信·多进程·queue·process