In this lesson we want to learn about Python PySide6 Signals and Slots.
Introduction
In GUI programming, communication between objects is crucial for building complex and interactive applications. Signals and slots in PySide6 are a mechanism for implementing such communication. Signals are emitted by objects when an event occurs, and slots are functions that can be executed in response to a signal. Pyside6 is a Python binding for the Qt toolkit, which provides an implementation of the signals and slots mechanism. In this lesson, we will explore the signals and slots mechanism in Pyside6 and how it can be used to build interactive GUI applications.
Signals and Slots in Pyside6
Signals and slots mechanism in Pyside6 works by connecting a signal emitted by an object to a slot function. The connection is established using the connect()
method of the signal object. The connect()
method takes as arguments the slot function to be executed and the signal that it should be connected to.
For example, let’s see a simple GUI application with a button when clicked, should display a message. We can define a slot function for this purpose as follows:
1 2 |
def display_message(): print("Button clicked") |
To connect this slot function to the clicked
signal of a button, we can use the following code:
1 |
button.clicked.connect(display_message) |
Here, button
is an instance of the QPushButton
class, and clicked
is a signal emitted by the button when it is clicked. The connect()
method connects the clicked
signal to the display_message()
slot function, so that when the button is clicked, the display_message()
function is executed.
Passing Arguments to Slots
Slots can also take arguments when they are executed. For example, consider a GUI application with a slider that controls the font size of a text label. We can define a slot function that takes the value of the slider as an argument:
1 2 3 4 |
def set_font_size(value): font = label.font() font.setPointSize(value) label.setFont(font) |
To connect this slot function to the valueChanged
signal of a slider, we can use the following code:
1 |
slider.valueChanged.connect(set_font_size) |
Here, slider
is an instance of the QSlider
class, and valueChanged
is a signal emitted by the slider when its value changes. The connect()
method connects the valueChanged
signal to the set_font_size()
slot function, passing the value of the slider as an argument.
Disconnecting Signals and Slots
It is also possible to disconnect a signal from a slot using the disconnect()
method. For example, to disconnect the clicked
signal from the display_message()
slot function, we can use the following code:
1 |
button.clicked.disconnect(display_message) |
Here, the disconnect()
method disconnects the clicked
signal from the display_message()
slot function.
This is the complete code for a simple GUI application that demonstrates the use of signals and slots in Pyside6:
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 |
import sys from PySide6.QtWidgets import QApplication, QMainWindow, QPushButton, QSlider, QLabel class MyWindow(QMainWindow): def __init__(self): super().__init__() # Create a button self.button = QPushButton("Click me!", self) self.button.setGeometry(50, 50, 100, 50) # Create a slider self.slider = QSlider(self) self.slider.setGeometry(50, 120, 100, 20) self.slider.setRange(10, 30) self.slider.setValue(20) # Create a label self.label = QLabel("Hello, GeeksCoders.com", self) self.label.setGeometry(50, 180, 300, 50) # Connect the button clicked signal to the display_message slot self.button.clicked.connect(self.display_message) # Connect the slider valueChanged signal to the set_font_size slot self.slider.valueChanged.connect(self.set_font_size) def display_message(self): print("Button clicked") def set_font_size(self, value): font = self.label.font() font.setPointSize(value) self.label.setFont(font) if __name__ == "__main__": app = QApplication(sys.argv) window = MyWindow() window.setGeometry(100, 100, 200, 250) window.show() sys.exit(app.exec()) |
This code defines a MyWindow
class that inherits from QMainWindow
and defines a simple GUI with a button, a slider, and a label. The display_message()
slot function is connected to the button’s clicked
signal, and set_font_size()
slot function is connected to the slider’s valueChanged
signal.
When we click button, the display_message()
function is executed, which simply prints a message to the console. When the slider’s value changes, the set_font_size()
function is executed, which updates the font size of the label according to the value of the slider.
Run the complete code and this will be the result.

Final Thoughts
Signals and slots are a powerful mechanism for implementing communication between objects in GUI applications. In Pyside6, the signals and slots mechanism is implemented using the connect()
and disconnect()
methods of signal objects. Slots can take arguments, and connections can be established and disconnected dynamically at runtime. This flexibility makes signals and slots a popular choice for building interactive GUI applications in Pyside6.