-1

I want to bind a function to open a new window with form to fill details with a 'Register' button. But even after defining the register() function, it gives NameError when I click the button.

Tried with: 1) 'command=function" method 2) btn.bind() method Still giving error.

register_btn = Button(main_screen,text = "Register", bg = "grey",width = "30", height = "2")
register_btn.pack()
register_btn.bind("<Button-1>", register)

#function
def register(event):
---

Exception has occurred: NameError name 'register' is not defined

2
  • 1
    define the function before you bind it. So define it above the bind. Commented Sep 7, 2019 at 5:54
  • Questions like this require that they come with a minimal reproducible example. You probably would have found the answer yourself that way. As a new user, please take the tour and read How to Ask. Commented Sep 7, 2019 at 6:18

1 Answer 1

2

You need to define the function before you actually call it, unless you're using classes of course.

#function
def register(event):
    pass

register_btn = Button(main_screen,text = "Register", bg = "grey",width = "30", height = "2")
register_btn.pack()
register_btn.bind("<Button-1>", register)
Sign up to request clarification or add additional context in comments.

5 Comments

Thank you mate. Problem solved. Didn't knew I was making such a noob mistake.
Don't worry about it, we've all been there, would you mind accepting the answer as correct though? I would really appreciate it. @NeerajRawat
"You need to define the function before you actually call it, unless you're using classes" Huh? Classes are not an exception to this rule.
No I meant you can call one class method from another method in the same class no matter where it is defined, whether it's way up or way down. @Aran-Fey get what I am saying?
@Sahil Just like you can call a function from another function, no matter where it is defined.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.