In this lesson we want to learn about Python PySide6 QCheckBox. QCheckBox widget is a simple GUI component that can be used in different GUI applications. It can be used to allow users to select or deselect an option, to enable or disable a feature, or to show or hide a specific section of an application.
Creating QCheckBox in PySide6 is straightforward. you can create new QCheckBox instance using the following code:
1 2 3 4 5 6 7 8 9 10 |
from PySide6.QtWidgets import QApplication, QMainWindow, QCheckBox app = QApplication() window = QMainWindow() checkbox = QCheckBox("Option 1", parent=window) checkbox.move(50, 50) window.show() app.exec() |
In this example, we have created new QCheckBox instance with label “Option 1”. after that we set the parent of the QCheckBox to be the main window and move it to position (50, 50) on the screen. Finally, we show the main window and start the application event loop.
Run the complete code and this will be the result.

QCheckBox widget emits a signal whenever its state changes, that can be used to trigger an action in the application. to connect a slot to the signal, you can use the following code:
1 2 3 4 5 6 7 |
def checkbox_changed(state): if state == 2: print("Checkbox is selected") else: print("Checkbox is deselected") checkbox.stateChanged.connect(checkbox_changed) |
In this example, we define new function checkbox_changed
that takes single argument state
. the state argument represents the new state of the QCheckBox (0 for unchecked, 1 for partially checked, and 2 for checked). after that we check state of the QCheckBox and print a message to the console.
after that we can connect the stateChanged
signal of the QCheckBox to the checkbox_changed
function using the connect
method. this will ensure that the checkbox_changed
function is called every time the state of the QCheckBox changes.
This is the complete code using OOP concept
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 |
from PySide6.QtWidgets import QApplication, QMainWindow, QCheckBox class MainWindow(QMainWindow): def __init__(self): super().__init__() self.setWindowTitle("QCheckBox Example") self.checkbox = QCheckBox("Option 1", parent=self) self.checkbox.move(50, 50) self.checkbox.stateChanged.connect(self.checkbox_changed) def checkbox_changed(self, state): if state == 2: print("Checkbox is selected") else: print("Checkbox is deselected") if __name__ == "__main__": app = QApplication() window = MainWindow() window.show() app.exec_() |
In the above example we have define a new class MainWindow
that inherits from QMainWindow
. after that we override __init__
method to set the window title and create a new QCheckBox
instance with the label “Option 1”. we set the parent of QCheckBox
to be the main window and move it to position (50, 50) on the screen. also we connect stateChanged
signal of the QCheckBox
to the checkbox_changed
method of MainWindow
class.
after that we define a new method checkbox_changed
that takes a single argument state
. this method checks the state of the QCheckBox
and prints a message to the console.
In the main block, we create a new QApplication
instance and a new MainWindow
instance. We show the main window and start the application event loop.
Using OOP principles allows us to encapsulate the functionality of the QCheckBox widget into a self contained class, making our code more modular and easier to maintain.
Final Thoughts
QCheckBox widget is powerful component of PySide6 toolkit. it has simple interface and powerful signals and slots system, it can be used to create a wide range of graphical interfaces for desktop applications.