I keep getting the "name is not defined" error in Python when I have already defined the name?

Hi,
i am trying to write a code for an assignment that uses tkinter for the GUI and SQL to save the data.
here is the annoying bit of code.

cur.execute("INSERT INTO user (ID, Name, Age, Email, Town) VALUES (?, ?, ?, ?, ?)", 
(uid, str(nme), str(age), str(eml), str(twn)))

and here is the error i get

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

the thing is that i have already defined all the names in a function.
here is my full code

from tkinter import *
import sqlite3
con = sqlite3.connect("user_info.db")
cur = con.cursor()
root = Tk()
root.title("create account")



def get_input():
    uid = e0.get()
    nme = e1.get()
    age = e2.get()
    eml = e3.get()
    twn = e4.get()
    e0.delete("0", "end")
    e1.delete("0", "end")
    e2.delete("0", "end")
    e3.delete("0", "end")
    e4.delete("0", "end")
    print(uid)
    print(nme)
    print(age)
    print(eml)
    print(twn)

lbl = Label(root, text= "ID: ")
lbl.grid(row="0", column="0")

e0 = Entry(root, width= 30)
e0.grid(row="0", column="1")

lbl1 = Label(root,text= "Name: ")
lbl1.grid(row="2",column="0")

e1 = Entry(root, width = 30)
e1.grid(row="2",column="1")

lbl2 = Label(root, text = "Age: ")
lbl2.grid(row="4",column="0")

e2 = Entry(root, width = 30)
e2.grid(row="4",column="1")

lbl3 = Label(root, text = "Email: ")
lbl3.grid(row="6",column="0")

e3 = Entry(root, width = 30)
e3.grid(row="6",column="1")

lbl4 = Label(root, text = "Town: ")
lbl4.grid(row="8",column="0")

e4 = Entry(root, width = 30)
e4.grid(row="8",column="1")

sbut = Button(root, text= "submit", command = get_input)
sbut.grid(row="9",column="1")

root.geometry("300x300")
root.mainloop()

cur.execute("INSERT INTO user (ID, Name, Age, Email, Town) VALUES (?, ?, ?, ?, ?)", 
(uid, str(nme), str(age), str(eml), str(twn)))

con.commit()
con.close()```
can someone help

I don’t see where you are calling that function. Also, I don’t see that function updating anything outside of its scope.

sbut = Button(root, text= “submit”, command = get_input)
is where its been called. its a tkinter thing

Is it calling it? Or just assigning it as some sort of an action for a button?

And I still don’t see where you are updating a scope outside of the function.

when the code is run the tkinter module displays a gui and when you click the submit button it is meant to save what was in the entry boxes and assign the data to variables then clear the entry boxes. this part calls the function when the button is clicked
command = get_input

Ok, I’m still not seeing how this function is affecting a scope outside of itself.

let me quickly redesign the code

i redesigned the code and made it so the sql command that saves the data is in with the function that is called when the button is pressed and that worked

I am new to tkiner and sql. I admit i am not the best at python and have a lot to learn. thank you soo much Bob for the help as it allows me to continue with my project

1 Like

I’m glad I could help.

1 Like