In this lesson we are going to learn How to Create CheckBox in PyQt6 & Python, A QCheckBox is an option button that can be checked or unchecked. Checkboxes are typically used to represent features in an application that can be enabled or disabled without affecting others. Whenever a checkbox is checked, it emits the signal stateChanged(). Connect to this signal if you want to trigger an action each time the checkbox changes state. also you can use isChecked() method to query whether or not a checkbox is checked. mostly checkbox is used when you want the user to select one or more than one option from a set of options. In such situations, you need to make use of checkboxes.
This is the complete code for How to Create CheckBox in PyQt6 & Python
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 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
from PyQt6.QtWidgets import QApplication, QWidget, QCheckBox,QVBoxLayout, QLabel,QHBoxLayout from PyQt6.QtGui import QIcon,QFont from PyQt6.QtCore import QSize from PyQt6.QtCore import Qt import sys class Window(QWidget): def __init__(self): super().__init__() self.setGeometry(200, 200, 700, 400) self.setWindowTitle("PyQt6 QCheckBox") self.setWindowIcon(QIcon('py.png')) # this is our hboxlayout hbox = QHBoxLayout() # these are checkboxes self.check1 = QCheckBox("Python") self.check1.setIcon(QIcon('java.png')) self.check1.setIconSize(QSize(40, 40)) self.check1.setFont(QFont("Sanserif", 13)) self.check1.toggled.connect(self.item_selected) hbox.addWidget(self.check1) self.check2 = QCheckBox("JavaScript") self.check2.setIcon(QIcon('javascript.png')) self.check2.setIconSize(QSize(40, 40)) self.check2.setFont(QFont("Sanserif", 13)) self.check2.toggled.connect(self.item_selected) hbox.addWidget(self.check2) self.check3 = QCheckBox("Java") self.check3.setIcon(QIcon('java.png')) self.check3.setIconSize(QSize(40, 40)) self.check3.setFont(QFont("Sanserif", 13)) self.check3.toggled.connect(self.item_selected) hbox.addWidget(self.check3) # this is the vboxlayout vbox = QVBoxLayout() # we have created a label in here self.label = QLabel("Hello") self.label.setFont(QFont("Sanserif", 15)) # add the label in the vbox layout vbox.addWidget(self.label) # add the hbox layout in the vbox layout vbox.addLayout(hbox) # set the layout for the main window self.setLayout(vbox) def item_selected(self): value = "" # check for the check box value if self.check1.isChecked(): value = self.check1.text() if self.check2.isChecked(): value = self.check2.text() if self.check3.isChecked(): value = self.check3.text() self.label.setText("You have selected : " + value) app = QApplication(sys.argv) window = Window() window.show() sys.exit(app.exec()) |
- How to Use Qt Designer in PyQt and PyQt6
- Build Text to Speech App with Python & PyQt5
- How to Build GUI Window in PyQt6
- How to Show Image in PyQt6
- How to Create Calendar with Python & PyQt6
In here we have created the width and height also x and y position for the window.
1 |
self.setGeometry(200, 200, 700, 400) |
This is used for adding title to the window.
1 |
self.setWindowTitle("PyQt6 QCheckBox") |
In here we have created an icon, make sure that you have already added an icon to your
working directory.
1 |
self.setWindowIcon(QIcon('py.png')) |
You can use QCheckBox class for creating checkbox in pyqt6 & python.
1 |
self.check1 = QCheckBox("Python") |
You can use toggled signal to add functionally for the checkbox, and in here we have
connected the signal with item_selected() slot.
1 |
self.check3.toggled.connect(self.item_selected) |
And this is the method or slot that we have connected with toggled signal, in this method
we are getting the value from the check box and after that we set that value to the label.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
def item_selected(self): value = "" # check for the check box value if self.check1.isChecked(): value = self.check1.text() if self.check2.isChecked(): value = self.check2.text() if self.check3.isChecked(): value = self.check3.text() self.label.setText("You have selected : " + value) |
Run the complete code and this will be the result.
