{"id":4984,"date":"2022-09-21T01:00:02","date_gmt":"2022-09-21T01:00:02","guid":{"rendered":"https:\/\/www.pythontutorial.net\/?page_id=4984"},"modified":"2022-10-29T07:49:43","modified_gmt":"2022-10-29T07:49:43","slug":"pyqt-qcheckbox","status":"publish","type":"page","link":"https:\/\/www.pythontutorial.net\/pyqt\/pyqt-qcheckbox\/","title":{"rendered":"PyQt QCheckBox"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you&#8217;ll learn how to use the PyQt <code>QCheckBox<\/code> class to create a checkbox widget.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='introduction-to-the-pyqt-qcheckbox-widget'>Introduction to the PyQt QCheckBox widget <a href=\"#introduction-to-the-pyqt-qcheckbox-widget\" class=\"anchor\" id=\"introduction-to-the-pyqt-qcheckbox-widget\" title=\"Anchor for Introduction to the PyQt QCheckBox widget\">#<\/a><\/h2>\n\n\n\n<p>The <code>QCheckBox<\/code> class allows you to create a checkbox widget, which can be switched on or off. To create a checkbox using the <code>QCheckBox<\/code> class, you follow these steps:<\/p>\n\n\n\n<p>First, import the <code>QCheckBox<\/code> class:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\"><span class=\"hljs-keyword\">from<\/span> PyQt6.QtWidgets <span class=\"hljs-keyword\">import<\/span> QCheckBox<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Second, create a new instance of the <code>QCheckBox<\/code> class:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">checkbox = QCheckBox(text)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The following program shows a window that has a checkbox:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">import<\/span> sys\n<span class=\"hljs-keyword\">from<\/span> PyQt6.QtWidgets <span class=\"hljs-keyword\">import<\/span> QApplication, QWidget, QCheckBox, QGridLayout\n<span class=\"hljs-keyword\">from<\/span> PyQt6.QtCore <span class=\"hljs-keyword\">import<\/span> Qt\n\n\n<span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">MainWindow<\/span><span class=\"hljs-params\">(QWidget)<\/span>:<\/span>\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">__init__<\/span><span class=\"hljs-params\">(self, *args, **kwargs)<\/span>:<\/span>\n        super().__init__(*args, **kwargs)\n\n        self.setWindowTitle(<span class=\"hljs-string\">'PyQt QCheckBox'<\/span>)\n        self.setGeometry(<span class=\"hljs-number\">100<\/span>, <span class=\"hljs-number\">100<\/span>, <span class=\"hljs-number\">320<\/span>, <span class=\"hljs-number\">210<\/span>)\n\n        <span class=\"hljs-comment\"># create a grid layout<\/span>\n        layout = QGridLayout()\n        self.setLayout(layout)\n\n        <span class=\"hljs-comment\"># create a checkbox<\/span>\n        checkbox = QCheckBox(<span class=\"hljs-string\">'I agree'<\/span>, self)\n\n        layout.addWidget(checkbox, <span class=\"hljs-number\">0<\/span>, <span class=\"hljs-number\">0<\/span>, Qt.AlignmentFlag.AlignCenter)\n\n        <span class=\"hljs-comment\"># show the window<\/span>\n        self.show()\n\n\n<span class=\"hljs-keyword\">if<\/span> __name__ == <span class=\"hljs-string\">'__main__'<\/span>:\n    app = QApplication(sys.argv)\n    window = MainWindow()\n    sys.exit(app.exec())<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Note that the program uses the <code><a href=\"https:\/\/www.pythontutorial.net\/pyqt\/pyqt-qgridlayout\/\">QGridLayout<\/a><\/code> to place the check box on the window.<\/p>\n\n\n\n<p>Output:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"482\" height=\"361\" src=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2022\/09\/PyQt-QCheckBox.png\" alt=\"\" class=\"wp-image-4985\" srcset=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2022\/09\/PyQt-QCheckBox.png 482w, https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2022\/09\/PyQt-QCheckBox-300x225.png 300w\" sizes=\"auto, (max-width: 482px) 100vw, 482px\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\" id='the-statechanged-signal'>The stateChanged signal <a href=\"#the-statechanged-signal\" class=\"anchor\" id=\"the-statechanged-signal\" title=\"Anchor for The stateChanged signal\">#<\/a><\/h2>\n\n\n\n<p>A checkbox emits the <code>stateChanged<\/code> signal whenever you check or uncheck it. <\/p>\n\n\n\n<p>If you want to do something when the checkbox is checked or unchecked, you can connect a slot to the <code>stateChanged<\/code> signal. For example:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">checkbox = QCheckBox(<span class=\"hljs-string\">'I agree'<\/span>, self)\ncheckbox.stateChanged.connect(self.on_checkbox_changed)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The <code>stateChanged<\/code> signal sends a value that indicates whether the button is checked or unchecked. To check the state of a <code>QCheckBox<\/code>, you create a <code>Qt.CheckState<\/code> instance:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">state = Qt.CheckState(value)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>And compare it with one of three values:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>State<\/th><th>Meaning<\/th><\/tr><\/thead><tbody><tr><td><code>Qt.CheckState.Checked<\/code><\/td><td>Checked<\/td><\/tr><tr><td><code>Qt.CheckState.Unchecked<\/code><\/td><td>Unchecked<\/td><\/tr><tr><td><code>Qt.CheckState.PartiallyChecked<\/code><\/td><td>Partially checked<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"note\">Note that the <code>Qt.CheckState.PartiallyChecked<\/code> is used for a tristate checkbox that will be covered shortly.<\/p>\n\n\n\n<p>For example:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">on_checkbox_changed<\/span><span class=\"hljs-params\">(self, value)<\/span>:<\/span>\n    state = Qt.CheckState(value)\n    <span class=\"hljs-keyword\">if<\/span> state == Qt.CheckState.Checked:\n        print(<span class=\"hljs-string\">'Checked'<\/span>)\n    <span class=\"hljs-keyword\">elif<\/span> state == Qt.CheckState.Unchecked:\n        print(<span class=\"hljs-string\">'Unchecked'<\/span>)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Also, you can use the <code>isChecked()<\/code> method to check if a checkbox is checked.<\/p>\n\n\n\n<p>The following shows a complete program that displays a message in the console when a checkbox is checked or unchecked:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-7\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">import<\/span> sys\n<span class=\"hljs-keyword\">from<\/span> PyQt6.QtWidgets <span class=\"hljs-keyword\">import<\/span> QApplication, QWidget, QCheckBox, QGridLayout\n<span class=\"hljs-keyword\">from<\/span> PyQt6.QtCore <span class=\"hljs-keyword\">import<\/span> Qt\n\n\n<span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">MainWindow<\/span><span class=\"hljs-params\">(QWidget)<\/span>:<\/span>\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">__init__<\/span><span class=\"hljs-params\">(self, *args, **kwargs)<\/span>:<\/span>\n        super().__init__(*args, **kwargs)\n\n        self.setWindowTitle(<span class=\"hljs-string\">'PyQt QCheckBox'<\/span>)\n        self.setGeometry(<span class=\"hljs-number\">100<\/span>, <span class=\"hljs-number\">100<\/span>, <span class=\"hljs-number\">320<\/span>, <span class=\"hljs-number\">210<\/span>)\n\n        <span class=\"hljs-comment\"># create a grid layout<\/span>\n        layout = QGridLayout()\n        self.setLayout(layout)\n\n        <span class=\"hljs-comment\"># create a checkbox<\/span>\n        checkbox = QCheckBox(<span class=\"hljs-string\">'I agree'<\/span>, self)\n        checkbox.stateChanged.connect(self.on_checkbox_changed)\n\n        layout.addWidget(checkbox, <span class=\"hljs-number\">0<\/span>, <span class=\"hljs-number\">0<\/span>, Qt.AlignmentFlag.AlignCenter)\n\n        <span class=\"hljs-comment\"># show the window<\/span>\n        self.show()\n\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">on_checkbox_changed<\/span><span class=\"hljs-params\">(self, value)<\/span>:<\/span>\n        state = Qt.CheckState(value)\n        <span class=\"hljs-keyword\">if<\/span> state == Qt.CheckState.Checked:\n            print(<span class=\"hljs-string\">'Checked'<\/span>)\n        <span class=\"hljs-keyword\">elif<\/span> state == Qt.CheckState.Unchecked:\n            print(<span class=\"hljs-string\">'Unchecked'<\/span>)\n\n\n<span class=\"hljs-keyword\">if<\/span> __name__ == <span class=\"hljs-string\">'__main__'<\/span>:\n    app = QApplication(sys.argv)\n    window = MainWindow()\n    sys.exit(app.exec())<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-7\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\" id='setting-checked-or-unchecked-of-pyqt-qcheckbox-programmatically'>Setting checked or unchecked of PyQt QCheckBox programmatically <a href=\"#setting-checked-or-unchecked-of-pyqt-qcheckbox-programmatically\" class=\"anchor\" id=\"setting-checked-or-unchecked-of-pyqt-qcheckbox-programmatically\" title=\"Anchor for Setting checked or unchecked of PyQt QCheckBox programmatically\">#<\/a><\/h2>\n\n\n\n<p>The <code>QCheckBox<\/code> class has the <code>setChecked()<\/code> method that allows you to check or uncheck a checkbox programmatically.<\/p>\n\n\n\n<p>If you pass <code>True<\/code> to the <code>setChecked()<\/code> method, the checkbox will be checked. However, if you pass <code>False<\/code> to the <code>setCheck()<\/code> method, the checkbox will be unchecked.<\/p>\n\n\n\n<p>Also, you can use the <code>setCheckState()<\/code> method of the <code>QCheckBox<\/code> class to set the state of the checkbox. The <code>setCheckState()<\/code> method accepts one of three state values of the <code>Qt.CheckState<\/code> enum.<\/p>\n\n\n\n<p>The following program illustrates how to use the <code>setChecked()<\/code> method to check and uncheck a checkbox:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-8\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">import<\/span> sys\n<span class=\"hljs-keyword\">from<\/span> PyQt6.QtWidgets <span class=\"hljs-keyword\">import<\/span> QApplication, QWidget, QCheckBox, QPushButton, QGridLayout\n<span class=\"hljs-keyword\">from<\/span> PyQt6.QtCore <span class=\"hljs-keyword\">import<\/span> Qt\n\n\n<span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">MainWindow<\/span><span class=\"hljs-params\">(QWidget)<\/span>:<\/span>\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">__init__<\/span><span class=\"hljs-params\">(self, *args, **kwargs)<\/span>:<\/span>\n        super().__init__(*args, **kwargs)\n\n        self.setWindowTitle(<span class=\"hljs-string\">'PyQt QCheckBox'<\/span>)\n        self.setGeometry(<span class=\"hljs-number\">100<\/span>, <span class=\"hljs-number\">100<\/span>, <span class=\"hljs-number\">320<\/span>, <span class=\"hljs-number\">210<\/span>)\n\n        <span class=\"hljs-comment\"># create a grid layout<\/span>\n        layout = QGridLayout()\n        self.setLayout(layout)\n\n        <span class=\"hljs-comment\"># create a checkbox<\/span>\n        self.checkbox = QCheckBox(<span class=\"hljs-string\">'I agree'<\/span>, self)\n\n        check_button = QPushButton(<span class=\"hljs-string\">'Check'<\/span>, self)\n        check_button.clicked.connect(self.check)\n\n        uncheck_button = QPushButton(<span class=\"hljs-string\">'Uncheck'<\/span>, self)\n        uncheck_button.clicked.connect(self.uncheck)\n\n        layout.addWidget(self.checkbox, <span class=\"hljs-number\">0<\/span>, <span class=\"hljs-number\">0<\/span>, <span class=\"hljs-number\">0<\/span>, <span class=\"hljs-number\">2<\/span>,\n                         Qt.AlignmentFlag.AlignCenter)\n        layout.addWidget(check_button, <span class=\"hljs-number\">1<\/span>, <span class=\"hljs-number\">0<\/span>)\n        layout.addWidget(uncheck_button, <span class=\"hljs-number\">1<\/span>, <span class=\"hljs-number\">1<\/span>)\n\n        <span class=\"hljs-comment\"># show the window<\/span>\n        self.show()\n\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">check<\/span><span class=\"hljs-params\">(self)<\/span>:<\/span>\n        self.checkbox.setChecked(<span class=\"hljs-literal\">True<\/span>)\n\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">uncheck<\/span><span class=\"hljs-params\">(self)<\/span>:<\/span>\n        self.checkbox.setChecked(<span class=\"hljs-literal\">False<\/span>)\n\n\n<span class=\"hljs-keyword\">if<\/span> __name__ == <span class=\"hljs-string\">'__main__'<\/span>:\n    app = QApplication(sys.argv)\n    window = MainWindow()\n    sys.exit(app.exec())<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-8\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Output:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"482\" height=\"361\" src=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2022\/09\/PyQt-QCheckBox-stateChanged-Signal.png\" alt=\"\" class=\"wp-image-4986\" srcset=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2022\/09\/PyQt-QCheckBox-stateChanged-Signal.png 482w, https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2022\/09\/PyQt-QCheckBox-stateChanged-Signal-300x225.png 300w\" sizes=\"auto, (max-width: 482px) 100vw, 482px\" \/><\/figure>\n\n\n\n<p>How it works.<\/p>\n\n\n\n<p>First, create a checkbox and add it to the attribute of the class:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-9\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">self.checkbox = QCheckBox(<span class=\"hljs-string\">'I agree'<\/span>, self)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-9\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>By making the checkbox an attribute of the class, we can reference it in other methods within the same class.<\/p>\n\n\n\n<p>Second, create two <code><a href=\"https:\/\/www.pythontutorial.net\/pyqt\/pyqt-qpushbutton\/\">QPushButton<\/a><\/code> widgets and connect each of them to the <code>check()<\/code> and <code>uncheck()<\/code> methods:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-10\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">check_button = QPushButton(<span class=\"hljs-string\">'Check'<\/span>, self)\ncheck_button.clicked.connect(self.check)\n\nuncheck_button = QPushButton(<span class=\"hljs-string\">'Uncheck'<\/span>, self)\nuncheck_button.clicked.connect(self.uncheck)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-10\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Third, call the <code>setChecked()<\/code> method with True to check the checkbox in the <code>check()<\/code> method:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-11\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">check<\/span><span class=\"hljs-params\">(self)<\/span>:<\/span>\n    self.checkbox.setChecked(<span class=\"hljs-literal\">True<\/span>)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-11\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Finally, call the <code>setChecked()<\/code> method with False to uncheck the checkbox in the <code>uncheck()<\/code> method:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-12\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">uncheck<\/span><span class=\"hljs-params\">(self)<\/span>:<\/span>\n    self.checkbox.setChecked(<span class=\"hljs-literal\">False<\/span>)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-12\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\" id='creating-a-tristate-checkbox'>Creating a tristate checkbox <a href=\"#creating-a-tristate-checkbox\" class=\"anchor\" id=\"creating-a-tristate-checkbox\" title=\"Anchor for Creating a tristate checkbox\">#<\/a><\/h2>\n\n\n\n<p>Besides checked and unchecked, a <code>QCheckBox<\/code> supports the third state that indicates &#8220;no change&#8221;. In this case, a checkbox has three states: <\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Checked<\/li><li>Unchecked<\/li><li>Partially checked<\/li><\/ul>\n\n\n\n<p>In practice, you use a tristate checkbox to give the user the option of neither checking nor unchecking the checkbox.<\/p>\n\n\n\n<p>To create a tristate checkbox, you use the <code>setTristate()<\/code> to True:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-13\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">checkbox.setTristate(<span class=\"hljs-literal\">True<\/span>)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-13\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The following program shows a tristate checkbox:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-14\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">import<\/span> sys\n<span class=\"hljs-keyword\">from<\/span> PyQt6.QtWidgets <span class=\"hljs-keyword\">import<\/span> QApplication, QWidget, QCheckBox,  QGridLayout\n<span class=\"hljs-keyword\">from<\/span> PyQt6.QtCore <span class=\"hljs-keyword\">import<\/span> Qt\n\n\n<span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">MainWindow<\/span><span class=\"hljs-params\">(QWidget)<\/span>:<\/span>\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">__init__<\/span><span class=\"hljs-params\">(self, *args, **kwargs)<\/span>:<\/span>\n        super().__init__(*args, **kwargs)\n\n        self.setWindowTitle(<span class=\"hljs-string\">'PyQt QCheckBox'<\/span>)\n        self.setGeometry(<span class=\"hljs-number\">100<\/span>, <span class=\"hljs-number\">100<\/span>, <span class=\"hljs-number\">320<\/span>, <span class=\"hljs-number\">210<\/span>)\n\n        <span class=\"hljs-comment\"># create a grid layout<\/span>\n        layout = QGridLayout()\n        self.setLayout(layout)\n\n        <span class=\"hljs-comment\"># create a tristate checkbox<\/span>\n        self.checkbox = QCheckBox(<span class=\"hljs-string\">'A Tristate Checkbox'<\/span>, self)\n        self.checkbox.setTristate(<span class=\"hljs-literal\">True<\/span>)\n\n        layout.addWidget(self.checkbox, <span class=\"hljs-number\">0<\/span>, <span class=\"hljs-number\">0<\/span>, Qt.AlignmentFlag.AlignCenter)\n\n        <span class=\"hljs-comment\"># show the window<\/span>\n        self.show()\n\n\n<span class=\"hljs-keyword\">if<\/span> __name__ == <span class=\"hljs-string\">'__main__'<\/span>:\n    app = QApplication(sys.argv)\n    window = MainWindow()\n    sys.exit(app.exec())<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-14\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Output:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"482\" height=\"361\" src=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2022\/09\/PyQt-QCheckBox-Tristate-checkbox-1.png\" alt=\"\" class=\"wp-image-4988\" srcset=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2022\/09\/PyQt-QCheckBox-Tristate-checkbox-1.png 482w, https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2022\/09\/PyQt-QCheckBox-Tristate-checkbox-1-300x225.png 300w\" sizes=\"auto, (max-width: 482px) 100vw, 482px\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\" id='summary'>Summary <a href=\"#summary\" class=\"anchor\" id=\"summary\" title=\"Anchor for Summary\">#<\/a><\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li>Use the <code>QCheckbox<\/code> class to create a checkbox widget.<\/li><li>The <code>stateChanged<\/code> signal is emitted when the checkbox is checked or unchecked.<\/li><li>Use the <code>setChecked()<\/code> or <code>setState()<\/code> method to check or uncheck a checkbox programmatically.<\/li><li>Use <code>setTristate()<\/code> method to create a tristate checkbox.<\/li><\/ul>\n<div class=\"helpful-block-content\" data-title=\"\">\n\t<header>\n\t\t<div class=\"wth-question\">Was this tutorial helpful ?<\/div>\n\t\t<div class=\"wth-thumbs\">\n\t\t\t<button\n\t\t\t\tdata-post=\"4984\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pythontutorial.net\/pyqt\/pyqt-qcheckbox\/\"\n\t\t\t\tdata-post-title=\"PyQt QCheckBox\"\n\t\t\t\tdata-response=\"1\"\n\t\t\t\tclass=\"wth-btn-rounded wth-yes-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t\tclass=\"feather feather-thumbs-up block w-full h-full\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M14 9V5a3 3 0 0 0-3-3l-4 9v11h11.28a2 2 0 0 0 2-1.7l1.38-9a2 2 0 0 0-2-2.3zM7 22H4a2 2 0 0 1-2-2v-7a2 2 0 0 1 2-2h3\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> Yes <\/span>\n\t\t\t<\/button>\n\n\t\t\t<button\n\t\t\t\tdata-response=\"0\"\n\t\t\t\tdata-post=\"4984\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pythontutorial.net\/pyqt\/pyqt-qcheckbox\/\"\n\t\t\t\tdata-post-title=\"PyQt QCheckBox\"\n\t\t\t\tclass=\"wth-btn-rounded wth-no-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M10 15v4a3 3 0 0 0 3 3l4-9V2H5.72a2 2 0 0 0-2 1.7l-1.38 9a2 2 0 0 0 2 2.3zm7-13h2.67A2.31 2.31 0 0 1 22 4v7a2.31 2.31 0 0 1-2.33 2H17\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> No <\/span>\n\t\t\t<\/button>\n\t\t<\/div>\n\t<\/header>\n\n\t<div class=\"wth-form hidden\">\n\t\t<div class=\"wth-form-wrapper\">\n\t\t\t<div class=\"wth-title\"><\/div>\n\t\t\t<textarea class=\"wth-message\"><\/textarea>\n\t\t\t<input type=\"button\" name=\"wth-submit\" class=\"wth-btn wth-btn-submit\" id=\"wth-submit\" \/>\n\t\t\t<input type=\"button\" class=\"wth-btn wth-btn-cancel\" value=\"Cancel\" \/>\n\t\t<\/div>\n\t<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, you&#8217;ll learn how to use the PyQt QCheckBox widget to create a checkbox.<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":4862,"menu_order":9,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-4984","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/4984","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/comments?post=4984"}],"version-history":[{"count":0,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/4984\/revisions"}],"up":[{"embeddable":true,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/4862"}],"wp:attachment":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/media?parent=4984"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}