如何检查是否从另一个类中检查了RadioButton?

时间:2019-11-08 23:53:18

标签: python pyqt5

我正在PyQt5中制作一个GUI,其中有多个选项卡。在一个选项卡中,用户选择一个单选按钮,并输入他们要输入的数据类型。

此信息更改了其他选项卡中可用的按钮,但我无法从原始选项卡类传递此信息。我想看看是否从另一个标签中选中了单选按钮,如果已选中,请启用一个按钮。

我有一个函数可以返回一个字符串,具体取决于选择哪个单选按钮,但是当我从另一个选项卡调用getDataType()函数时,出现错误“ AttributeError:模块'DataTab'没有属性'getDataType。 '“

我尝试将getDataType()函数移出DataTab类之外,但是由于单选按钮使用“自我”,所以我无法访问它们。

我也尝试过创建一个新类并从该函数调用该新类,但是后来我陷入了如何将信息获取到其他选项卡的困境。

因此,在此示例中,一旦用户在“数据”选项卡中选择了单选按钮,我就希望启用“分析”选项卡上的相应按钮。

取消注释对getDataType()的调用时,会出现以下错误:“ AttributeError:模块'DataTab'没有属性'getDataType'”

from PyQt5.QtWidgets import (QWidget, QVBoxLayout, QLabel, QTableWidget, QTableWidgetItem,
                             QLineEdit, QFileDialog, QRadioButton, QGroupBox, QPushButton,
                             QGridLayout, QButtonGroup, QApplication, QAbstractItemView,
                             QTabWidget)
from PyQt5.QtGui import QIcon
import sys, os


class TabPage(QTabWidget):
    def __init__(self):
        super().__init__()
        self.setStyleSheet('font: 15pt Tw Cen MT')
        self.show()
        # Creating the tabs here to have a reference
        self.tabWidget = QTabWidget()
        self.tabWidget.addTab(DataTab(), "Data Input")
        self.tabWidget.addTab(AnalysisTab(), "Analysis")

        self.layout = QVBoxLayout()
        self.layout.addWidget(self.tabWidget)
        self.setLayout(self.layout)

# The DataTab class holds all the GUI for the DataTab
class DataTab(QWidget):
    def __init__(self):
        super().__init__()
        self.app = QApplication(sys.argv)

        self.layout = QGridLayout()

        self.intervalRadioButton = QRadioButton("Interval")
        self.ordinalRadioButton = QRadioButton("Ordinal")
        self.frequencyRadioButton = QRadioButton("Frequency")

        self.submitButton = QPushButton("Submit Data")

        self.layout.addWidget(self.intervalRadioButton, 7, 0, 1, 3)
        self.layout.addWidget(self.ordinalRadioButton, 8, 0, 1, 3)
        self.layout.addWidget(self.frequencyRadioButton, 9, 0, 1, 3)
        self.setLayout(self.layout)
        self.show()


    def getDataType(self):
        if self.intervalRadioButton.isChecked():
            return "interval"
        elif self.ordinalRadioButton.isChecked():
            return "ordinal"
        elif self.frequencyRadioButton.isChecked():
            return "frequency"


class AnalysisTab(QWidget):
    def __init__(self):
        super().__init__()

        self.app = QApplication(sys.argv)

        self.createChooseIntervalGroup()
        self.createChooseOrdinalGroup()
        self.createChooseFrequencyGroup()

        self.layout = QGridLayout()
        self.layout.addWidget(self.ChooseIntervalGroup, 0, 1)
        self.layout.addWidget(self.ChooseOrdinalGroup, 1, 1)
        self.layout.addWidget(self.ChooseFrequencyGroup, 2, 1)
        self.setLayout(self.layout)
        self.show()

    # The right side of AnalysisTab containing the buttons for
    # analysis
    def createChooseIntervalGroup(self):
        self.ChooseIntervalGroup = QGroupBox("Tests for Interval Data")

        self.analyzeIntervalButton = QPushButton("Analyze")
        self.analyzeIntervalButton.setEnabled(False)

        # if DataTab.getDataType() != "interval":
        #     self.analyzeIntervalButton.setEnabled(True)

        self.layout = QGridLayout()
        self.layout.addWidget(self.analyzeIntervalButton, 1, 1)
        self.ChooseIntervalGroup.setLayout(self.layout)

    # The right side of AnalysisTab containing the buttons for
    # analysis
    def createChooseOrdinalGroup(self):
        self.ChooseOrdinalGroup = QGroupBox("Tests for Ordinal Data")

        self.analyzeOrdinalButton = QPushButton("Analyze")
        self.analyzeOrdinalButton.setEnabled(False)

        # if DataTab.getDataType() != "ordinal":
        #     self.analyzeIntervalButton.setEnabled(True)

        self.layout = QGridLayout()
        self.layout.addWidget(self.analyzeOrdinalButton, 1, 1)
        self.ChooseOrdinalGroup.setLayout(self.layout)

    # The right side of AnalysisTab containing the buttons for
    # analysis
    def createChooseFrequencyGroup(self):
        self.ChooseFrequencyGroup = QGroupBox("Tests for Frequency Data")

        self.analyzeFrequencyButton = QPushButton("Analyze")
        self.analyzeFrequencyButton.setEnabled(False)

        # if DataTab.getDataType() != "frequency":
        #     self.analyzeIntervalButton.setEnabled(True)

        self.layout = QGridLayout()
        self.layout.addWidget(self.analyzeFrequencyButton, 1, 1)
        self.ChooseFrequencyGroup.setLayout(self.layout)

