from PySide2.QtGui import QPainter, QPolygon, QPen, QColor from PySide2.QtCore import QPoint, Qt from PySide2.QtWidgets import QApplication, QWidget class MyWidget(QWidget): def paintEvent(self, event): painter = QPainter(self) painter.setRenderHint(QPainter.Antialiasing) pen = QPen() pen.setWidth(2) pen.setColor(Qt.red) painter.setPen(pen) points = [QPoint(10, 10), QPoint(50, 50), QPoint(10, 50)] poly = QPolygon(points) painter.drawPolygon(poly) app = QApplication([]) widget = MyWidget() widget.show() app.exec_()
from PySide2.QtGui import QPainter, QPolygon, QPen, QColor from PySide2.QtCore import QPoint, Qt from PySide2.QtWidgets import QApplication, QWidget class MyWidget(QWidget): def paintEvent(self, event): painter = QPainter(self) painter.setRenderHint(QPainter.Antialiasing) pen = QPen() pen.setWidth(2) pen.setColor(Qt.blue) painter.setPen(pen) points = [QPoint(50, 10), QPoint(20, 80), QPoint(90, 30), QPoint(10, 30), QPoint(80, 80)] poly = QPolygon(points) painter.drawPolygon(poly) app = QApplication([]) widget = MyWidget() widget.show() app.exec_()In this example, we defined the vertices of a five-pointed star using five QPoint objects and stored them in a Python list. We then created a QPolygon object with these points and used the drawPolygon() method to draw our star onto the canvas. The points are arranged such that every other point is an outer point and every other point is an inner point, creating the star shape. PySide2.QtGui is a package library for Python that provides a range of tools and methods for creating GUI applications. The drawPolygon() method is one of the many methods available in this library, and it is used to draw polygons on a canvas surface. The examples provided demonstrate how to draw both triangles and stars using this method.