PyQt5 Signal And Slots Practical Example

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

 

 

 

First of all we need some imports, basically we are importing some required classes of PyQt5.

 

 

 

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.

 

 

 

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.

 

 

 

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.

 

 

Also every PyQt5 application must create an application object.

 

 

Finally, we enter the mainloop of the application. The event handling starts from this point. 

 

 

 

 

 

Complete Source Code  PyQt5 Signal And Slots Practical Example

 

 

 

Run the code and this will be the result

PyQt5 Signal And Slots Practical Example
PyQt5 Signal And Slots Practical Example

 

 

 

This is more complex example on PyQt5 Signals and Slots

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

PyQt5 Signal And Slots Practical Example
PyQt5 Signal And Slots Practical Example

 

 

 

Also you can watch the complete video for this article

Leave a Comment

Share via
Copy link
Powered by Social Snap