'Main' object 在 parent Ui class "Ui_MainWindow" 和 child class 之间没有属性 'TabWidget' "Ui_dialog_List"

'Main' object has no attribute 'TabWidget' between parent Ui class "Ui_MainWindow" and child class "Ui_dialog_List"

根据下面的代码,当我尝试从 addLineDialoge 函数中获取 MainTabIndex 值时,它是 parent Main () class 的一部分,并尝试在child Dialoge_list class,出现 'AttributeError:' Main 'object has no attribute' MainTabIndex ' 错误。 请告诉我如何解决这个问题。

Parent class:

class Main(QMainWindow, Ui_MainWindow):
def __init__(self):
    super(Main, self).__init__()
    self.setupUi(self)
    self.addLineButt.clicked.connect(self.addLineDialoge)

def addLineDialoge(self):
    self.**MainTabIndex** = self.MainTab.currentIndex()
    self.**ListTabIndex** = self.ListTab.currentIndex()
    print(self.ListTabIndex)
    addList = Dialoge_list()
    addWaste = Dialoge_Waste()
    if self.MainTab.currentIndex() == 0:
        addList.exec()
    elif self.MainTab.currentIndex() == 1:
        addWaste.exec()

Child class:

class Dialoge_list(QDialog, Ui_dialog_List):
    def __init__(self):
        super(Dialoge_list, self).__init__()
        self.setupUi(self)
        self.addItemButt.clicked.connect(self.addListToBase)

    def addListToBase(self):
        numCell = self.numLine.text()
        thikCell = self.thikLine.text()
        matCell = self.matLine.text()
        sizeCell = self.sizeLine.text()
        quantityCell = self.quantityLine.text()
        dateinCell = self.dateinLine.text()
        applyCell = self.applyLine.text()
        noticeCell = self.noticeLine.text()
        list_tab = [(numCell,
                     thikCell,
                     matCell,
                     sizeCell,
                     quantityCell,
                     dateinCell,
                     applyCell,
                     noticeCell)]
        if "".__eq__(numCell) and "".__eq__(thikCell) and "".__eq__(matCell) and "".__eq__(
                sizeCell) and "".__eq__(quantityCell) and "".__eq__(dateinCell) and "".__eq__(
            applyCell) and "".__eq__(noticeCell):
            emptyError = EmptyErrorDialoge()
            emptyError.exec()

        elif Main().**MainTabIndex** == 0  and Main().**ListTabIndex** == 0:
             . . . . 

我找到了解决办法。 有必要在“Dialoge_list”classWas 的超级方法中进行以下更改:

class Dialoge_list(QDialog, Ui_dialog_List):
    def __init__(self):
        super(Dialoge_list, self).__init__()

变成:

class Dialoge_list(QDialog, Ui_dialog_List):
    def __init__(self, parent):
        super(Dialoge_list, self).__init__(parent)

还更改了 addListToBase function.Was:

def addLineDialoge(self):
    self.MainTabIndex = self.MainTab.currentIndex()
    self.ListTabIndex = self.ListTab.currentIndex()
    print(self.ListTabIndex)
    addList = Dialoge_list()
. . .

成为

def addLineDialoge(self):
    self.MainTabIndex = self.MainTab.currentIndex()
    self.ListTabIndex = self.ListTab.currentIndex()
    print(self.ListTabIndex)
    addList = Dialoge_list(self)
. . .

之后在childclass中可以调用MainTabIndex和ListTabIndex函数,等这个你只需要添加方法parent()就可以了。是:

class Dialoge_list(QDialog, Ui_dialog_List):
    def __init__(self):
        super(Dialoge_list, self).__init__()
        self.setupUi(self)
        self.addItemButt.clicked.connect(self.addListToBase)

变成:

class Dialoge_list(QDialog, Ui_dialog_List):
    def __init__(self):
        super(Dialoge_list, self).__init__()
        self.setupUi(self)
        self.addItemButt.clicked.connect(self.addListToBase)
        self.MainTabIndex = str(self.parent().MainTab.currentIndex())
        self.ListTabIndex = str(self.parent().ListTab.currentIndex())

并且只有在那之后,这些函数的值才能在 child class 中的任何地方使用。