Open In App

PyQt5 – Check box checked state depending upon another check box

Improve
Improve
Like Article
Like
Save
Share
Report

Sometimes while creating a GUI(Graphical User Interface) application there is a need to make lot of check boxes, and some check box depend upon the previous check box for example there are two following check box first check box is “Do you have Laptop ?” and second check box is “Your laptop has i7 Processor?” Here we can see that if first check box is true then only second check box can be true. So in order to over come this dependency we have to stop the second check box to get checked if first check box is unchecked. When the first check box get checked then only user can check the second check box.

In order to do this we have to do the following: 1. Create two check boxes. 2. Set second check box check-able state to False i.e by default it can’t get checked. 3. Add action to first check box i.e when state of first check box changes call the associated method with it. 4. Inside the action method check if the first check box is checked. 5. If first check box is checked then make second check box check-able state to True i.e now it can get checked. 6. If first check box is unchecked make second check box state to uncheck and make it uncheck-able.

Below is the implementation. 

Python3




# importing libraries
from PyQt5.QtWidgets import *
from PyQt5 import QtCore, QtGui
from PyQt5.QtGui import *
from PyQt5.QtCore import *
import sys
 
 
class Window(QMainWindow):
 
    def __init__(self):
        super().__init__()
 
        # setting title
        self.setWindowTitle("Python ")
 
        # setting geometry
        self.setGeometry(100, 100, 600, 400)
 
        # calling method
        self.UiComponents()
 
        # showing all the widgets
        self.show()
 
    # method for widgets
    def UiComponents(self):
 
        # creating the check-box
        self.checkbox1 = QCheckBox('Geek ?', self)
 
        # setting geometry of check box
        self.checkbox1.setGeometry(200, 150, 100, 40)
 
        # adding action to check box
        self.checkbox1.stateChanged.connect(self.allow)
 
        # creating the check-box
        self.checkbox2 = QCheckBox('Geeky Geek ?', self)
 
        # setting geometry of check box
        self.checkbox2.setGeometry(200, 180, 100, 40)
 
        # stopping check box to get checked
        self.checkbox2.setCheckable(False)
 
 
    # method called by checkbox1
    def allow(self):
         
        # checking if checkbox1 is checked ?
        if self.checkbox1.isChecked():
             
            # allow second check box to get checked
            self.checkbox2.setCheckable(True)
 
        # first check box is unchecked
        else:
             
            # make second check box unchecked
            self.checkbox2.setCheckState(0)
             
            # make second check box uncheckable
            self.checkbox2.setCheckable(False)
 
 
# create pyqt5 app
App = QApplication(sys.argv)
 
# create the instance of our Window
window = Window()
 
# start the app
sys.exit(App.exec())


Output :



Last Updated : 14 Jan, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads