pythonpyqtpyqt4qradiobutton

Disable and enable radiobuttons from another radiobutton in PyQt4 Python


I have 3 radiobutton groups in forms of Yes or No. I want to disable the last 2 radiobutton groups if the first radiobutton is "No" and enable them when the first radiobutton is "Yes". I can make the enabled or disabled by default but toggling Yes/No from the first group doesn't change the result in the second and third group.

Here is a summary of my code:

self.yes_radioButtonGroup1 = QtGui.QRadioButton(self.centralwidget)      
self.yes_radioButtonGroup1.setObjectName(_fromUtf8("yes_radioButtonGroup1"))
self.no_radioButtonGroup1 = QtGui.QRadioButton(self.centralwidget)
self.no_radioButtonGroup1.setObjectName(_fromUtf8("no_radioButtonGroup1"))
self.radioButtonGroup1= QtGui.QButtonGroup(MainWindow)   
self.radioButtonGroup1.setObjectName(_fromUtf8("radioButtonGroup1"))
self.radioButtonGroup1.addButton(self.yes_radioButtonGroup1)
self.radioButtonGroup1.addButton(self.no_radioButtonGroup1)

self.yes_radioButtonGroup2 = QtGui.QRadioButton(self.centralwidget)      
self.yes_radioButtonGroup2.setObjectName(_fromUtf8("yes_radioButtonGroup2"))
self.no_radioButtonGroup2 = QtGui.QRadioButton(self.centralwidget)
self.no_radioButtonGroup2.setObjectName(_fromUtf8("no_radioButtonGroup2"))
self.radioButtonGroup2= QtGui.QButtonGroup(MainWindow)   
self.radioButtonGroup2.setObjectName(_fromUtf8("radioButtonGroup2"))
self.radioButtonGroup2.addButton(self.yes_radioButtonGroup1)
self.radioButtonGroup2.addButton(self.no_radioButtonGroup1)

self.yes_radioButtonGroup3 = QtGui.QRadioButton(self.centralwidget)      
self.yes_radioButtonGroup3.setObjectName(_fromUtf8("yes_radioButtonGroup3"))
self.no_radioButtonGroup3 = QtGui.QRadioButton(self.centralwidget)
self.no_radioButtonGroup3.setObjectName(_fromUtf8("no_radioButtonGroup3"))
self.radioButtonGroup3= QtGui.QButtonGroup(MainWindow)   
self.radioButtonGroup3.setObjectName(_fromUtf8("radioButtonGroup3"))
self.radioButtonGroup3.addButton(self.yes_radioButtonGroup3)
self.radioButtonGroup3.addButton(self.no_radioButtonGroup3)


if self.yes_radioButtonGroup1.isChecked() == True:
        self.yes_radioButtonGroup2.setEnabled(True)
        self.no_radioButtonGroup2.setEnabled(True)
        self.yes_radioButtonGroup3.setEnabled(True)
        self.no_radioButtonGroup3.setEnabled(True)

elif self.no_radioButtonGroup1.isChecked() == True:
        self.yes_radioButtonGroup2.setEnabled(False)
        self.no_radioButtonGroup2.setEnabled(False)
        self.yes_radioButtonGroup3.setEnabled(False)
        self.no_radioButtonGroup3.setEnabled(False)

I was wondering if anybody knows how to dynamically enable or disable the last two radiobutton group when values from the first radiobutton changes?


Solution

  • Since the buttons are exclusive enough to monitor one of them, so the appropriate signal is toggled() that sends the information if the button is checked or not.

        self.yes_radioButtonGroup1 = QtGui.QRadioButton(self.centralwidget)      
        self.yes_radioButtonGroup1.setObjectName(_fromUtf8("yes_radioButtonGroup1"))
        self.no_radioButtonGroup1 = QtGui.QRadioButton(self.centralwidget)
        self.no_radioButtonGroup1.setObjectName(_fromUtf8("no_radioButtonGroup1"))
        self.radioButtonGroup1= QtGui.QButtonGroup(MainWindow)   
        self.radioButtonGroup1.setObjectName(_fromUtf8("radioButtonGroup1"))
        self.radioButtonGroup1.addButton(self.yes_radioButtonGroup1)
        self.radioButtonGroup1.addButton(self.no_radioButtonGroup1)
    
        self.yes_radioButtonGroup2 = QtGui.QRadioButton(self.centralwidget)      
        self.yes_radioButtonGroup2.setObjectName(_fromUtf8("yes_radioButtonGroup2"))
        self.no_radioButtonGroup2 = QtGui.QRadioButton(self.centralwidget)
        self.no_radioButtonGroup2.setObjectName(_fromUtf8("no_radioButtonGroup2"))
        self.radioButtonGroup2= QtGui.QButtonGroup(MainWindow)   
        self.radioButtonGroup2.setObjectName(_fromUtf8("radioButtonGroup2"))
        self.radioButtonGroup2.addButton(self.yes_radioButtonGroup1)
        self.radioButtonGroup2.addButton(self.no_radioButtonGroup1)
    
        self.yes_radioButtonGroup3 = QtGui.QRadioButton(self.centralwidget)      
        self.yes_radioButtonGroup3.setObjectName(_fromUtf8("yes_radioButtonGroup3"))
        self.no_radioButtonGroup3 = QtGui.QRadioButton(self.centralwidget)
        self.no_radioButtonGroup3.setObjectName(_fromUtf8("no_radioButtonGroup3"))
        self.radioButtonGroup3= QtGui.QButtonGroup(MainWindow)   
        self.radioButtonGroup3.setObjectName(_fromUtf8("radioButtonGroup3"))
        self.radioButtonGroup3.addButton(self.yes_radioButtonGroup3)
        self.radioButtonGroup3.addButton(self.no_radioButtonGroup3)
    
        self.yes_radioButtonGroup1.toggled(self.on_yes_checked)
        # set initial state
        self.on_yes_checked(self.yes_radioButtonGroup1.isChecked())
    
    def on_yes_checked(self, checked):
        self.yes_radioButtonGroup2.setEnabled(checked)
        self.no_radioButtonGroup2.setEnabled(checked)
        self.yes_radioButtonGroup3.setEnabled(checked)
        self.no_radioButtonGroup3.setEnabled(checked)