Logo
Section 1 / 5

QPainter 类

了解我们的GUI应用程序如何使用QPainter类。

导入

QPainter 类

了解我们的GUI应用程序如何使用QPainter类。

1import sys 2from PyQt6.QtWidgets import QApplication, QWidget 3from PyQt6.QtGui import (QPainter, QPainterPath, QColor, QBrush, QPen, 4 QFont, QPolygon, QLinearGradient) 5from PyQt6.QtCore import Qt, QPoint, QRect 6 7class Drawing(QWidget): 8 def __init__(self): 9 super().__init__() 10 self.initializeUI() 11 12 def initializeUI(self): 13 self.setFixedSize(600, 600) 14 self.setWindowTitle('QPainter Class') 15 # 创建几种笔的颜色 16 self.black = '#000000' 17 self.blue = '#2041F1' 18 self.green = '#12A708' 19 self.purple = '#6512F0' 20 self.red = '#E00C0C' 21 self.orange = '#FF930A' 22 self.show() 23 24 def paintEvent(self, event): 25 painter = QPainter() 26 painter.begin(self) 27 self.drawPoints(painter) 28 self.drawDiffLines(painter) 29 self.drawText(painter) 30 self.drawRectangles(painter) 31 self.drawPolygons(painter) 32 self.drawRoundedRects(painter) 33 self.drawCurves(painter) 34 self.drawCircles(painter) 35 self.drawGradients(painter) 36 painter.end() 37 38 def drawPoints(self, painter): 39 pen = QPen(QColor(self.black)) 40 for i in range(1, 9): 41 pen.setWidth(i * 2) 42 painter.setPen(pen) 43 painter.drawPoint(i * 20, i * 20) 44 45 def drawDiffLines(self, painter): 46 pen = QPen(QColor(self.black), 2) 47 painter.setPen(pen) 48 painter.drawLine(230, 20, 230, 180) 49 pen.setStyle(Qt.PenStyle.DashLine) 50 painter.setPen(pen) 51 painter.drawLine(260, 20, 260, 180) 52 pen.setStyle(Qt.PenStyle.DotLine) 53 painter.setPen(pen) 54 painter.drawLine(290, 20, 290, 180) 55 pen.setStyle(Qt.PenStyle.DashDotLine) 56 painter.setPen(pen) 57 painter.drawLine(320, 20, 320, 180) 58 # 更改笔的颜色和粗细 59 blue_pen = QPen(QColor(self.blue), 4) 60 painter.setPen(blue_pen) 61 painter.drawLine(350, 20, 350, 180) 62 blue_pen.setStyle(Qt.PenStyle.DashDotDotLine) 63 painter.setPen(blue_pen) 64 painter.drawLine(380, 20, 380, 180) 65 66 def drawText(self, painter): 67 text = "Don't look behind." 68 pen = QPen(QColor(self.red)) 69 painter.setFont(QFont("Helvetica", 15)) 70 painter.setPen(pen) 71 painter.drawText(420, 110, text) 72 73 def drawRectangles(self, painter): 74 pen = QPen(QColor(self.black)) 75 brush = QBrush(QColor(self.black)) 76 painter.setPen(pen) 77 painter.drawRect(20, 220, 80, 80) 78 painter.setPen(pen) 79 painter.setBrush(brush) 80 painter.drawRect(120, 220, 80, 80) 81 red_pen = QPen(QColor(self.red), 5) 82 green_brush = QBrush(QColor(self.green)) 83 painter.setPen(red_pen) 84 painter.setBrush(green_brush) 85 painter.drawRect(20, 320, 80, 80) 86 # 演示如何更改 alpha 通道以实现透明效果 87 blue_pen = QPen(QColor(32, 85, 230, 100), 5) 88 blue_pen.setStyle(Qt.PenStyle.DashLine) 89 painter.setPen(blue_pen) 90 painter.setBrush(green_brush) 91 painter.drawRect(120, 320, 80, 80) 92 93 def drawPolygons(self, painter): 94 pen = QPen(QColor(self.blue), 2) 95 brush = QBrush(QColor(self.orange)) 96 points = QPolygon([QPoint(240, 240), QPoint(380, 250), 97 QPoint(230, 380), QPoint(370, 360)]) 98 painter.setPen(pen) 99 painter.setBrush(brush) 100 painter.drawPolygon(points) 101 102 def drawRoundedRects(self, painter): 103 pen = QPen(QColor(self.black)) 104 brush = QBrush(QColor(self.black)) 105 rect_1 = QRect(420, 340, 40, 60) 106 rect_2 = QRect(480, 300, 50, 40) 107 rect_3 = QRect(540, 240, 40, 60) 108 painter.setPen(pen) 109 brush.setStyle(Qt.BrushStyle.Dense1Pattern) 110 painter.setBrush(brush) 111 painter.drawRoundedRect(rect_1, 8, 8) 112 brush.setStyle(Qt.BrushStyle.Dense5Pattern) 113 painter.setBrush(brush) 114 painter.drawRoundedRect(rect_2, 5, 20) 115 brush.setStyle(Qt.BrushStyle.BDiagPattern) 116 painter.setBrush(brush) 117 painter.drawRoundedRect(rect_3, 15, 15) 118 119 def drawCurves(self, painter): 120 pen = QPen(Qt.GlobalColor.black, 3) 121 brush = QBrush(Qt.GlobalColor.white) 122 path = QPainterPath() 123 path.moveTo(30, 420) 124 path.cubicTo(30, 420, 65, 500, 30, 560) 125 path.lineTo(163, 540) 126 path.cubicTo(125, 360, 110, 440, 30, 420) 127 path.closeSubpath() 128 painter.setPen(pen) 129 painter.setBrush(brush) 130 painter.drawPath(path) 131 132 def drawCircles(self, painter): 133 height, width = self.height(), self.width() 134 center_x, center_y = (width / 2), height - 100 135 radius_x, radius_y = 60, 60 136 pen = QPen(Qt.GlobalColor.black, 2, Qt.PenStyle.SolidLine) 137 brush = QBrush(Qt.GlobalColor.darkMagenta, Qt.BrushStyle.Dense5Pattern) 138 painter.setPen(pen) 139 painter.setBrush(brush) 140 painter.drawEllipse(QPoint(center_x, center_y), radius_x, radius_y) 141 142 def drawGradients(self, painter): 143 pen = QPen(QColor(self.black), 2) 144 gradient = QLinearGradient(450, 480, 520, 550) 145 gradient.setColorAt(0.0, Qt.GlobalColor.blue) 146 gradient.setColorAt(0.5, Qt.GlobalColor.yellow) 147 gradient.setColorAt(1.0, Qt.GlobalColor.cyan) 148 painter.setPen(pen) 149 painter.setBrush(QBrush(gradient)) 150 painter.drawRect(420, 420, 160, 160) 151 152if __name__ == '__main__': 153 app = QApplication(sys.argv) 154 window = Drawing() 155 sys.exit(app.exec())

美化我们的图形用户界面

创建一个绘图程序图形界面