
- PyQt - Home
- PyQt - Introduction
- PyQt - Environment
- PyQt - Hello World
- PyQt - Major Classes
- PyQt - Using Qt Designer
- PyQt - Meta Objects
- PyQt Signals & Slots
- PyQt - Signals and Slots
- PyQt - Support and Signals
- PyQt - Unbound and Bound Signals
- PyQt - New Signals with PyQtSignal
- PyQt - Connecting, Disconnecting, & Emitting Signals
- PyQt - Slot decorator
- PyQt - Slot Connection
- PyQt Layouts
- PyQt - Layout Management
- PyQt - QBoxLayout
- PyQt - QGridLayout
- PyQt - QFormLayout
- PyQt - QHBoxLayout
- PyQt - QVBoxLayout
- PyQt - QStackedLayout
- PyQt - QGraphicsGridLayout
- PyQt - QGraphicsAnchorLayout
- PyQt - QGraphicsLayout
- PyQt - QGraphicsLinearLayout
- PyQt Basic Widgets
- PyQt - Basic Widgets
- PyQt - Qlabel Widget
- PyQt - QLineEdit Widget
- PyQt - QPushButton Widget
- PyQt - QRadioButton Widget
- PyQt - QCheckBox Widget
- PyQt - QComboBox Widget
- PyQt - QSpinBox Widget
- PyQt - QMessageBox
- PyQt - QDialogButtonBox Widget
- PyQt - QFontComboBox Widget
- PyQt - QDoubleSpinBox Widget
- PyQt - QToolBox Widget
- PyQt - QMenuBar, QMenu & Qaction Widgets
- PyQt - QToolTip
- PyQt - QInputDialog Widget
- PyQt - QFontDialog Widget
- PyQt - QDialog Widget
- PyQt - QFileDialog Widget
- PyQt - QTab Widget
- PyQt - QSplitter Widget
- PyQt - QDock Widget
- PyQt - QStatusBar Widget
- PyQt - QTabBar
- PyQt - QList Widget
- PyQt - QScrollBar Widget
- PyQt - QProgressBar
- PyQt - QCalendar Widget
- PyQt - QMessageBox Widget
- PyQt - QPlainTextEdit
- PyQt - QDateEdit
- PyQt - QDateTimeEdit
- PyQt - QTimeEdit
- PyQt - QTextEdit
- PyQt - QTextBrowser
- PyQt - QScrollArea
- PyQt - Drag and Drop
- PyQt - Multiple Document Interface
- PyQt - QDialog Class
- PyQt Views
- PyQt - QColumnView
- PyQt - QTableView
- PyQt Drawing API
- PyQt - Drawing API
- PyQt - Drawing a Line
- PyQt - Drawing a Rectangle
- PyQt - Drawing a Triangle
- PyQt - Drawing a Circle
- PyQt - Drawing a Ellipse
- PyQt - Drawing a Polygon
- PyQt - Geometric Transformation
- PyQt - Drawing Effect
- PyQt Groups
- PyQt - QButtonGroup
- PyQt - QGroupBox
- PyQt Effects
- PyQt - Effects
- PyQt - Opacity Effect
- PyQt - QGraphicsBlur Effect
- PyQt - QGraphicsColorize Effect
- PyQt - QGraphicsDropShadow Effect
- PyQt Events
- PyQt - Event Handling
- PyQt - Drag & Drop Events
- PyQt - File Open Event
- PyQt - Action Event
- PyQt - Hide Event
- PyQt - Resize Event
- PyQt Database
- PyQt - Database Handling
- PyQt Essentials
- PyQt - BrushStyle Constants
- PyQt - QClipboard
- PyQt - QPixmap Class
- PyQt Useful Resources
- PyQt - Quick Guide
- PyQt - Useful Resources
- PyQt - Discussion
PyQt - Drawing a Polygon
A polygon is consider as multiple geometrical shapes which has aleast 3 sides, angles, or typically five or more than its. It is two-dimensional closed form shape of straight line. The example of polygon such as triangle, quadilateral, pentagon, hexagon, etc.

Drawing a Polygon in PyQt
In PyQt, a Polygon can be drawn either by the methods createPoly() or QPolygon(). These methods are called using drawPolygon(). The drawPolygon method is referenced from the class QPainter, i.e. used to draw a polygon in a QPainter device or widget.
Example
In this example, we follow the method named createPoly() to draw a polygon using PyQt.
import sys import math from PyQt6 import QtCore, QtGui, QtWidgets class MyWidget(QtWidgets.QWidget): def __init__(self, parent=None): QtWidgets.QWidget.__init__(self, parent) # set lineColor self.pen = QtGui.QPen(QtGui.QColor(0, 0, 0)) # set line width self.pen.setWidth(3) # set fillColor self.brush = QtGui.QBrush(QtGui.QColor(255, 255, 255, 255)) # The createPoly parameters- n point, radius, angle of first point self.polygon = self.createPoly(8,140,0) def createPoly(self, n, r, s): polygon = QtGui.QPolygonF() # angle as per step w = 360/n # add the points to polygon for i in range(n): t = w * i + s x = r * math.cos(math.radians(t)) y = r * math.sin(math.radians(t)) polygon.append(QtCore.QPointF(self.width() / 2 + x, self.height() / 2 + y)) return polygon def paintEvent(self, event): painter = QtGui.QPainter(self) painter.setPen(QtCore.Qt.GlobalColor.green) painter.setBrush(QtCore.Qt.GlobalColor.gray) painter.drawPolygon(self.polygon) app = QtWidgets.QApplication(sys.argv) widget = MyWidget() widget.show() sys.exit(app.exec())
Output
The above code produces the following output −

Drawing a Polygon(triangle) in PyQt
As we earlier discussed about polygons that consider a minimum of 3 sides and angles. So, below the example illustrates tringle using various methods such as QPolygon(), QPoint(), and setGeometry().
The built-in function of QPolygon and Qpoint classes −
- QPolygon() − The class QPolygon itself a built-in function QPolygon() that store the list of point using integer precision.
- Qpoint() − The Qpoint class perform the same functionality w.r.t QPolygon. The class QPoint works on two parameters- x coordinate and y coordinate.
Note that, QPoint() can be applied multiple times to the method QPolygon based on polygon shape.
Example
Following example demonstrate the code snippet of polygon using PyQt.
import sys from PyQt6.QtGui import QPainter, QPolygon, QColor from PyQt6.QtWidgets import QWidget, QApplication from PyQt6.QtCore import QPoint class Example(QWidget): def __init__(self): super().__init__() self.setGeometry(400, 400, 450, 300) def paintEvent(self, event): qp = QPainter(self) qp.setPen(QColor(0, 0, 0)) qp.setBrush(QColor(255, 155, 156)) points = QPolygon([QPoint(60, 60), QPoint(160, 60), QPoint(110, 160)]) qp.drawPolygon(points) if __name__ == '__main__': app = QApplication(sys.argv) ex = Example() ex.show() sys.exit(app.exec())
Output
The above code produces the following output −
