Creating Custom Widgets in PyQt5

11 May, 2020
PyQt5 is one of the most advanced GUI library for Python. While being powerful, it’s also well structured and makes it easy for you to build ‘advanced’ items. Custom widgets in PyQt5 is a breeze.
The below has a well-described way of building custom Widgets with PyQt5,
The Main WindowLet’s start by creating our main window. We go the OOP route right from the start. An OOP-less way is a pain for maintenance. Our skeleton looks like this
import sys  from PyQt5 import QtWidgetsfrom PyQt5 import QtCorefrom PyQt5 import QtGui    class MainWindow(QtWidgets.QMainWindow):    def __init__(self, parent = None):        super().__init__(parent)        self.init_gui()      def init_gui(self):        self.window = QtWidgets.QWidget()        self.layout = QtWidgets.QGridLayout()        self.setCentralWidget(self.window)        self.window.setLayout(self.layout)                if __name__ == '__main__':    app = QtWidgets.QApplication([])      win = MainWindow()    win.show()      sys.exit(app.exec_())
which outputs to
A Normal App
Let’s add a textbox and a label that just echoes whatever we type.Our main window turns to this:
class MainWindow(QtWidgets.QMainWindow):    def __init__(self, parent = None):        super().__init__(parent)        self.init_gui()      def init_gui(self):        self.window = QtWidgets.QWidget()        self.layout = QtWidgets.QGridLayout()        self.setCentralWidget(self.window)        self.window.setLayout(self.layout)          self.textbox = QtWidgets.QLineEdit()        self.echo_label = QtWidgets.QLabel('')          self.textbox.textChanged.connect(self.textbox_text_changed)          self.layout.addWidget(self.textbox, 0, 0)        self.layout.addWidget(self.echo_label, 1, 0)      def textbox_text_changed(self):        self.echo_label.setText(self.textbox.text())
which outputs to
Widget Skeleton
An empty widget looks like this:
class MyWidget(QtWidgets.QWidget):      def __init__(self, *args, **kwargs):        super().__init__(*args, **kwargs)        self.layout = QtWidgets.QGridLayout()        self.setLayout(self.layout)
Let’s name it as EchoText and add what we added to our main window
class EchoText(QtWidgets.QWidget):      def __init__(self, *args, **kwargs):        super().__init__(*args, **kwargs)        self.layout = QtWidgets.QGridLayout()        self.setLayout(self.layout)          self.textbox = QtWidgets.QLineEdit()        self.echo_label = QtWidgets.QLabel('')          self.textbox.textChanged.connect(self.textbox_text_changed)          self.layout.addWidget(self.textbox, 0, 0)        self.layout.addWidget(self.echo_label, 1, 0)      def textbox_text_changed(self):        self.echo_label.setText(self.textbox.text())
Using Like A Normal Widget
In our main window, leave only the skeleton and add the following:
self.echotext_widget = EchoText()  self.layout.addWidget(self.echotext_widget)
and it gets displayed as when we coded everything in the main window itself.
The Complete App
Here’s the complete code for the entire application
import sys  from PyQt5 import QtWidgetsfrom PyQt5 import QtCorefrom PyQt5 import QtGui  class EchoText(QtWidgets.QWidget):      def __init__(self, *args, **kwargs):        super().__init__(*args, **kwargs)        self.layout = QtWidgets.QGridLayout()        self.setLayout(self.layout)          self.textbox = QtWidgets.QLineEdit()        self.echo_label = QtWidgets.QLabel('')          self.textbox.textChanged.connect(self.textbox_text_changed)          self.layout.addWidget(self.textbox, 0, 0)        self.layout.addWidget(self.echo_label, 1, 0)      def textbox_text_changed(self):        self.echo_label.setText(self.textbox.text())    class MainWindow(QtWidgets.QMainWindow):      def __init__(self, parent = None):        super().__init__(parent)        self.init_gui()      def init_gui(self):        self.window = QtWidgets.QWidget()        self.layout = QtWidgets.QGridLayout()        self.setCentralWidget(self.window)        self.window.setLayout(self.layout)          self.echotext_widget = EchoText()          self.layout.addWidget(self.echotext_widget)  if __name__ == '__main__':    app = QtWidgets.QApplication([])      win = MainWindow()    win.show()      sys.exit(app.exec_())
Try creating a different widget and adding it. PyQt5 sounds tougher than Tkinter to get started but it’s worth it!
Python-gui
Python-PyQt
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Different ways to create Pandas Dataframe
Enumerate() in Python
Python String | replace()
How to Install PIP on Windows ?
*args and **kwargs in Python
Python Classes and Objects
Python OOPs Concepts
Convert integer to string in Python
Introduction To PYTHON
How to drop one or multiple columns in Pandas Dataframe