'Main'对象在父 Ui 类"Ui_MainWindow"和子类"Ui_dialog_List"之间没有属性'TabWidget'



根据下面的代码,当我尝试从addlinedialog函数(这是父Main()类的一部分)中获取MainTabIndex值,并尝试在子dialge_list类中使用它时,'AttributeError:' Main '对象没有属性' MainTabIndex '错误发生。请告诉我如何解决这个问题。

父类:

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()

子类:

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:
. . . . 

我找到了一个解决方案。有必要对" dialge_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函数进行了更改。是:

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)
. . .

之后,在子类中,你可以调用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())

只有在此之后,这些函数的值才能在子类中的任何地方使用。