简体   繁体   中英

NameError: name 'regc' is not defined in Tkinter

I got a name error when i am using this code.Can anyone fix this problem?

from tkinter import *
import mysql.connector

home=Tk()
home.geometry("700x700")
home.title("Home")

reg=Button(home,text="Register",bg='brown',fg='white',width=20,command=regc)
reg.place(x=350,y=200)
mainloop()

I got an error like this:

Traceback (most recent call last):
 File "C:/Users/Softech/Desktop/tkinterproject.py", line 29, in <module>
 reg=Button(home,text="Register",bg='brown',fg='white',width=20,command=regc)
 NameError: name 'regc' is not defined

Maybe you forgot to define the function regc , anyway in the code its not there. So start off by defining it. Keep in mind, you have to define it before the declaration of the button.

from tkinter import *
import mysql.connector

def regc():
    new=Toplevel()
    new.geometry("500x500")
    new.title("Registration")
    Label_reg=Label(new,text="REGISTRATION FORM",width=20,font=("bold",20)) 
    Label_reg.place(x=90,y=53)
    lname=Label(new,text="Name",width=20,font=("bold",10)) 
    lname.place(x=80,y=130)

home=Tk()
home.geometry("700x700")
home.title("Home")

reg=Button(home,text="Register",bg='brown',fg='white',width=20,command=regc)
reg.place(x=350,y=200)
home.mainloop()

To understand more on how to define a function, take a look here

Hope it helped you solve your error.

Cheers

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM