在PyQt4 Python中禁用并启用另一个radiobutton的radiobutton

时间:2018-04-19 20:25:26

标签: python pyqt pyqt4 qradiobutton

我有3个无线电按钮组,形式为是或否。如果第一个无线电按钮为“否”,我想禁用最后2个无线电按钮组,并在第一个无线电按钮为“是”时启用它们。我可以默认启用或禁用,但是从第一组切换是/否不会更改第二组和第三组中的结果。

以下是我的代码摘要:

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)

我想知道当第一个radiobutton的值发生变化时,是否有人知道如何动态启用或禁用最后两个radiobutton组?

1 个答案:

答案 0 :(得分:1)

由于这些按钮足以监视其中一个按钮,因此相应的信号为toggled(),如果选中该按钮则会发送信息。

    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)