Skip to content
Related Articles

Related Articles

Improve Article

PyQt5 – Selecting any one check box among group of check boxes

  • Last Updated : 22 Apr, 2020

When we make a form and there are multiple check boxes. We can select some of them or only one of them as they are contradictory to each other. So the GUI (Graphical User Interface) stops us to selecting more than one check box, if we select more than one check box it makes other boxes unchecked.

In this article we will see how to make a group of check boxes such that we can select any one of them. If we try to select the other check box it will automatically make other check boxes uncheck.

In order to do this we have to do the following –

1. Create a group of check box.
2. For each check box connect a method such that every time state of check box get changed that method should get called.
3. That method should check if the state is checked, if the state is unchecked do nothing.
4. If the state is checked, check by which check box this method is called.
5. Make all other check boxes to unchecked.

Below is the implementation.

# importing libraries
import sys
from PyQt5.QtWidgets import * 
from PyQt5.QtCore import *
# main window class
class   Window(QMainWindow):
    # constructor
    def __init__(self):
        # calling the method for widgets
    def initUI(self):
        # creating check box
        self.checkBoxNone = QCheckBox("Don't know ?", self)
        # setting geometry
        self.checkBoxNone.setGeometry(200, 150, 100, 30)
        # creating check box
        self.checkBoxA = QCheckBox("Geek", self)
        # setting geometry
        self.checkBoxA.setGeometry(200, 180, 100, 30)
        # creating check box
        self.checkBoxB = QCheckBox(" Not a geek ?", self)
        # setting geometry
        self.checkBoxB.setGeometry(200, 210, 100, 30)
        # calling the uncheck method if any check box state is changed
        # setting window title
        # setting geometry of window
        self.setGeometry(100, 100, 600, 400)
        # showing all the widgets
    # uncheck method
    def uncheck(self, state):
        # checking if state is checked
        if state == Qt.Checked:
            # if first check box is selected
            if self.sender() == self.checkBoxNone:
                # making other check box to uncheck
            # if second check box is selected
            elif self.sender() == self.checkBoxA:
                # making other check box to uncheck
            # if third check box is selected
            elif self.sender() == self.checkBoxB:
                # making other check box to uncheck
# create pyqt5 app
App = QApplication(sys.argv)
# create the instance of our Window
window = Window()
# start the app

Output :

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course

My Personal Notes arrow_drop_up
Recommended Articles
Page :