How To Draw Polygon In Python With PyQt5 (QPainter Class)

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.

 

 

 

This is the complete source code for How To Draw Polygon In Python With PyQt5 (QPainter Class)

 

 

 

These are the imported classes that we are using in this article.

 

 

 

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.

 

 

 

In here we are going to set our window title, window icon and window geometry, also we need to show our window in here.

 

 

 

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

 

 

 

 

Run the complete code and this will be the result

How To Draw Polygon In Python With PyQt5 (QPainter Class)
How To Draw Polygon In Python With PyQt5 (QPainter Class)

 

 

 

 

FAQs:

 

How do you draw a polygon in Python?

In Python, you can draw a polygon using different libraries such as PyQt5 or matplotlib. In PyQt5, you can use QPainter class to draw polygons. 

 

 

How to add shape in PyQt5?

For adding shapes in PyQt5, you typically need a subclass of QWidget or QMainWindow and override paintEvent method. Inside the paintEvent method, you can use QPainter to draw different shapes such as rectangles, ellipses, polygons, lines, etc. 

Subscribe and Get Free Video Courses & Articles in your Email

 

1 thought on “How To Draw Polygon In Python With PyQt5 (QPainter Class)”

  1. 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.

    Reply

Leave a Comment

Share via
Copy link
Powered by Social Snap
×