In this PyQt5 Tutorial we are going to learn about Create Custom Widget in PyQt5, PyQt5 is powerful Python library that allows developers to create graphical user interfaces (GUIs) quickly and easily. one of the key strengths of PyQt5 is its ability to create custom widgets, which can be used to add new functionality to your applications. In this article we will walk through the process of creating custom widget in PyQt5.
Import Required Libraries: for creating custom widget we will need to import the following PyQt5 libraries:
- QtCore
- QtWidgets
- QtGui
1 |
from PyQt5 import QtCore, QtGui, QtWidgets |
Define Custom Widget: we need to create custom widget by subclassing QWidget class. in this example, we are going to create custom widget called MyWidget that displays red circle.
1 2 3 4 5 6 7 8 9 10 |
class MyWidget(QtWidgets.QWidget): def __init__(self, parent=None): super().__init__(parent) self.setMinimumSize(100, 100) def paintEvent(self, event): painter = QtGui.QPainter(self) painter.setRenderHint(QtGui.QPainter.Antialiasing, True) painter.setBrush(QtGui.QColor(255, 0, 0)) painter.drawEllipse(self.rect()) |
In the constructor we have set minimum size of the widget to 100×100 pixels. in the paintEvent method we have create QPainter object that will be used to draw the red circle. we have set render hint to enable anti aliasing, which smooths out the edges of the circle. than we set the brush color to red and draw an ellipse that fits the entire widget’s rectangular area.
Use Custom Widget in PyQt5 Application : We can now use our custom widget in PyQt5 application. this is an example of how to create window that contains our custom widget.
1 2 3 4 5 6 7 8 9 10 11 12 |
class MainWindow(QtWidgets.QMainWindow): def __init__(self): super().__init__() widget = MyWidget() self.setCentralWidget(widget) if __name__ == '__main__': app = QtWidgets.QApplication([]) window = MainWindow() window.show() app.exec_() |
In MainWindow constructor we have created an instance of our custom widget and set it as the central widget of the window. after that we have created an instance of QApplication and show the window.
In result we can say that Creating custom widgets is powerful feature of PyQt5 that allows developers to create highly specialized user interfaces. by subclassing QWidget class and implementing the paintEvent method we can create widgets that display custom graphics and interact with the user in unique ways. In this article we have learned how to create simple custom widget that displays red circle.
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 25 26 |
from PyQt5 import QtCore, QtGui, QtWidgets class MyWidget(QtWidgets.QWidget): def __init__(self, parent=None): super().__init__(parent) self.setMinimumSize(100, 100) def paintEvent(self, event): painter = QtGui.QPainter(self) painter.setRenderHint(QtGui.QPainter.Antialiasing, True) painter.setBrush(QtGui.QColor(255, 0, 0)) painter.drawEllipse(self.rect()) class MainWindow(QtWidgets.QMainWindow): def __init__(self): super().__init__() self.setWindowTitle("GeeksCoders.com") widget = MyWidget() self.setCentralWidget(widget) if __name__ == '__main__': app = QtWidgets.QApplication([]) window = MainWindow() window.show() app.exec_() |
Run the code and this will be the result
Learn More on Python
- Is Python Good for GUI Apps
- Is Python Good for Ethical Hacking
- Python Best Libraries for Web Development
- Top 10 Python REST API Frameworks
- How to Build REST API with Flask
- Build Python REST API with FastAPI
- Python Best Frameworks for Web Development
- Why to Use Python for Web Development
- PyQt5 TableWidget Tutorial