0

I'm tryin' to structure a Tkinter program. However, when I try to call the function 'check_click' in CheckPage, I get the following error: AttributeError: 'CheckPage' object has no attribute 'check_click'. Somehow I dunno which part I did wrong (I thought if I want to call the function 'check_click' in CheckPage, I just need to prefix it with 'self'). I know this is a simple error. Hope someone can guide me to a working program, but also help me learn~!

Here's my code:

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()

And here is the error in the console:

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'

1 Answer 1

4

Currently, check_update and check_click are inside __init__, and not inside the class CheckPage (therefore, they are not methods). Unindent four spaces, so it'll become:

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)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.