【问题标题】:'Main' object has no attribute 'TabWidget' between parent Ui class "Ui_MainWindow" and child class "Ui_dialog_List"'Main' 对象在父 Ui 类“Ui_MainWindow”和子类“Ui_dialog_List”之间没有属性“TabWidget”
【发布时间】:2021-10-21 08:29:57
【问题描述】:

根据下面的代码,当我尝试从作为父 Main() 类的一部分的 addLineDialoge 函数中获取 MainTabIndex 值,并尝试在子 Dialoge_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:
             . . . . 

【问题讨论】:

  • 所需的功能用** ... **标记

标签: python class user-interface attributes parent


【解决方案1】:

我找到了解决方案。 有必要在“Dialoge_list”类Was的super方法中做如下改动:

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

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

【讨论】:

    猜你喜欢
    • 2016-08-28
    • 2017-09-01
    • 2019-01-26
    • 2013-07-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-06-10
    • 1970-01-01
    相关资源
    最近更新 更多