Why do i get the error AttributeError: 'NoneType' object has no attribute 'get'?

Viewed 39

I am trying to create a login GUI using Tkinter in python. Everything works well for registering an account as the details are stored in a txt file, but when i use those details to login i get the error AttributeError: 'NoneType' object has no attribute 'get'. I don't see anything wrong with my code but here is the login funtion below:

def check_login():
    login_username = username_login.get()
    login_password = password_login.get()
    with open(login_username_login+".txt", "r") as file:
        #make sure this works. A space is needed after the readlines to mimic what is in the text file
        if (file.readlines()+" ") == password_login:
            Label(screen1, text = "Login Success", fg = "red", bg="#DFA0F1" ,font = ("calibri", 11)).pack()
        else:
            Label(screen1, text="Username/Password incorrect", fg = "red", bg="#DFA0F1").pack()

username_login and password_login are defined here:

def login():
    global username_login
    global password_login
    username_login = StringVar()
    password_login = StringVar()
    top = Toplevel()
    top.title("Login")
    top.geometry("500x300")
    top["bg"]="#DFA0F1"
    Label(top, text="Login:", bg="#F3EF9D").pack()
    username_label = Label(top, text="Username", bg="#AAF7C7").pack()
    username_login = Entry(top, textvariable=username_label).pack()
    password_label = Label(top, text="Password", bg="#AAF7C7").pack()
    password_login = Entry(top, textvariable=password_label, show="*").pack()
    login = Button(top, text="Login", height="1", width="10", bg="#AEF0F5", command=check_login).pack()
1 Answers

The rest of the code would be of help. I suspect that the use of the global variables is not correct somewhere, because check_login() cannot access those variables.

Does this minimum working example help?

import tkinter as tk
from tkinter import StringVar


def login():
    global username_login
    global password_login
    username_login = StringVar()
    password_login = StringVar()
    username_login.set("USR")
    password_login.set("PWD")
    

def check_login():
    print(username_login.get())
    print(password_login.get())


if __name__ == "__main__":
    master_window = tk.Tk()
    login()         # Must be called before check_login()
    check_login()
Related