具有启用另一个groupBox的radioButtons的GroupBox不会保持按钮状态

时间:2019-06-24 14:49:19

标签: python pyqt pyqt5 qt-designer

我有三个groupBox,每个都有两个radioButton。第一个框中的按钮 启用/禁用第二个groupBox。第二个按钮启用/禁用 第三个groupBox。

这应该是这样的。

第二个和第三个groupBox默认情况下处于禁用状态。 第一个框中的radioButton发送信号toggled(bool)以启用第二个 框。在那里,默认情况下单击了禁用第三个框的radioButton,第二个radioButton将信号toggled(bool)发送到第三个框以启用它。

实际上发生的是,当我启用第二个框时,第三个框 启用。当我在第二个框中切换按钮时,可以启用/禁用第三个框,但是当我再次从第一个框中禁用第二个框然后再次启用它时,无论在第二个框中单击哪个按钮,都将启用第三个框。

有什么作用?

公平游戏,这是一个示例(试图使它尽可能短地与Designer配合使用)(导入,头等舱和文件末尾的'if name == ...”包含额外的4个空格,以便代码显示为代码,请将其删除以运行):

from PyQt5 import QtCore, QtGui, QtWidgets

class Ui_MainWindow(object):
def setupUi(self, MainWindow):
    MainWindow.setObjectName("MainWindow")
    MainWindow.resize(800, 600)
    self.centralwidget = QtWidgets.QWidget(MainWindow)
    self.centralwidget.setObjectName("centralwidget")
    self.horizontalLayout = QtWidgets.QHBoxLayout(self.centralwidget)
    self.horizontalLayout.setObjectName("horizontalLayout")
    self.gridLayout = QtWidgets.QGridLayout()
    self.gridLayout.setObjectName("gridLayout")
    self.groupBox = QtWidgets.QGroupBox(self.centralwidget)
    self.groupBox.setObjectName("groupBox")
    self.horizontalLayout_3 = QtWidgets.QHBoxLayout(self.groupBox)
    self.horizontalLayout_3.setObjectName("horizontalLayout_3")
    self.horizontalLayout_2 = QtWidgets.QHBoxLayout()
    self.horizontalLayout_2.setObjectName("horizontalLayout_2")
    self.radioButton_2 = QtWidgets.QRadioButton(self.groupBox)
    self.radioButton_2.setChecked(True)
    self.radioButton_2.setObjectName("radioButton_2")
    self.horizontalLayout_2.addWidget(self.radioButton_2)
    self.radioButton = QtWidgets.QRadioButton(self.groupBox)
    self.radioButton.setObjectName("radioButton")
    self.horizontalLayout_2.addWidget(self.radioButton)
    self.horizontalLayout_3.addLayout(self.horizontalLayout_2)
    self.gridLayout.addWidget(self.groupBox, 0, 0, 1, 1)
    self.groupBox_2 = QtWidgets.QGroupBox(self.centralwidget)
    self.groupBox_2.setEnabled(False)
    self.groupBox_2.setObjectName("groupBox_2")
    self.horizontalLayout_5 = QtWidgets.QHBoxLayout(self.groupBox_2)
    self.horizontalLayout_5.setObjectName("horizontalLayout_5")
    self.horizontalLayout_4 = QtWidgets.QHBoxLayout()
    self.horizontalLayout_4.setObjectName("horizontalLayout_4")
    self.radioButton_3 = QtWidgets.QRadioButton(self.groupBox_2)
    self.radioButton_3.setChecked(True)
    self.radioButton_3.setObjectName("radioButton_3")
    self.horizontalLayout_4.addWidget(self.radioButton_3)
    self.radioButton_4 = QtWidgets.QRadioButton(self.groupBox_2)
    self.radioButton_4.setObjectName("radioButton_4")
    self.horizontalLayout_4.addWidget(self.radioButton_4)
    self.horizontalLayout_5.addLayout(self.horizontalLayout_4)
    self.gridLayout.addWidget(self.groupBox_2, 1, 0, 1, 1)
    self.groupBox_3 = QtWidgets.QGroupBox(self.centralwidget)
    self.groupBox_3.setEnabled(False)
    self.groupBox_3.setObjectName("groupBox_3")
    self.gridLayout.addWidget(self.groupBox_3, 2, 0, 1, 1)
    self.horizontalLayout.addLayout(self.gridLayout)
    MainWindow.setCentralWidget(self.centralwidget)
    self.menubar = QtWidgets.QMenuBar(MainWindow)
    self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 30))
    self.menubar.setObjectName("menubar")
    MainWindow.setMenuBar(self.menubar)
    self.statusbar = QtWidgets.QStatusBar(MainWindow)
    self.statusbar.setObjectName("statusbar")
    MainWindow.setStatusBar(self.statusbar)

    self.retranslateUi(MainWindow)
    self.radioButton.toggled['bool'].connect(self.groupBox_2.setEnabled)
    self.radioButton_4.toggled['bool'].connect(self.groupBox_3.setEnabled)
    self.radioButton_2.toggled['bool'].connect(self.groupBox_3.setDisabled)
    self.radioButton_3.toggled['bool'].connect(self.groupBox_3.setDisabled)
    QtCore.QMetaObject.connectSlotsByName(MainWindow)

