You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PyQt父类Main与子类Dialoge_list间AttributeError问题求助:'Main'对象无'MainTabIndex'属性

解决AttributeError: 'Main' object has no attribute 'MainTabIndex'问题

兄弟,你踩了一个Python GUI开发里很常见的坑:在Dialoge_list里用Main().MainTabIndex的时候,你新建了一个完全独立的Main实例,而不是触发对话框的那个已经初始化过MainTabIndex的Main对象!新实例根本没执行过addLineDialoge方法,自然没有这个属性,所以才会报这个错。

给你两个简单直接的解决办法,推荐第一个,逻辑更清晰:


方法一:创建对话框时直接传递索引参数

修改父类的addLineDialoge方法,把当前的MainTabIndex和ListTabIndex作为参数传给Dialoge_list的构造函数,让子类直接拿到需要的值:

修改后的父类代码

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(self.MainTabIndex, self.ListTabIndex)
        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, main_tab_index, list_tab_index):
        super(Dialoge_list, self).__init__()
        self.setupUi(self)
        # 把参数保存为子类的属性,方便后续使用
        self.main_tab_index = main_tab_index
        self.list_tab_index = list_tab_index
        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 all(not content for content in [numCell, thikCell, matCell, sizeCell, quantityCell, dateinCell, applyCell, noticeCell]):
            emptyError = EmptyErrorDialoge()
            emptyError.exec()
        # 直接用子类自己保存的属性判断
        elif self.main_tab_index == 0 and self.list_tab_index == 0:
            # 这里写你的业务逻辑
            pass

方法二:通过父窗口实例访问(适合需要更多父类资源的场景)

如果后续需要用到父类的更多属性或方法,也可以在创建对话框时把父类实例本身传进去:

父类修改部分

# 创建对话框时传入self(当前Main实例)
addList = Dialoge_list(parent=self)

子类修改部分

class Dialoge_list(QDialog, Ui_dialog_List):
    def __init__(self, parent=None):
        super(Dialoge_list, self).__init__(parent)
        self.setupUi(self)
        self.addItemButt.clicked.connect(self.addListToBase)
    def addListToBase(self):
        # 通过self.parent()获取父类实例,然后访问属性
        if self.parent().MainTabIndex == 0 and self.parent().ListTabIndex == 0:
            # 业务逻辑
            pass

这个方法要注意:必须确保父类实例已经执行过addLineDialoge方法,初始化了MainTabIndex和ListTabIndex,不然还是会报错。


内容的提问来源于stack exchange,提问作者clickaider

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.30 05:09:07