In this PyQt6 lesson we are going to learn about How to Create QCheckBox in PyQt6, first we will learn that how you can use coding for creating QCheckBox, after that we will use Qt Designer for creating QCheckBox in PyQt6.
This is the complete code for this lesson.
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 77 78 79 80 |
from PyQt6.QtWidgets import QApplication, QWidget,QLabel,\ QVBoxLayout ,QCheckBox, QHBoxLayout from PyQt6.QtGui import QIcon, QFont import sys class Window(QWidget): def __init__(self): super().__init__() self.setWindowTitle("PyQt6 CheckBox - Geekscoders.com") self.setWindowIcon(QIcon("qt.png")) self.setGeometry(500,200, 500,400) self.create_checkbox() def create_checkbox(self): # this is our hboxlayout hbox = QHBoxLayout() # these are checkboxes self.check1 = QCheckBox("Python") self.check1.setFont(QFont("Times New Roman", 13)) self.check1.toggled.connect(self.item_selected) hbox.addWidget(self.check1) self.check2 = QCheckBox("C++") self.check2.setFont(QFont("Times New Roman", 13)) self.check2.toggled.connect(self.item_selected) hbox.addWidget(self.check2) self.check3 = QCheckBox("Java") self.check3.setFont(QFont("Times New Roman", 13)) self.check3.toggled.connect(self.item_selected) hbox.addWidget(self.check3) self.check4 = QCheckBox("C#") self.check4.setFont(QFont("Times New Roman", 13)) self.check4.toggled.connect(self.item_selected) hbox.addWidget(self.check3) 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() if self.check4.isChecked(): value = self.check4.text() self.label.setText("You have selected : " + value) app = QApplication(sys.argv) window = Window() window.show() sys.exit(app.exec()) |
In this example we want to use two layouts first one is a QHBoxLayout, in the QHBoxLayout we want to add our three checkboxes.
1 |
hbox = QHBoxLayout() |
You can use QCheckBox class for creating of the checkboxes, and you need to just add the title for the checkbox.
1 |
self.check1 = QCheckBox("Python") |
You can set the font size of QCheckBox using this code.
1 |
self.check2.setFont(QFont("Times New Roman", 13)) |
In here we have connected the toggled() signal of the QCheckBox with the item_selected() method that we will create.
1 |
self.check1.toggled.connect(self.item_selected) |
Also you need to add your checkbox in the QHBoxLayout using this code.
1 |
hbox.addWidget(self.check2) |
In here we have created QVBoxLayout with QLabel.
1 2 3 4 |
vbox = QVBoxLayout() # we have created a label in here self.label = QLabel("Hello") self.label.setFont(QFont("Sanserif", 15)) |
And this is the method that we have already connected with toggled() signal of the checkbox. basically in this method we are going to check the value of the checkboxes.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
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() if self.check4.isChecked(): value = self.check4.text() self.label.setText("You have selected : " + value) |
Run the complete code and this is the result.
Now we want to use Qt Designer for creating of the QCheckBox, Open your Qt Designer, you can just write pyqt5designer in your terminal, after opening the Qt Designer you need to create QWidget window, now we need to add our widgets in Qt Designer.
- Add three QCheckBox and make the layout horizontally
- Add a QLabel, after that make the main window layout vertically
After completing the design you need to save the .ui file, iam going to name it CheckBox.ui, for converting you need to use pyuic6 module. pyuic6 module is located in the Scripts folder of your Python installation, run this command for converting in your terminal.
1 |
pyuic6 -x CheckBox.ui -o mycheckbox.py |
This is the converted code.
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 77 78 79 80 81 82 83 84 85 |
from PyQt6 import QtCore, QtGui, QtWidgets class Ui_Form(object): def setupUi(self, Form): Form.setObjectName("Form") Form.resize(352, 176) self.verticalLayout = QtWidgets.QVBoxLayout(Form) self.verticalLayout.setObjectName("verticalLayout") self.horizontalLayout = QtWidgets.QHBoxLayout() self.horizontalLayout.setObjectName("horizontalLayout") self.checkBox = QtWidgets.QCheckBox(Form) font = QtGui.QFont() font.setPointSize(12) font.setBold(True) font.setWeight(75) self.checkBox.setFont(font) self.checkBox.setObjectName("checkBox") self.horizontalLayout.addWidget(self.checkBox) self.checkBox.toggled.connect(self.item_selected) self.checkBox_2 = QtWidgets.QCheckBox(Form) font = QtGui.QFont() font.setPointSize(12) font.setBold(True) font.setWeight(75) self.checkBox_2.setFont(font) self.checkBox_2.setObjectName("checkBox_2") self.horizontalLayout.addWidget(self.checkBox_2) self.checkBox_2.toggled.connect(self.item_selected) self.checkBox_3 = QtWidgets.QCheckBox(Form) font = QtGui.QFont() font.setPointSize(12) font.setBold(True) font.setWeight(75) self.checkBox_3.setFont(font) self.checkBox_3.setObjectName("checkBox_3") self.horizontalLayout.addWidget(self.checkBox_3) self.verticalLayout.addLayout(self.horizontalLayout) self.checkBox_3.toggled.connect(self.item_selected) self.label = QtWidgets.QLabel(Form) font = QtGui.QFont() font.setPointSize(14) font.setBold(True) font.setWeight(75) self.label.setFont(font) self.label.setText("") self.label.setObjectName("label") self.verticalLayout.addWidget(self.label) self.retranslateUi(Form) QtCore.QMetaObject.connectSlotsByName(Form) def item_selected(self): value = "" # check for the check box value if self.checkBox.isChecked(): value = self.checkBox.text() if self.checkBox_2.isChecked(): value = self.checkBox_2.text() if self.checkBox_3.isChecked(): value = self.checkBox_3.text() self.label.setText("You have selected : " + value) def retranslateUi(self, Form): _translate = QtCore.QCoreApplication.translate Form.setWindowTitle(_translate("Form", "Form")) self.checkBox.setText(_translate("Form", "Python")) self.checkBox_2.setText(_translate("Form", "Java")) self.checkBox_3.setText(_translate("Form", "C#")) if __name__ == "__main__": import sys app = QtWidgets.QApplication(sys.argv) Form = QtWidgets.QWidget() ui = Ui_Form() ui.setupUi(Form) Form.show() sys.exit(app.exec()) |
We have added some new codes in our converted file, the first one is the toggled() signal for our QCheckBox, and we have connected this with the method.
1 |
self.checkBox_3.toggled.connect(self.item_selected) |
And also this is the method that we have connected with the toggled() signal of pyqt6 checkbox.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
def item_selected(self): value = "" # check for the check box value if self.checkBox.isChecked(): value = self.checkBox.text() if self.checkBox_2.isChecked(): value = self.checkBox_2.text() if self.checkBox_3.isChecked(): value = self.checkBox_3.text() self.label.setText("You have selected : " + value) |
Run the complete code and this is the result.