AttributeError:对象没有属性?

AttributeError: object has no attribute?(AttributeError:对象没有属性?)

本文介绍了AttributeError:对象没有属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,这是我收到的错误: AttributeError:‘Des’对象没有‘ASMULT_OUTPUT’属性

这就是我正在尝试做的事情。

当我在此框架上时,我正在创建一个文本变量,然后将其发送到Set类。

class upload_csv(Frame):
    def __init__(self, master):
        self.master = master
        self.frame = tk.Frame(self.master, width=250, height=160, bg='white')
        
        self.upload_csv_btn = Button(
                self.frame,
                text="Add Data Source",
                fg="DodgerBlue4",
                font=("Graph Type", 15),
                height=1, width=20,
                borderwidth=2,
                relief="groove",
                command=self.upload)

        self.upload_csv_btn.place(x=10, y=10)
        
        self.frame.pack()
        
    def upload(self):
        global text
        self.xvalues = []
        self.yvalues = []
        self.xyvalues = []
        self.header = []
       
        filename = filedialog.askopenfilename()
        if len(filename) != 0:
            print('Selected:', filename)
            with open(filename) as file:
                csvreader = csv.reader(file)
                self.header.append(next(csvreader)) 
                for row in csvreader:
                    if len(row) == 3:
                        self.xvalues.append(int(row[0]))
                        self.yvalues.append(int(row[1]))
                        self.xyvalues.append(int(row[2])) 
                        text = (
                            self.header[0][0]+ ": " + str(self.xvalues).replace('[','').replace(']','') + 
                            "

" + self.header[0][1] + ": " + str(self.yvalues).replace('[','').replace(']','') + 
                            "

" + self.header[0][2] + ": " + str(self.xyvalues).replace('[','').replace(']',''))   
                        
                    elif len(row) == 2:
                        self.xvalues.append(row[0])
                        self.yvalues.append(row[1])  
                        text = (
                            self.header[0][0] + ": " + str(self.xvalues).replace('[','').replace(']','') + 
                            "

" + self.header[0][1] + ": " + str(self.yvalues).replace('[','').replace(']',''))
# -------------------------------------------------------------------------
        s = Set(text)                        
        s.set_summary()
#-----------------------------------------------------------------------

                    

使用Upload类,我通过调用set类和set_sum方法发送变量。使用这个Set类,我将字符串设置为对象项,然后将其发送到我的DES类。我希望在tk文本框元素上设置此项目作为摘要。我在DES类中收到的文本没有问题,但在尝试修改摘要元素时出现以下错误。 我收到的错误:

Traceback (most recent call last):
  File "C:Users***AppDataLocalProgramsPythonPython39lib	kinter\__init__.py", line 1892, in __call__
    return self.func(*args)
  File "C:Users***DocumentsWorkspace******viewupload_csv.py", line 115, in upload
    s.set_summary()
  File "C:Users***DocumentsWorkspace******viewSet.py", line 14, in set_summary
    s.set_summary_text()
  File "C:Users***DocumentsWorkspace******view	est.py", line 164, in set_summary_text
    print(self.summary_output)
AttributeError: 'DES' object has no attribute 'summary_output'

我的Set类:

class Set:
    def __init__ (self, summary):
        self.summary = summary
        
    def set_summary(self):
        print(self.summary)
        s = DES(self.summary)                        
        s.set_summary_text()

我的DES类:

class DES(Frame):
    def __init__(self, summary):
        self.summary = summary
        
    def createFrame(self, master):
        self.frame = tk.Frame(master, width=750, height=968,bg='white')
        self.summary_output = tk.Text(
                        self.frame, 
                        height=8,
                        width=78,
                        bg="gray95",
                        borderwidth=2, 
                        relief="groove",
                        font=("Arial", 12))

        self.summary_output.configure(state='disabled') 

      
        self.summary_output.place(x=20, y=610)
        
        self.frame.pack()
       

    def set_summary_text(self):
        print(self.summary)
        print(self.summary_output)
        self.summary_output.configure(state='normal')
        self.summary_output.delete('1.0', END) # Remote all text
        self.summary_output.insert('end',self.summary)
        self.summary_output.configure(state='disabled')  #Make text widget read only 

def main(): 
    global root
    root = tk.Tk()
    # app = DES(root)
    # app = DES.createFrame(root)
    s = DES("")                        
    s.createFrame(root)
    root.mainloop()
    

if __name__ == '__main__':
    main()
    

编辑:

所以在尝试回答后,我得到了以下错误,我所做的只是添加了建议:

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:Users***AppDataLocalProgramsPythonPython39lib	kinter\__init__.py", line 1892, in __call__
    return self.func(*args)
  File "C:Users***DocumentsWorkspace\***\***viewupload_csv.py", line 115, in upload
    s.set_summary()
  File "C:Users\***DocumentsWorkspace\***viewSet.py", line 22, in set_summary
    s.createFrame(root)
  File "C:Users\***DocumentsWorkspace\***view	est.py", line 120, in createFrame
    self.canvas.draw() # Create the graph canvas
  File "C:Users\***AppDataLocalProgramsPythonPython39libsite-packagesmatplotlibackendsackend_tkagg.py", line 11, in draw
    self._master.update_idletasks()
AttributeError: 'str' object has no attribute 'update_idletasks'
Exception in Tkinter callback
Traceback (most recent call last):
  File "C:Users\***AppDataLocalProgramsPythonPython39lib	kinter\__init__.py", line 1892, in __call__
    return self.func(*args)
  File "C:Users\***AppDataLocalProgramsPythonPython39libsite-packagesmatplotlibackends\_backend_tk.py", line 235, in filter_destroy
    self._master.update_idletasks()
AttributeError: 'str' object has no attribute 'update_idletasks'

所以我删除了matploy图,并得到以下错误:

所以可能是图表干扰了?我不确定,我需要这个图表。

推荐答案

  • 类中的summary_output将在 createFrame方法。
  • 您首先从Set.set_summary()中的DES类实例化 方法,然后调用它使用的set_summary_text()方法 summary_output。这是不正确的,因为summary_output尚未定义。
  • 您应该首先调用createFrame()方法来定义 属性,然后调用set_summary_text()以 使用SUMMARY_OUTPUT。

Set类中这样做:

class Set:
    def __init__ (self, summary):
        self.summary = summary
        
    def set_summary(self):
        global root
        print(self.summary)
        s = DES(self.summary)   
        s.createFrame(root)                     
        s.set_summary_text()

或做任何您认为最适合自己的事情,但您应该先定义summary_output,然后再定义print或使用它。

这篇关于AttributeError:对象没有属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:AttributeError:对象没有属性?