-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

1 Answers1

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)
Sahil
  • 1,387
  • 14
  • 41
  • Thank you mate. Problem solved. Didn't knew I was making such a noob mistake. – Neeraj Rawat Sep 07 '19 at 06:12
  • 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 – Sahil Sep 07 '19 at 06:20
  • *"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. – Aran-Fey Sep 07 '19 at 06:33
  • 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 Sep 07 '19 at 06:39
  • @Sahil Just like you can call a function from another function, no matter where it is defined. – Aran-Fey Sep 07 '19 at 10:59