In this PyQt5 article we want to learn about Graphics and Painting in Python PyQt5, when you are building GUI applications, sometimes you may need to draw something on the screen, for drawing in PyQt5 we can use QPainter class, and also we know that PyQt5 is one of the most popular GUI Frameworks in Python, and also PyQt5 provides powerful tools for creating graphics and painting in your applications.
PyQt5 Installation
First of all we need to install PyQt5, for the installation we can use pip like this:
1 |
pip install PyQt5 |
PyQt5 Basic Drawing
Now let’s start from basic drawing, in this code we are going to create a custom widget by subclassing QWidget and override its paintEvent method. in this method, we can use QPainter to do drawing operations. this is a simple example that draws a rectangle on the window:
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 |
# Import necessary modules from PyQt5 from PyQt5.QtWidgets import QApplication, QWidget from PyQt5.QtGui import QPainter, QColor # Define custom widget class that inherits from QWidget class CustomWidget(QWidget): # Override paintEvent method to customize the painting def paintEvent(self, event): # Create QPainter object for this widget painter = QPainter(self) # Set pen color to black painter.setPen(QColor(0, 0, 0)) # Set the brush color to red painter.setBrush(QColor(255, 0, 0)) # Draw a rectangle at position (10, 10) painter.drawRect(10, 10, 100, 100) # Create a QApplication object app = QApplication([]) # Create an instance of custom widget widget = CustomWidget() # Show the widget widget.show() # Start application event loop app.exec_() |
In this example, we have created a CustomWidget class that overrides the paintEvent method. after that we create a QPainter object and set the pen and brush properties to define the drawing style. and lastly we call drawRect to draw a rectangle on the widget.
Run the complete code and this will be the result
Handling PyQt5 Mouse Events
Graphics applications often need to respond to user interactions such as mouse clicks and movements. PyQt5 provides event handlers for mouse events that can be overridden to add custom behavior. Let’s extend our previous example to respond to a mouse click:
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 |
import sys from PyQt5.QtWidgets import QApplication, QWidget from PyQt5.QtGui import QPainter, QColor from PyQt5.QtCore import Qt # Define custom widget class that inherits from QWidget class CustomWidget(QWidget): def __init__(self): super().__init__() # Enable mouse tracking to detect mouse movements self.setMouseTracking(True) # Override paintEvent method to customize the painting def paintEvent(self, event): # Create QPainter object for this widget painter = QPainter(self) # Set pen color to black painter.setPen(QColor(0, 0, 0)) # Set brush color to red painter.setBrush(QColor(255, 0, 0)) # Draw a rectangle at position painter.drawRect(10, 10, 100, 100) # Override the mousePressEvent method def mousePressEvent(self, event): # Check if the left mouse button was pressed if event.button() == Qt.LeftButton: print("Left button clicked") # Create QApplication object app = QApplication([]) # Create an instance of custom widget widget = CustomWidget() # Show widget widget.show() # Start the application's event loop sys.exit(app.exec_()) |
PyQt5 Advanced Drawing
PyQt5 provides different drawing primitives and features for advanced graphics. You can draw lines, polygons, ellipses and even images. this is an example of drawing some more shapes.
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 53 |
from PyQt5.QtWidgets import QApplication, QWidget from PyQt5.QtGui import QPainter, QColor, QPolygon, QImage from PyQt5.QtCore import QPoint # Define custom widget class that inherits from QWidget class CustomWidget(QWidget): # Override paintEvent method to customize the painting def paintEvent(self, event): # Create QPainter object for this widget painter = QPainter(self) # Set pen color to black painter.setPen(QColor(0, 0, 0)) # Set brush color to red painter.setBrush(QColor(255, 0, 0)) # Draw rectangle at position painter.drawRect(10, 10, 100, 100) # Draw line from point painter.drawLine(10, 10, 100, 100) # Draw polygon with three points polygon = QPolygon([QPoint(200, 10), QPoint(250, 110), QPoint(300, 10)]) # Set the brush color to green painter.setBrush(QColor(0, 255, 0)) # Draw the polygon painter.drawPolygon(polygon) # Set the brush color to blue painter.setBrush(QColor(0, 0, 255)) # Draw an ellipse at position painter.drawEllipse(150, 150, 100, 50) # Load an image from file image = QImage("python.png") # Draw the image at position (300, 200) painter.drawImage(300, 200, image) # Create a QApplication object app = QApplication([]) # Create an instance of the custom widget widget = CustomWidget() # Show the widget widget.show() # Start the application event loop app.exec_() |
In this example, we have added four additional drawing operations:
- drawPolygon: We create a QPolygon object with a set of QPoint vertices and use it to draw a polygon on the widget.
- drawEllipse: We specify the bounding rectangle coordinates and draw an ellipse within it.
- drawImage: We load an image using the QImage class and draw it on the widget at the specified position.
Run the complete code and this is the result
Subscribe and Get Free Video Courses & Articles in your Email