Python AttributeError:对象没有属性

2024-07-01 07:57:58 发布

您现在位置:Python中文网/ 问答频道 /正文

我在试着组织一个Tkinter项目。但是,当我尝试在CheckPage中调用函数“check\u click”时,我得到以下错误:AttributeError:“CheckPage”对象没有属性“check\u 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'

Tags: selfinitcheckleftsidepacktkbut
1条回答
网友
1楼 · 发布于 2024-07-01 07:57:58

当前,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)

相关问题 更多 >

    热门问题