In this PyQt5 article i want to show you How To Draw Polygon In Python With PyQt5 (QPainter Class). QPainter class is is used for drawing different shapes in PyQt5. before this we had a complete introduction to QPainter class so you can check my previous articles in the below links.
Learn More on Qt5
1: How To Draw Rectangle In PyQt5 With QPainter Class
2: How To Draw Ellipse in PyQt5 With QPainter Class
Also you can check more Python GUI articles in the below links
1: Kivy GUI Development Tutorials
2: TKinter GUI Development Tutorials
4: wxPython GUI Development Tutorials
5: PyQt5 GUI Development Tutorials
This is the complete source code for How To Draw Polygon In Python With PyQt5 (QPainter Class)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
from PyQt5 import QtGui from PyQt5.QtWidgets import QApplication, QMainWindow import sys from PyQt5.QtGui import QBrush, QPen,QPainter, QPolygon from PyQt5.QtCore import QPoint, Qt class Window(QMainWindow): def __init__(self): super().__init__() self.title = "PyQt5 Drawing Polygon" self.top = 200 self.left = 500 self.width = 400 self.height = 300 self.InitWindow() def InitWindow(self): self.setWindowIcon(QtGui.QIcon("icon.png")) self.setWindowTitle(self.title) self.setGeometry(self.left, self.top, self.width, self.height) self.show() def paintEvent(self, event): painter = QPainter(self) painter.setPen(QPen(Qt.black, 5, Qt.SolidLine)) #painter.setBrush(QBrush(Qt.red, Qt.SolidPattern)) painter.setBrush(QBrush(Qt.red, Qt.VerPattern)) points = QPolygon([ QPoint(10,10), QPoint(10,100), QPoint(100,10), QPoint(100,100) ]) painter.drawPolygon(points) App = QApplication(sys.argv) window = Window() sys.exit(App.exec()) |
These are the imported classes that we are using in this article.
1 2 3 4 5 |
from PyQt5 import QtGui from PyQt5.QtWidgets import QApplication, QMainWindow import sys from PyQt5.QtGui import QBrush, QPen,QPainter, QPolygon from PyQt5.QtCore import QPoint, Qt |
So now this is our main Window class that extends from QMainWindow. and in the constructor of the class we need to initialize some window requirements.
1 2 3 4 5 6 7 8 9 10 11 12 |
class Window(QMainWindow): def __init__(self): super().__init__() self.title = "PyQt5 Drawing Polygon" self.top = 200 self.left = 500 self.width = 400 self.height = 300 self.InitWindow() |
In here we are going to set our window title, window icon and window geometry, also we need to show our window in here.
1 2 3 4 5 |
def InitWindow(self): self.setWindowIcon(QtGui.QIcon("icon.png")) self.setWindowTitle(self.title) self.setGeometry(self.left, self.top, self.width, self.height) self.show() |
So in QPainter class when we are going to do drawing we need to use paintEvent() method. in this method first we have created the QPainter class object and we have set the QBrush and QPen for our QPainter class and for drawing of QPolygon we need some QPoints and at the end we draw our polygon
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
def paintEvent(self, event): painter = QPainter(self) painter.setPen(QPen(Qt.black, 5, Qt.SolidLine)) #painter.setBrush(QBrush(Qt.red, Qt.SolidPattern)) painter.setBrush(QBrush(Qt.red, Qt.VerPattern)) points = QPolygon([ QPoint(10,10), QPoint(10,100), QPoint(100,10), QPoint(100,100) ]) painter.drawPolygon(points) |
Run the complete code and this will be the result
Subscribe and Get Free Video Courses & Articles in your Email
Hey Parwiz, please help me out. its been only one month that I have started learning Qt. Now i need to draw an arrow on an image using mouse click events. I know how to draw rectangle, ellipse, line using the available libraries, but my requirement is to start drawing the arrow once the mouse is pressed and end the drawing when the mouse is released. I hope my querry is clear. please please please help me out. I am stuck at this and tried several ways, but failed. Hope you l reply.