PyQt5 绘图

PyQt5 绘图_第1张图片

import sys

from PyQt5.QtCore import Qt, QRect
from PyQt5.QtGui import (QPen, QPainter, QBrush, QColor,QLinearGradient)
from PyQt5.QtWidgets import QWidget, QApplication

from Painter import Ui_Painter


class MyPainter(QWidget):
    def __init__(self,parent=None):
        super().__init__(parent)

        self.ui = Ui_Painter()
        self.ui.setupUi(self)

    def paintEvent(self, event):
        painter = QPainter(self)
        painter.setRenderHint(QPainter.Antialiasing)
        pen = QPen()
        pen.setColor(Qt.red)
        pen.setStyle(Qt.SolidLine)
        pen.setWidth(3)
        painter.setPen(pen)

        brush = QBrush()
        brush.setColor(QColor(255,255,0))
        brush.setStyle(Qt.SolidPattern)
        painter.setBrush(brush)
        painter.drawRect(10,10,100,100)

        self.drawGradient(painter)

    def drawGradient(self,painter):
        pen = QPen()
        pen.setStyle(Qt.NoPen)
        painter.setPen(pen)
        w = self.width()
        h = self.height()
        rect = QRect(w/4,h/4,w/2,h/2)
        linearGrad = QLinearGradient(rect.left(),rect.top(),rect.right(),rect.top())
        linearGrad.setColorAt(0,Qt.blue)
        linearGrad.setColorAt(0.5,Qt.yellow)
        linearGrad.setColorAt(1,Qt.green)
        painter.setBrush(linearGrad)
        painter.drawRect(rect);

if __name__ == '__main__':
    app = QApplication(sys.argv)
    wgt = MyPainter()
    wgt.show()
    sys.exit(app.exec_())

aaa

你可能感兴趣的:(PyQt)