In this Python PyQt6 article we are going to learn How to Build Custom Widgets with Python PyQt6, so Python PyQt6 is powerful graphical user interface (GUI) toolkit that allows developers to build interactive and nice desktop applications. PyQt6 offers different builtin widgets that can be used to create standard GUI components, such as buttons, labels and text fields. however sometimes you need to create custom widgets to meet specific design requirements or to add new functionality to your application. in this article we are going to talk about the process of building custom widgets using PyQt6.
For creating custom widget, you need to define new class that extends from an existing PyQt6 widget class, such as QWidget or QFrame. for example suppose you want to create custom widget that displays a circle. you can define a new class called CircleWidget that extends from the QWidget class as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
from PyQt6.QtWidgets import QWidget, QPainter class CircleWidget(QWidget): def __init__(self, parent=None): super().__init__(parent) self.setMinimumSize(50, 50) def paintEvent(self, event): painter = QPainter(self) painter.setRenderHint(QPainter.Antialiasing) painter.setPen(Qt.black) painter.setBrush(Qt.red) painter.drawEllipse(self.rect()) |
In the above code we have defined new class called CircleWidget that extends from the QWidget class. we have also defined a constructor that sets the minimum size of the widget to 50×50 pixels. paintEvent() method is called whenever the widget needs to be redrawn. in this method we have created QPainter object and set its render hint to QPainter.Antialiasing to ensure that the circle is drawn smoothly. We then set the pen color to black and the brush color to red and draw an ellipse that fills the entire widget’s rectangle.
This is the complete code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
from PyQt6.QtWidgets import QApplication, QWidget, QVBoxLayout from PyQt6.QtGui import QPainter from PyQt6.QtCore import Qt class CircleWidget(QWidget): def __init__(self, parent=None): super().__init__(parent) self.setMinimumSize(50, 50) def paintEvent(self, event): painter = QPainter(self) painter.setPen(Qt.GlobalColor.black) painter.setBrush(Qt.GlobalColor.red) painter.drawEllipse(self.rect()) app = QApplication([]) widget = QWidget() layout = QVBoxLayout(widget) circle_widget = CircleWidget() layout.addWidget(circle_widget) widget.show() app.exec() |
In this code, we have imported necessary PyQt6 classes: QApplication, QWidget, QVBoxLayout, QPainter, and Qt.
After that we have defined new class called CircleWidget that inherits from QWidget. in the constructor we set the minimum size of the widget to 50×50 pixels. in the paintEvent() method we have created QPainter object. we then set the pen color to black and the brush color to red and draw an ellipse that fills the entire widget’s rectangle.
and finally we have created QApplication object, QWidget object called widget and QVBoxLayout object called layout. we add the CircleWidget instance to the layout and show the widget. we then start the application event loop using app.exec().
Run the complete code and this is the result
Learn More on Python GUI
- How to Create Label in PySide6
- How to Create Button in Python & PySide6
- How to Use Qt Designer with PySide6
- How to Add Icon to PySide6 Window
- How to Load UI in Python PySide6
- How to Create RadioButton in PySide6
- How to Create ComboBox in PySide6
- How to Create CheckBox in Python PySide6
- Responsive Applications with PyQt6 Multithreading
- Event Handling in Python and PyQt6