def retranslateUi(self, MainWindow):
    _translate = QtCore.QCoreApplication.translate
    MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
    self.groupBox.setTitle(_translate("MainWindow", "whatever"))
    self.radioButton_2.setText(_translate("MainWindow", "Off"))
    self.radioButton.setText(_translate("MainWindow", "Sensors"))
    self.groupBox_2.setTitle(_translate("MainWindow", "Control method"))
    self.radioButton_3.setText(_translate("MainWindow", "On/Off"))
    self.radioButton_4.setText(_translate("MainWindow", "PID"))
    self.groupBox_3.setTitle(_translate("MainWindow", "PID settings"))


if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())

1 个答案:

答案 0 :(得分:0)

首先(这是最好的方式),这是一些非常丑陋的代码。我已将您的代码清理干净,并在Win10上将Python 3.7与pyqt5配合使用,效果很好,并且应该更容易理解。我希望这可以帮助您走上一条更有条理的发展道路。请记住,您经常必须返回并重新访问和/或重用您最好编写的旧代码,以使其真正一目了然的方式编写出来。

from sys import exit as sysExit

from PyQt5.QtCore    import *
from PyQt5.QtGui     import *
from PyQt5.QtWidgets import *

class BoxOne(QGroupBox):
    def __init__(self, CentrPane):
        QGroupBox.__init__(self, CentrPane)
      # Reference back to parent
        self.CntrPane = CentrPane

        self.setTitle('Main Controller')

        self.radBtnOff = QRadioButton('Off')
        self.radBtnOff.setChecked(True)
        self.radBtnOff.toggled['bool'].connect(self.BoxTwoOff)
        self.radBtnSnsr = QRadioButton('Sensors')
        self.radBtnSnsr.toggled['bool'].connect(self.BoxTwoOn)

        self.horizontalLayout = QHBoxLayout()
        self.horizontalLayout.addWidget(self.radBtnOff)
        self.horizontalLayout.addWidget(self.radBtnSnsr)

        self.setLayout(self.horizontalLayout)

    def BoxTwoOff(self):
      # I assume you did not mean to leave Box Three Enabled
        self.CntrPane.SecndBox.radBtnOnOff.setChecked(True)
        self.CntrPane.SecndBox.setDisabled(True)

    def BoxTwoOn(self):
        self.CntrPane.SecndBox.setEnabled(True)

class BoxTwo(QGroupBox):
    def __init__(self, CentrPane):
        QGroupBox.__init__(self, CentrPane)
      # Reference back to parent
        self.CntrPane = CentrPane

        self.setTitle('Control Method')
        self.setEnabled(False)

        self.radBtnOnOff = QRadioButton('On/Off')
        self.radBtnOnOff.setChecked(True)
        self.radBtnOnOff.toggled['bool'].connect(self.BoxThreeOff)
        self.radBtnPID = QRadioButton('PID')
        self.radBtnPID.toggled['bool'].connect(self.BoxThreeOn)

        self.horizontalLayout = QHBoxLayout()
        self.horizontalLayout.addWidget(self.radBtnOnOff)
        self.horizontalLayout.addWidget(self.radBtnPID)

        self.setLayout(self.horizontalLayout)

    def BoxThreeOff(self):
        self.CntrPane.ThirdBox.setDisabled(True)

    def BoxThreeOn(self):
        self.CntrPane.ThirdBox.setEnabled(True)

class BoxThree(QGroupBox):
    def __init__(self, CentrPane):
        QGroupBox.__init__(self, CentrPane)
      # Reference back to parent
        self.CntrPane = CentrPane

        self.setTitle('PID Settings')
        self.setEnabled(False)

        self.MyEditor = QTextEdit('Editorial')

        self.horizontalLayout = QHBoxLayout()
        self.horizontalLayout.addWidget(self.MyEditor)

        self.setLayout(self.horizontalLayout)

class CenterPanel(QWidget):
    def __init__(self, MainWin):
        QWidget.__init__(self)
      # Reference back to parent
        self.MainWin = MainWin

        self.FirstBox = BoxOne(self)
        self.SecndBox = BoxTwo(self)
        self.ThirdBox = BoxThree(self)

        self.gridLayout = QGridLayout()
        self.gridLayout.addWidget(self.FirstBox, 0, 0, 1, 1)
        self.gridLayout.addWidget(self.SecndBox, 1, 0, 1, 1)
        self.gridLayout.addWidget(self.ThirdBox, 2, 0, 1, 1)

        self.setLayout(self.gridLayout)

class MenuToolBar(QDockWidget):
    def __init__(self, MainWin):
        QDockWidget.__init__(self)
      # Reference back to parent
        self.MainWin = MainWin
        self.MainMenu = MainWin.menuBar()

class StatusBar(QDockWidget):
    def __init__(self, MainWin):
        QDockWidget.__init__(self)
      # Reference back to parent
        self.MainWin = MainWin
        self.MainMenu = MainWin.statusBar()

class UI_MainWindow(QMainWindow):
    def __init__(self, AppDesktop):
        super(UI_MainWindow, self).__init__(AppDesktop)
      # Reference back to parent
        self.MainDeskTop = AppDesktop

        self.setWindowTitle('Main Window')
      # Left, Top, Width, Height
        self.setGeometry(200, 200, 800, 600)

        self.CenterPane = CenterPanel(self)
        self.setCentralWidget(self.CenterPane)

        self.MenuToolBar = MenuToolBar(self)

        self.StatusBar = StatusBar(self)

if __name__ == '__main__':
    MainApp = QApplication([])

    MainGui = UI_MainWindow(MainApp.desktop())
    MainGui.show()

    sysExit(MainApp.exec_())