Python

pyqt checkbox

A checkbox can be created using the QCheckBox widget. When creating an new checkbox with the QCheckBox class, the first parameter is the label.

To apply actions to the toggle switch, we call .stateChanged.connect() followed by a callback method. When this method is called, it sends a boolean as state parameter. If checked, its the value QtCore.Qt.checked.

checkbox pyqt

Related course:
Create Simple GUI Applications with Python and Qt

Checkbox example

The example below creates a checkbox which calls the method clickBox when toggled.

import sys
from PyQt5 import QtCore, QtWidgets
from PyQt5.QtWidgets import QMainWindow, QLabel, QCheckBox, QWidget
from PyQt5.QtCore import QSize

class ExampleWindow(QMainWindow):
def __init__(self):
QMainWindow.__init__(self)

self.setMinimumSize(QSize(140, 40))
self.setWindowTitle("Checkbox")

self.b = QCheckBox("Awesome?",self)
self.b.stateChanged.connect(self.clickBox)
self.b.move(20,20)
self.b.resize(320,40)

def clickBox(self, state):

if state == QtCore.Qt.Checked:
print('Checked')
else:
print('Unchecked')

if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
mainWin = ExampleWindow()
mainWin.show()
sys.exit( app.exec_() )
Previous Post Next Post

Cookie policy | Privacy policy | ©

PyQt