Checkbox (QCheckbox) is a default qt widget that can be checked with the method setChecked(true). It’s part of PyQt5.QtWidgets.

You can add a click callback / signal with .toggled.connect(). The receiving slot can then handle the event.

pyqt checkox

Related Course:
PyQt Desktop Apps with Python
icon

QCheckBox

PyQt Checkbox Example

The example below creates a checkbox in a PyQt window. If you click on the checkbox it will call the method onClicked(). The method .isChecked() can be used to get the checkbox state.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
from PyQt5.QtWidgets import *
import sys

class Window(QWidget):
def __init__(self):
QWidget.__init__(self)
layout = QGridLayout()
self.setLayout(layout)

radiobutton = QCheckBox("I have a Cat")
radiobutton.setChecked(True)
radiobutton.animal = "Cat"
radiobutton.toggled.connect(self.onClicked)
layout.addWidget(radiobutton, 0, 0)

def onClicked(self):
radioButton = self.sender()
print("Animal " + (radioButton.animal) + " is " + str(radioButton.isChecked()))

app = QApplication(sys.argv)
screen = Window()
screen.show()
sys.exit(app.exec_())