【问题标题】:AttributeError: object has no attribute?AttributeError:对象没有属性?
【发布时间】:2021-11-22 02:43:16
【问题描述】:

好的,这是我得到的错误: AttributeError:“DES”对象没有属性“summary_output”

这就是我想要做的。

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

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(']','') + 
                            "\n\n" + self.header[0][1] + ": " + str(self.yvalues).replace('[','').replace(']','') + 
                            "\n\n" + 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(']','') + 
                            "\n\n" + self.header[0][1] + ": " + str(self.yvalues).replace('[','').replace(']',''))
# -------------------------------------------------------------------------
        s = Set(text)                        
        s.set_summary()
#-----------------------------------------------------------------------

                    

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

Traceback (most recent call last):
  File "C:\Users\***\AppData\Local\Programs\Python\Python39\lib\tkinter\__init__.py", line 1892, in __call__
    return self.func(*args)
  File "C:\Users\***\Documents\Workspace\***\***\view\upload_csv.py", line 115, in upload
    s.set_summary()
  File "C:\Users\***\Documents\Workspace\***\***\view\Set.py", line 14, in set_summary
    s.set_summary_text()
  File "C:\Users\***\Documents\Workspace\***\***\view\test.py", line 164, in set_summary_text
    print(self.summary_output)
AttributeError: 'DES' object has no attribute 'summary_output'

我的设置类:

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\***\AppData\Local\Programs\Python\Python39\lib\tkinter\__init__.py", line 1892, in __call__
    return self.func(*args)
  File "C:\Users\***\Documents\Workspace\\***\\***\view\upload_csv.py", line 115, in upload
    s.set_summary()
  File "C:\Users\\***\Documents\Workspace\\***\view\Set.py", line 22, in set_summary
    s.createFrame(root)
  File "C:\Users\\***\Documents\Workspace\\***\view\test.py", line 120, in createFrame
    self.canvas.draw() # Create the graph canvas
  File "C:\Users\\***\AppData\Local\Programs\Python\Python39\lib\site-packages\matplotlib\backends\backend_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\\***\AppData\Local\Programs\Python\Python39\lib\tkinter\__init__.py", line 1892, in __call__
    return self.func(*args)
  File "C:\Users\\***\AppData\Local\Programs\Python\Python39\lib\site-packages\matplotlib\backends\_backend_tk.py", line 235, in filter_destroy
    self._master.update_idletasks()
AttributeError: 'str' object has no attribute 'update_idletasks'

所以我删除了 matplot 图并得到了这个错误:

所以也许图表有干扰?我不确定,我需要图表。

【问题讨论】:

  • 运行您的代码时没有出现错误
  • 你也不应该使用关键字set,它是python中的一个保留字
  • 嗨,Travis,请查看我的回答。
  • @BendikKnapstad 再次检查我发现错误
  • @BehdadAbdollahiMoghadam 请在我注意到错误时查看编辑

标签: python oop tkinter tk


【解决方案1】:
  • DES 类中的summary_output 将在 createFrame 方法。
  • 您首先从Set.set_summary() 中的DES 类实例化 方法,然后调用它使用的set_summary_text() 方法 summary_output。这是不正确的,因为尚未定义 summary_output
  • 您应该首先调用createFrame() 方法来定义 summary_output 属性然后调用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或者使用它。

【讨论】:

  • 太棒了!感谢您的答复。我试了一下,得到以下错误:
  • 请看编辑
  • 所以我的回答,删除了你的错误..你有一个新的错误?如果您不要更改问题,请针对新的错误和错误提出另一个问题。
  • 如果我的回答帮助您解决了以前的错误,那么选择我的回答为correct answer 会很棒 ✅ 我会尝试在新问题中帮助您解决新错误。 ..
  • 不,你的回答造成了新的错误
猜你喜欢
  • 2012-12-01
  • 2021-04-19
  • 1970-01-01
  • 1970-01-01
  • 2018-08-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-10-23
相关资源
最近更新 更多