def run():
    app = QApplication(sys.argv)
    tabPage = TabPage()
    tabPage.show()
    app.exec_()

run()

1 个答案:

答案 0 :(得分:2)

对于这种情况,可能的解决方案是使用信号通知QRadioButton的状态更改以启用或禁用按钮。

import sys

from PyQt5.QtGui import QIcon
from PyQt5.QtWidgets import (
    QWidget,
    QVBoxLayout,
    QRadioButton,
    QGroupBox,
    QPushButton,
    QGridLayout,
    QButtonGroup,
    QApplication,
    QTabWidget,
)


class Widget(QWidget):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.setStyleSheet("font: 15pt Tw Cen MT")

        self.data_tab = DataTab()
        self.analysis_tab = AnalysisTab()

        self.tabWidget = QTabWidget()
        self.tabWidget.addTab(self.data_tab, "Data Input")
        self.tabWidget.addTab(self.analysis_tab, "Analysis")

        layout = QVBoxLayout(self)
        layout.addWidget(self.tabWidget)

        self.data_tab.intervalRadioButton.toggled.connect(
            self.analysis_tab.analyzeIntervalButton.setEnabled
        )
        self.data_tab.ordinalRadioButton.toggled.connect(
            self.analysis_tab.analyzeOrdinalButton.setEnabled
        )
        self.data_tab.frequencyRadioButton.toggled.connect(
            self.analysis_tab.analyzeFrequencyButton.setEnabled
        )


class DataTab(QWidget):
    def __init__(self, parent=None):
        super().__init__(parent)
        layout = QGridLayout(self)
        self.intervalRadioButton = QRadioButton("Interval")
        self.ordinalRadioButton = QRadioButton("Ordinal")
        self.frequencyRadioButton = QRadioButton("Frequency")
        self.submitButton = QPushButton("Submit Data")
        layout.addWidget(self.intervalRadioButton, 7, 0, 1, 3)
        layout.addWidget(self.ordinalRadioButton, 8, 0, 1, 3)
        layout.addWidget(self.frequencyRadioButton, 9, 0, 1, 3)


class AnalysisTab(QWidget):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.createChooseIntervalGroup()
        self.createChooseOrdinalGroup()
        self.createChooseFrequencyGroup()

        layout = QGridLayout(self)
        layout.addWidget(self.ChooseIntervalGroup, 0, 1)
        layout.addWidget(self.ChooseOrdinalGroup, 1, 1)
        layout.addWidget(self.ChooseFrequencyGroup, 2, 1)

    def createChooseIntervalGroup(self):
        self.ChooseIntervalGroup = QGroupBox("Tests for Interval Data")
        self.analyzeIntervalButton = QPushButton("Analyze")
        self.analyzeIntervalButton.setEnabled(False)
        layout = QGridLayout(self.ChooseIntervalGroup)
        layout.addWidget(self.analyzeIntervalButton, 1, 1)

    def createChooseOrdinalGroup(self):
        self.ChooseOrdinalGroup = QGroupBox("Tests for Ordinal Data")
        self.analyzeOrdinalButton = QPushButton("Analyze")
        self.analyzeOrdinalButton.setEnabled(False)
        layout = QGridLayout(self.ChooseOrdinalGroup)
        layout.addWidget(self.analyzeOrdinalButton, 1, 1)

    def createChooseFrequencyGroup(self):
        self.ChooseFrequencyGroup = QGroupBox("Tests for Frequency Data")
        self.analyzeFrequencyButton = QPushButton("Analyze")
        self.analyzeFrequencyButton.setEnabled(False)
        layout = QGridLayout(self.ChooseFrequencyGroup)
        layout.addWidget(self.analyzeFrequencyButton, 1, 1)


def run():
    app = QApplication(sys.argv)
    tabPage = Widget()
    tabPage.show()
    app.exec_()


run()