In this Python PySide6 article we are going to talk about How to Use Signals & Slots in Python Pyside6, Signals and slots are powerful feature of PySide6. PySide6 is Qt binding for Python Programming Language, Signals and slots allow you to communicate between objects in a way that is both flexible and efficient. In this article we want to talk the basics of signals and slots in PySide6 and see how they can be used to build dynamic and responsive applications.
Now let’s talk about Python PySide6 Signals and Slots, a signal is an event that is emitted by an object when particular action or event occurs. slot is a function that is executed when signal is emitted. Signals and slots provide a mechanism for communication between objects without requiring them to have direct knowledge of each other.
For example, suppose you have a button in your application that the user can click. when the user clicks the button, you want to display a message box. you can achieve this using signals and slots in PySide6.
The button emits a signal when it is clicked, and you can connect this signal to a slot that displays the message box. button does not need to know anything about the message box, and the message box does not need to know anything about the button. Signals and slots provide clean and decoupled way of connecting objects in your application.
How to Use Signals & Slots in Python Pyside6
Now for connecting a signal to slot we can use connect() method of the signal. connect() method takes the slot function as its argument. this is an example:
1 |
button.clicked.connect(display_message_box) |
In this example button is an instance of the QPushButton class, and clicked is a signal that is emitted when the button is clicked. display_message_box is a function that is executed when the signal is emitted.
Also you can connect multiple slots to a signal
1 2 |
button.clicked.connect(display_message_box) button.clicked.connect(do_something_else) |
In PySide6 also you can define your own signal like this, in the below code MyObject is subclass of the QObject class, and my_signal is a signal that takes an integer and a string as arguments.
1 2 |
class MyObject(QObject): my_signal = Signal(int, str) |
To emit the signal you call the emit() method of the signal and pass in the arguments.
1 |
self.my_signal.emit(42, "GeeksCoders") |
Now this is the complete code for Python PySide6 Signals and Slots, in this example we have QLabel and QPushButton, by clicking of the button we want to change the label text.
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 |
from PySide6.QtCore import Qt from PySide6.QtWidgets import QApplication, QMainWindow, QPushButton, QLabel from PySide6.QtGui import QFont class MyMainWindow(QMainWindow): def __init__(self): super().__init__() # Create the label self.label = QLabel("Hello, world", self) self.label.setAlignment(Qt.AlignCenter) self.setCentralWidget(self.label) # Create the button self.button = QPushButton("Click me", self) self.button.clicked.connect(self.change_label_text) self.button.move(50, 50) def change_label_text(self): self.label.setText("Welcome to GeeksCoders.com") self.label.setFont(QFont("Sanserif", 15)) self.label.setStyleSheet('color:red') if __name__ == "__main__": app = QApplication([]) window = MyMainWindow() window.show() app.exec() |
Now let’s describe the above code, first of all we have imported the required modules and classes from PySide6.
1 2 3 |
from PySide6.QtCore import Qt from PySide6.QtWidgets import QApplication, QMainWindow, QPushButton, QLabel from PySide6.QtGui import QFont |
After that we have created MyMainWindow class that extends from QMainWindow class. inside MyMainWindow class we have created QLabel widget, we have also created QPushButton widget and connect its clicked signal to the change_label_text() method using the connect() method.
1 2 3 4 5 6 7 8 9 10 11 |
class MyMainWindow(QMainWindow): def __init__(self): super().__init__() self.label = QLabel("Hello, world", self) self.label.setAlignment(Qt.AlignCenter) self.setCentralWidget(self.label) self.button = QPushButton("Click me", self) self.button.clicked.connect(self.change_label_text) self.button.move(50, 50) |
change_label_text() method simply sets the text of the label to Welcome to GeeksCoders.com, also by clicking of the button we are changing the text color and font size.
1 2 3 4 |
def change_label_text(self): self.label.setText("Welcome to GeeksCoders.com") self.label.setFont(QFont("Sanserif", 15)) self.label.setStyleSheet('color:red') |
At the end we have created an instance of the QApplication class, an instance of the MyMainWindow class and call show() method to display the main window. after that we starts event loop by calling exec() method of the QApplication instance.
1 2 3 4 5 |
if __name__ == "__main__": app = QApplication([]) window = MyMainWindow() window.show() app.exec() |
Run the code and click on the button this will be the result
Learn More on Python GUI
- How to Create Label in PySide6
- How to Create Button in Python & PySide6
- How to Use Qt Designer with PySide6
- How to Add Icon to PySide6 Window
- How to Load UI in Python PySide6
- How to Create RadioButton in PySide6
- How to Create ComboBox in PySide6
- How to Create CheckBox in Python PySide6
- Responsive Applications with PyQt6 Multithreading
- Event Handling in Python and PyQt6
- How to Use Stylesheets in Python PyQt6