开发者社区> 问答> 正文

无法在Python中访问父变量,怎么办?

我试图从父类访问变量。但它似乎没有用。

这是我缩短的原始代码,以显示基本行:

class Parent_Class(GUI):

    def __init__(self,hdf5w,master):
    self.frameFilter = Frame(self.frameDataset)
    self.frameFilter.grid(row=3,column=0,sticky='wen')
    # I shortened framefilter definition here.

    self.filter0 = Listbox(self.frameFilter,exportselection=0)
    self.filter0.grid(row=1,column=0,sticky='wen')
    self.filter0.bind('<<ListboxSelect>>', self.onSelectFilter0)

    def onSelectFilter0(self,evt):

        self.datasetsfilter = self.ohdf5.getDatasets(self.ohdf5.filterGroupPath)
        # Here is an exemple of variable that I will try to use below in extended class.

        for dataset in self.datasetsfilter:
            self.dataset_listbox.insert(i,dataset)
            i+=1

    def export(self):
        new_window2 = Toplevel(self.frameFile)
        w = ExportGUI(new_window2)

class Extended_Class(Parent_Class):

def __init__(self, master):

    self.master = master
    self.master.title("Export")
    self.master.geometry('200x200')

    export_selected = Button(self.master, text = 'Export filters',command=self.export_filter)
    export_selected.place(x=20, y=50)

    self.master.mainloop()   

def export_filter(self):  
    a = self.ohdf5.datasetsfilter
    print('EXPORTING')  
    print(a)    

所以我收到以下错误消息:

Exception in Tkinter callback Traceback (most recent call last): File "C:\ProgramData\Anaconda2\lib\lib-tk\Tkinter.py", line 1542, in call return self.func(*args) File "GUI\GUI.py", line 384, in export_filter a = self.ohdf5.datasetsfilter AttributeError: ExportGUI instance has no attribute 'ohdf5' 我找到了这个主题:如何在python中访问父类的实例变量?我有完全相同的错误代码。但我的变量datasetsfilter已经定义,self所以它不会工作。

展开
收起
被纵养的懒猫 2019-09-17 17:04:25 805 0
1 条回答
写回答
取消 提交回答
  • 推荐回答

    用super() def init(): super().init()

    2019-10-23 17:25:01
    赞同 展开评论 打赏
问答分类:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
From Python Scikit-Learn to Sc 立即下载
Data Pre-Processing in Python: 立即下载
双剑合璧-Python和大数据计算平台的结合 立即下载