实例方法无法访问实例属性

时间:2019-06-27 20:38:02

标签: python python-3.x oop pyqt pyqt5

我正在使用Pyqt5创建桌面应用程序。我创建了一个ClassA类,该类创建ClassB的实例。 ClassA的实例方法执行ClassB的另一个实例方法。然后过程重复。 ClassB尝试从ClassC实例获取数据。 错误是:

'QWidgetItem' object has no attribute 'hLayout1'
AttributeError: type object 'ClassB' has no attribute 'hLayout1'

您能教我如何访问实例变量吗? 这是完整的代码。我尽量简化了。

from PyQt5 import QtCore, QtWidgets
from PyQt5.QtWidgets import QApplication, QMainWindow


class MainWindow(QMainWindow):

    def __init__(self):
        super(MainWindow, self).__init__()
        self.setGeometry(10, 35, 1580, 820)
        mainWidget = ClassA()
        self.setCentralWidget(mainWidget)


class ClassA(QtWidgets.QWidget):
    def __init__(self):
        super().__init__()

        vLayout1 = QtWidgets.QVBoxLayout()
        self.vLayout2 = QtWidgets.QVBoxLayout()
        hLayout1 = QtWidgets.QHBoxLayout()

        self.btnGetClassB = QtWidgets.QPushButton('New instance of Class B', self)
        self.btnGetData = QtWidgets.QPushButton('Get data from all classes', self)

        self.setLayout(vLayout1)
        vLayout1.addLayout(hLayout1)
        vLayout1.addLayout(self.vLayout2)
        self.vLayout2.addWidget(self.btnGetData)
        self.vLayout2.addWidget(self.btnGetClassB)


        def newClassB():
            newClass = ClassB()
            self.vLayout2.addWidget(newClass)


        self.btnGetClassB.clicked.connect(newClassB)
        self.btnGetData.clicked.connect(self.getInfoFromClasses)

    def getInfoFromClasses(self):
        data = []

        #connect with class b
        for index in range(self.vLayout2.count()):
            item = self.vLayout2.itemAt(index)

            itemData = ClassB.sendInfoToClassA(item)
            data.append(itemData)

        print(data)


class ClassB(QtWidgets.QWidget):
    def __init__(self):
        super().__init__()

        #GUI elements
        vLayout1 = QtWidgets.QVBoxLayout()
        self.hLayout1 = QtWidgets.QHBoxLayout()

        self.label = QtWidgets.QLabel('Class B instance')
        btnGetClassC = QtWidgets.QPushButton('New instance of Class C')

        #set components to layouts
        self.setLayout(vLayout1)
        vLayout1.addWidget(btnGetClassC)
        vLayout1.addWidget(self.label)
        vLayout1.addLayout(self.hLayout1)

        #add new class to layout
        def newClassC():
            newClass = ClassC()
            self.hLayout1.addWidget(newClass)

        btnGetClassC.clicked.connect(newClassC)


    def sendInfoToClassA(self):

        data = []

        #connect class c                # i also tried 'ClassB.hLayout1(self)' error: 'ClassB' has no attribute 'hLayout1'
        for index in range(self.hLayout1().count()): #here is the error. 'QWidgetItem' object has no attribute 'hLayout1'
            item = self.hLayout1.itemAt(index)

            classData = item.dataService()
            data.append(classData)

        data.append(self.label.text())

        return data


class ClassC(QtWidgets.QWidget):
    def __init__(self):
        super().__init__()

        vLayout1 = QtWidgets.QVBoxLayout()

        self.label = QtWidgets.QLabel('Class C instance')

        vLayout1.addWidget(self.label)
        self.setLayout(vLayout1)

        self.info = {'classC': self.label.text()}


if __name__ == "__main__":

    import sys
    app = QApplication([])
    window = MainWindow()
    window.show()   
    app.exec_()

0 个答案:

没有答案