In this PyQt5 article we are going to talk about Signal and Slots Practical Example in PyQt5, also we are going to create Practical Example of signal and slot in PyQt5.
What are Signal and Slots in PyQt5 ?
Signal and Slots are used for communication between some objects. a Signal is emitted when a particular event occurs, and a Slot is called when its connected signal is emitted. so now we are going to create an example of PyQt5 Signal and Slots.
Join PyQt5 Full Course for Free
PyQt5 Course
Also you can read more Python GUI articles in the below links
- Kivy GUI Development Tutorials
- TKinter GUI Development Tutorials
- Pyside2 GUI Development
- wxPython GUI Development Tutorials
- PyQt5 GUI Development Tutorials
First of all we need some imports, basically we are importing some required classes of PyQt5.
1 2 3 4 5 |
from PyQt5.QtWidgets import QApplication, QMainWindow, QPushButton import sys from PyQt5 import QtGui from PyQt5.QtCore import QRect from PyQt5 import QtCore |
So now this is our main Window class that extends from QMainwindow, and we are going to add some requirement of the window in this class like title, window geometry, window icon and also we call our CreateButton() method in this class.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
class Window(QMainWindow): def __init__(self): super().__init__() title = "PyQt5 Signal And Slots left = 500 top = 200 width = 300 height = 250 iconName = "icon.png" self.setWindowTitle(title) self.setWindowIcon(QtGui.QIcon(iconName)) self.setGeometry(left, top, width, height) self.CreateButton() self.show() |
This is our method that we have called in our main window class, basically in this method we are going to create a QPushButton and we add geometry, icon and iconSize for our QPushButton. also we have connected the clicked signal of QPushButton with the ButtonAction method or slot that we will create later.
1 2 3 4 5 6 |
def CreateButton(self): button = QPushButton("Close Application", self) button.setGeometry(QRect(100,100,111,28)) button.setIcon(QtGui.QIcon("icon.png")) button.setIconSize(QtCore.QSize(40,40)) button.clicked.connect(self.ButtonAction) |
So now this is the method that we have already connected this with the clicked signal of QPushButton, this will just close the window by clicking the button.
1 2 |
def ButtonAction(self): sys.exit() |
Also every PyQt5 application must create an application object.
1 |
App = QApplication(sys.argv) |
Finally, we enter the mainloop of the application. The event handling starts from this point.
1 2 |
window = Window() sys.exit(App.exec_()) |
Complete Source Code PyQt5 Signal And Slots Practical Example
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 |
from PyQt5.QtWidgets import QApplication, QMainWindow, QPushButton import sys from PyQt5 import QtGui from PyQt5.QtCore import QRect from PyQt5 import QtCore class Window(QMainWindow): def __init__(self): super().__init__() title = "PyQt5 Signal And Slots" left = 500 top = 200 width = 300 height = 250 iconName = "icon.png" self.setWindowTitle(title) self.setWindowIcon(QtGui.QIcon(iconName)) self.setGeometry(left, top, width, height) self.CreateButton() self.show() def CreateButton(self): button = QPushButton("Close Application", self) button.setGeometry(QRect(100,100,111,28)) button.setIcon(QtGui.QIcon("python.png")) button.setIconSize(QtCore.QSize(40,40)) button.clicked.connect(self.ButtonAction) def ButtonAction(self): sys.exit() if __name__ == "__main__": App = QApplication(sys.argv) window = Window() sys.exit(App.exec_()) |
Run the code and this will be the result
This is more complex example on PyQt5 Signals and Slots
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, QVBoxLayout, QHBoxLayout, QLabel, QLineEdit, QPushButton import sys class MyWindow(QWidget): def __init__(self): super().__init__() self.setWindowTitle('Calculator') self.setGeometry(100, 100, 400, 200) # Create the widgets self.num1_label = QLabel('Number 1:') self.num1_edit = QLineEdit() self.num2_label = QLabel('Number 2:') self.num2_edit = QLineEdit() self.result_label = QLabel('Result:') self.calculate_button = QPushButton('Calculate') # Create the layout vbox = QVBoxLayout() hbox1 = QHBoxLayout() hbox2 = QHBoxLayout() hbox1.addWidget(self.num1_label) hbox1.addWidget(self.num1_edit) hbox2.addWidget(self.num2_label) hbox2.addWidget(self.num2_edit) vbox.addLayout(hbox1) vbox.addLayout(hbox2) vbox.addWidget(self.result_label) vbox.addWidget(self.calculate_button) self.setLayout(vbox) # Connect the button to the calculate() method self.calculate_button.clicked.connect(self.calculate) def calculate(self): # Try to convert the text in the QLineEdit widgets to integers try: num1 = int(self.num1_edit.text()) num2 = int(self.num2_edit.text()) result = num1 + num2 self.result_label.setText('Result: ' + str(result)) # Handle the case when the user enters an invalid value except ValueError: self.result_label.setText('Result: Error') if __name__ == '__main__': app = QApplication(sys.argv) window = MyWindow() window.show() sys.exit(app.exec_()) |
This code is an example of a simple calculator GUI built using the PyQt5 library. GUI consists of a window that contains two input fields for entering numbers a label for displaying the result of the calculation, and a button to trigger the calculation.
this code first imports the necessary modules from PyQt5 including QApplication, QWidget, QVBoxLayout, QHBoxLayout, QLabel, QLineEdit and QPushButton. it also imports the sys module for handling system level operations.
MyWindow class is defined which inherits from the QWidget class. init method is called when an instance of the MyWindow class is created. in this method the window title and size are set, and widgets are created. these includes two labels for the input fields, two QLineEdit widgets for entering the numbers, label for displaying the result, and QPushButton widget for triggering the calculation. these widgets are then added to the window layout which consists of two QHBoxLayout objects for organizing the input fields and a QVBoxLayout object for organizing the entire layout.
calculate method is defined to perform the addition operation on the two input numbers and display the result in the result label. it is called when the Calculate button is clicked. this method first attempts to convert the text in the QLineEdit widgets to integers using int() function. if successful it adds the two numbers and sets the result in the result label using the setText() method. if an invalid value is entered such as non numeric character, an exception is raised, and an error message is displayed in the result label.
main block of the code checks if the script is being run directly (i.e., not imported as a module) and creates an instance of the MyWindow class. The window is then shown using the show() method, and the script enters the main event loop using the exec_() method of the QApplication class. Finally, the sys.exit() function is called to exit the script when the user closes the window or quits the application.
Run the code and this will be the result
Also you can watch the complete video for this article
Subscribe and Get Free Video Courses & Articles in your Email