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

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何检查是否从另一个类中检查了RadioButton?相关的知识,希望对你有一定的参考价值。

我正在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()
答案

这种情况的可能解决方案是使用信号通知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()

以上是关于如何检查是否从另一个类中检查了RadioButton?的主要内容,如果未能解决你的问题,请参考以下文章

当 VC 从另一个 VC 返回时,如何清除 UITableVIew 中的所有检查?

如何使用 FluentAssertions 检查对象是不是从另一个类继承?

如何检查在 C# 的全局类中找到的数组中是不是存在对象

如何从另一个子 td 元素检查父 tr 元素内的 td 元素的值

如何检查定义的方法?来自在类中使用的模块,其中包含模块后定义的方法

是否可以使用 C# 检查是否启用了硬件虚拟化?