PyQt5 checkbox QCheckBox

by Atakan

Hi,
It is a general element about the use of checkbox element, which can be used in almost every application.
I will simply make an example.

You have seen the checkbox like “Do you accept the contract in the member registration forms?” I will do something similar.

This is how we create a checkbox

self.cbox = QCheckBox("I accept the Terms and Conditions and the Data policy")

I will then check in this way whether this is selected or not.

self.cbox.isChecked()

Result

Source code

from PyQt5.QtWidgets import *
import sys

W_WIDTH = 400
W_HEIGHT = 250
W_X = 400
W_Y = 150

class MainFrame(QWidget):
    def __init__(self):
        super().__init__()
        self.init()

    def init(self):

        self.text_edit = QPlainTextEdit("Rule 1 : .....\nRule 2 : .....\nRule 3 : .....")

        self.cbox = QCheckBox("I accept the Terms and Conditions and the Data policy")
        self.register_button = QPushButton("Register")

        #result label
        self.result_label = QLabel("")

        my_vertical_layout = QVBoxLayout()
        my_vertical_layout.addWidget(self.text_edit)
        my_vertical_layout.addWidget(self.cbox)
        my_vertical_layout.addWidget(self.register_button)
        my_vertical_layout.addWidget(self.result_label)

        self.register_button.clicked.connect(self.controlAgreement)


        self.setGeometry(W_WIDTH, W_HEIGHT, W_X, W_Y)
        self.setLayout(my_vertical_layout)
        self.setWindowTitle("www.langpy.com | Python GUI Tutorial")

        self.show()


    def controlAgreement(self):
        if self.cbox.isChecked():
            self.result_label.setText("Registration completed successfully")
        else:
            self.result_label.setText("You must accept the agreement")


app = QApplication(sys.argv)
win = MainFrame()
sys.exit(app.exec_())

You may also like

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. OK Read More