0

When i was trying to event bind inside my class, binding event creates argument error. Codes are written below-

class Login_App(tk.Tk):
    def __init__(self):
        super().__init__()
        self.btn_lgin = ttk.Button(self, text="Login")
        self.btn_lgin.grid()
        self.btn_lgin.bind('<Return>', lambda: Login(self=self))

    def Login(self):
        '''I need "Self" in some codes, cant remove it'''
        print("Clicked")

if __name__ == "__main__":
    app = Login_App()
    app.mainloop()

1 Answer 1

1

You should only do a minor change:

When calling a function with a parameter self inside a class self is not an actual parameter so you don't need to pass it as one.
What you should do is:

self.btn_lgin.bind('<Return>', lambda x: self.Login())

Be aware that when you change this you should also change: lambda: to lambda x: because lambda: gets one positional argument when you've passed zero, leading to TypeError

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.