0

Please do me a favour. I am getting an error

Traceback (most recent call last):
  File "C:\Users\mniza\AppData\Local\Programs\Python\Python39\lib\tkinter\__init__.py", line 1885, in __call__
    return self.func(*args)
  File "C:\Users\mniza\OneDrive\LOL\Nizpad_spell_check_04.py", line 1576, in aaa
    return CustomWidget.text_get()
TypeError: text_get() missing 1 required positional argument: 'self'

This is so annoying as I am googling and trying to find the answer for almost 2 days. I am new to coding as well as python. The code which produces this error is:

def colour_certain_text(event):
    class CustomWidget(tk.Frame):
        def __init__(self, parent, label, default=""):
            tk.Frame.__init__(self, parent)
            super().__init__(parent)

            self.text_entry_label = tk.Label(self, text= "Enter the text here")
            self.text_font_style_combobox_label = tk.Label(self, text= "Font Style")
            self.text_font_colour_entry_label = tk.Label(self, text= "Font Colour")
            self.text_entry= ttk.Entry(self)
            self.text_font_style_combobox = ttk.Combobox(self, values= ["Regular", "Bold", "Italic", "Bold Italic"])
            self.text_font_colour_entry= tk.Entry(self, state= "readonly", fg= "#FFFFFF")
            self.text_font_colour_select_button= ttk.Button(self, text= "Select Colour")
            self.customwidget_destroy_button= tk.Button(self, text="⨉ Delete", cursor = "hand2", relief= "ridge")


            self.customwidget_destroy_button.bind("<Enter>", lambda event: self.customwidget_destroy_button.config(bg= "#FF0000"))
            self.customwidget_destroy_button.bind("<Leave>", lambda event: self.customwidget_destroy_button.config(bg= "SystemButtonFace"))

            self.text_font_style_combobox.insert(0, font_mode.get())
            self.text_font_style_combobox.config(state= "readonly")

            self.text_entry_label.grid(row= 0, column= 0)
            self.text_font_style_combobox_label.grid(row= 0, column= 1)
            self.text_font_colour_entry_label.grid(row= 0, column= 2)
            
            self.text_entry.grid(row= 1, column= 0)
            self.text_font_style_combobox.grid(row= 1, column= 1 )
            self.text_font_colour_entry.grid(row= 1, column= 2)
            self.text_font_colour_select_button.grid(row= 0, column= 3, rowspan= 2)
            self.customwidget_destroy_button.grid(row= 0, column= 4, rowspan= 2)

           
        def text_get(self):
            return self.text_entry.get()

    
        def font_colour_get(self):
            return self.text_font_colour_entry.get()

       
        def font_style_get(self):
            return self.text_font_style_combobox.get()


    colour_certain_text_win= tk.Toplevel(notepad)

    a= CustomWidget(colour_certain_text_win, label= "Hello")
    a.pack()

    def aaa():
        a_= 0
        return CustomWidget.text_get()


    a_btn= tk.Button(colour_certain_text_win, command= aaa)
    a_btn.pack()

    colour_certain_text_win.mainloop()

I hate classes as I am not so good at it. But now I can't avoid it. So plz help me to figure out what is the problem going on here. Otherwise I am quitting tkinter. Oh, By the way, My code is really very dirty. Please don't yell at me if you can't understand it.

5
  • 2
    First of all why are you defining a class inside a function??? Also you need to first create an instance of the class before you can call text_get Commented Apr 28, 2021 at 20:08
  • 3
    Use a.text_get() instead of CustomWidget.text_get(). If you call the method on an instance, it passes the instance as the parameter self. Commented Apr 28, 2021 at 20:11
  • 1
    I think this code will work fine, without super().__init__(parent). Commented Apr 28, 2021 at 20:12
  • 1
    @CoolCloud I would remove tk.Frame.__init__(self, parent) instead of super().__init__(parent) Commented Apr 28, 2021 at 20:16
  • pls pls pls don't put a class inside at function!! Commented Apr 28, 2021 at 20:18

1 Answer 1

1

You have to call text_get() on an instance of the class, but you're trying to call it on the class itself. That's why it says you're missing the self argument. Instance methods are automatically passed the instance as the first argument (eg: self), but only when you call the method on the instance.

In your specific example, you should use a.text_get() since a holds a reference to a specific instance of CustomWidget.

Imagine if you created two instances of CustomWidget named a and b. If you call CustomWidget.text_get(), how would python know whether you meant to get the text from a or if you meant to get it from b? The way it knows is by you calling the method on a specific instance of the class.

Note: you could also do CustomWidget.text_get(a) to explicitly tell text_get which widget to use, but that's not the best way to do it.

Sign up to request clarification or add additional context in comments.

5 Comments

You might also want to mention OP inheriting from tk.Frame twice as @CoolCloud mentioned. I would remove the tk.Frame.__init__(self, parent)
@TheLizzard: that has nothing to do with the question being asked. While it's a bad practice, it's not relevant to the question being asked IMHO.
I still think that you should at least mention it to avoid help OP. Isn't this website to help people?
@TheLizzard: "Isn't this website to help people?" - yes and no. It is designed to be a repository of answers to technical questions. It is not designed to be a general-purpose code review platform. There's a separate site for that. I will usually point out bad practices if the bad practice contributed to the problem being asked about, but in this specific case it would just add noise to the answer.
Well from what I can see everyone is using this website to just ask for help when they encounter a problem that they can't resolve. I would usually just point out the mistakes that can effect code execution.

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.