PyQt5 signals and slots

Graphical applications (GUI) are event-driven, unlike console or terminal applications. A users action like clicks a button or selecting an item in a list is called an event.

If an event takes place, each PyQt5 widget can emit a signal. A signal does not execute any action, that is done by a slot.

Related courses:

Signals and slot introduction
Consider this example:


The button click (signal) is connected to the action (slot). In this example, the method slot_method will be called if the signal emits.

This principle of connecting slots methods or function to a widget, applies to all widgets,


or we can explicitly define the signal:

QtCore.QObject.connect(widget, QtCore.SIGNAL(‘signalname’), slot_function)

PyQt supports many type of signals, not just clicks.

We can create a method (slot) that is connected to a widget. A slot is any callable function or method.

pyqt button click

On running the application, we can click the button to execute the action (slot).

from PyQt5.QtWidgets import (QApplication, QComboBox, QDialog,
        QDialogButtonBox, QFormLayout, QGridLayout, QGroupBox, QHBoxLayout,
        QLabel, QLineEdit, QMenu, QMenuBar, QPushButton, QSpinBox, QTextEdit,
import sys
class Dialog(QDialog):
    def slot_method(self):
        print('slot method called.')
    def __init__(self):
        super(Dialog, self).__init__()
        mainLayout = QVBoxLayout()
        self.setWindowTitle("Button Example -")
if __name__ == '__main__':
    app = QApplication(sys.argv)
    dialog = Dialog()

PyQt5 buttons
PyQt5 messagebox