【问题标题】:Python AttributeError: Object has no attributePython AttributeError:对象没有属性
【发布时间】:2021-08-20 05:13:00
【问题描述】:

我正在尝试构建一个 Tkinter 程序。但是,当我尝试在 CheckPage 中调用函数“check_click”时,出现以下错误:AttributeError:“CheckPage”对象没有属性“check_click”。不知何故我不知道我做错了哪一部分(我想如果我想在 CheckPage 中调用函数'check_click',我只需要在它前面加上'self')。我知道这是一个简单的错误。希望有人可以指导我工作程序,也可以帮助我学习~!

这是我的代码:

import tkinter as tk 

class TkApp(tk.Tk):
    def __init__(self):
        super().__init__()
        self.wrap = tk.LabelFrame(self, text = 'Frame')
        self.wrap.place(height = 300, width = 700, rely = 0.45, relx = 0.0125)
        self.geometry('800x600')
        self.check_page = CheckPage(parent = self.wrap)
        
    
class CheckPage(tk.LabelFrame):
    def __init__(self, parent):
        super().__init__(parent) 
        check_q = tk.StringVar()
        self.check_scrollbar = tk.Scrollbar(parent, orient = tk.VERTICAL)
        self.check_label = tk.Label(parent, text = "Number:")
        self.check_label.pack(anchor=tk.N, pady = 10, padx = 10, side = tk.LEFT)
        self.check_entry = tk.Entry(parent, textvariable = check_q)
        self.check_entry.pack(anchor=tk.N, pady = 10, side = tk.LEFT)
        self.check_list = tk.Listbox(parent, width = 50, yscrollcommand = self.check_scrollbar.set)
        self.check_scrollbar.config(command = self.check_list.yview)
        self.check_scrollbar.pack(side = "right", fill = "y")
        self.check_but = tk.Button(parent, text = "Click")  
        self.check_but.pack(anchor=tk.N, pady = 8, padx = 10,  side = tk.LEFT)
        self.check_but.bind('<Button-1>', self.check_click)
        self.check_list.pack(anchor=tk.NW, pady = 8, padx = 10,  side = tk.LEFT, fill = 'both')
        
        def check_update(self, src): 
            self.check_list.insert('end', "Hi")

        def check_click(self, event=None):
            target = self.check_entry.get()
            self.check_update(target)


t = TkApp() 
t.mainloop()

这是控制台中的错误:

AttributeError                            Traceback (most recent call last)
<ipython-input-1-49b54d11f52f> in <module>
     37 
     38 
---> 39 t = TkApp()
     40 t.mainloop()
<ipython-input-1-49b54d11f52f> in __init__(self)
      9         self.wrap.place(height = 300, width = 700, rely = 0.45, relx = 0.0125)
     10         self.geometry('800x600')
---> 11         self.check_page = CheckPage(parent = self.wrap)
     12 
     13 
<ipython-input-1-49b54d11f52f> in __init__(self, parent)
     26         self.check_but = tk.Button(parent, text = "Click")
     27         self.check_but.pack(anchor=tk.N, pady = 8, padx = 10,  side = tk.LEFT)
---> 28         self.check_but.bind('<Button-1>', self.check_click)
     29         self.check_list.pack(anchor=tk.NW, pady = 8, padx = 10,  side = tk.LEFT, fill = 'both')
     30 
AttributeError: 'CheckPage' object has no attribute 'check_click'

【问题讨论】:

    标签: python python-3.x oop tkinter


    【解决方案1】:

    目前,check_updatecheck_click__init__ 内部,而不是在 CheckPage 类内部(因此,它们不是方法)。取消缩进四个空格,所以它会变成:

    class CheckPage(tk.LabelFrame):
        def __init__(self, parent):
            super().__init__(parent) 
            # ...
            
        def check_update(self, src): 
            self.check_list.insert('end', "Hi")
    
        def check_click(self, event=None):
            target = self.check_entry.get()
            self.check_update(target)
    

    【讨论】: