from PyQt5.QtGui import QPainter, QBrush, QColor, QPainterPath from PyQt5.QtCore import Qt from PyQt5.QtWidgets import QWidget, QApplication class Example(QWidget): def __init__(self): super().__init__() self.initUI() def initUI(self): self.setGeometry(300, 300, 350, 350) self.setWindowTitle('Fill Path Example') self.show() def paintEvent(self, event): qp = QPainter() qp.begin(self) qp.setBrush(QBrush(QColor(0, 255, 0), Qt.SolidPattern)) path = QPainterPath() path.addEllipse(100, 100, 100, 100) qp.fillPath(path, QBrush(QColor(0, 255, 0), Qt.SolidPattern)) qp.end() if __name__ == '__main__': app = QApplication([]) ex = Example() app.exec_()
from PyQt5.QtGui import QPainter, QBrush, QColor, QPainterPath, QLinearGradient from PyQt5.QtCore import Qt from PyQt5.QtWidgets import QWidget, QApplication class Example(QWidget): def __init__(self): super().__init__() self.initUI() def initUI(self): self.setGeometry(300, 300, 350, 350) self.setWindowTitle('Fill Path Example') self.show() def paintEvent(self, event): qp = QPainter() qp.begin(self) path = QPainterPath() path.moveTo(100, 100) path.lineTo(200, 100) path.lineTo(150, 200) path.lineTo(100, 100) gradient = QLinearGradient(100, 100, 200, 200) gradient.setColorAt(0, Qt.red) gradient.setColorAt(0.5, Qt.green) gradient.setColorAt(1, Qt.blue) qp.setBrush(QBrush(gradient)) qp.fillPath(path, QBrush(gradient)) qp.end() if __name__ == '__main__': app = QApplication([]) ex = Example() app.exec_()Both of these examples use the PyQt5.QtGui package library for the QPainter, QBrush, QColor, QPainterPath, and QLinearGradient classes.