Pynee
Pynee

Reputation: 11

NameError: name 'screen' is not defined

I keep getting an error saying:

Traceback (most recent call last):
  File "C:\Users\name\Desktop\dice\login system.py", line 64, in <module>
    loginForm()
  File "C:\Users\name\Desktop\dice\login system.py", line 33, in loginForm
    screen1 = Toplevel(screen)
NameError: name 'screen' is not defined

This is for a school project and I first have to make a login system, this is everything that could have to do with the error:

def main_screen():
    global screen
    global USERNAME
    global PASSWORD
    screen = Tk()
    screen.title('Name')
    screen.geometry("340x200")
        
    USERNAME = StringVar()
    PASSWORD = StringVar()

    Label(text = 'Welcome to Name', bg = 'grey', width = 300, height = 2, font = ('calibri', 12, 'bold')).pack()
    Label(text = '').pack()
    Button(text = 'Login', height = 2, width = 30, command = loginForm).pack()
    Label(text = '').pack()
    Button(text = 'Register', height = 2, width = 30, command = registerForm).pack()

    screen.mainloop()
def Database():
    global conn, cursor
    conn = sqlite.connect("db_member.db")
    cursor = conn.cursor()
    cursor.execute("CREATE TABLE IF NOT EXISTS `member` (mem_id INTEGER PRIMARY KEY AUTINCREMENT NOT NULL, username TEXT, password TEXT)")

 def loginForm():
    global screen1, lbl_result1
    
    screen1 = Toplevel(screen)
    screen1.title('Login')
    screen1.geometry("400x400")

    lbl_title = Label(screen1, text = "Login", bg = 'grey', width = 50, height = 2, font = ('calibri', 12, 'bold'))
    lbl_title.pack()
    lbl_username = Label(screen1, text = "Username:", font = ('calibri', 25, 'bold'), bd = 18)
    lbl_username.pack()
    username = Entry(screen1, font = ('calibri', 20, 'bold'), textvariable = USERNAME, width = 15)
    username.pack()
    lbl_password = Label(screen1, text = "Password:", font = ('calibri', 25, 'bold'), bd = 18)
    lbl_password.pack()
    password = Entry(screen1, font = ('calibri', 20, 'bold'), textvariable = PASSWORD, width = 15, show = "*")
    password.pack()
    lbl_result1 = Label(screen1, text = "", font = ('calibri', 18, 'bold'))
    lbl_result1.pack()
    btn_login = Button(screen1, text = 'Login', font = ('calibri', 18), width = 25, command = login)
    btn_login.pack()

def login():
    Databse()
    if USERNAME.get() == "" or PASSWORD.get() == "":
        lbl_result1.config(text = 'Please complete all the fields', fg = 'orange')
    else:
        cursor.execute("SELECT * FROM `member` WHERE `username` = ? and `password` = ?", (USERNAME.get(), PASSWORD.get()))
        if cursor.fetchone() is not None:
            lbl_result1.config(text = 'You logged in successfully', fg = 'blue')
        else:
            lbl_result1.config(text = 'Invalid Username or Password', fg = 'red')
loginForm()

main_screen()

Since I have done def login(): it keeps saying that either screen is not defined or that USERNAME and PASSWORD are not defined.

Upvotes: 0

Views: 1819

Answers (3)

Lovro Priselec
Lovro Priselec

Reputation: 11

It is because screen is in fucntion and other function can't see it you need to put it outised of function or give it argument self.

Upvotes: 0

Thomas Sablik
Thomas Sablik

Reputation: 16447

You are initializing the variable screen as global variable in main_screen and you are using it in loginForm but you call loginForm before main_screen and screen wasn't initialized.

Upvotes: 1

chepner
chepner

Reputation: 531345

global screen does not define a variable; it simply says that the name refers to a global variable named screen if it exists.

The problem is that you are calling loginForm (which tries to get the value of screen) before main_screen (which actually assigns a value to the name screen) is called.

You should not, however, be using so many global variables. Pass values as arguments to functions, use return values, and consider using classes to encapsulate data that can't be comfortably be shared via argument passing.

Upvotes: 3

Related Questions