Joshua Baldassari
Joshua Baldassari

Reputation: 15

attribute error in retrieval of text

After making progress in my coursework I have hit another roadblock in the fact that there is an attribute error which I am unable to resolve. I am trying to retrieve the information that the user has input for their account credentials so that the data can be compared to the information in the database I have and the user can be granted or denied access to the account i.e. a login screen. The error is:

AttributeError: 'LogInScreen' object has no attribute 'usernameEntry'

And the code where this comes from is:

class LogInScreen(tk.Frame):
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        description = tk.Label(self, text="Please log in to gain access to the content of this computer science program.", font=LARGE_FONT)
        description.pack()

        label1 = tk.Label(self, text="Enter your username:")
        label1.pack()

        usernameEntry = tk.Entry(self)
        usernameEntry.pack()

        label2 = tk.Label(self, text="Enter your password:")
        label2.pack()


        passwordEntry = tk.Entry(self, show="*")
        passwordEntry.pack()

        notRecognised=tk.Label(self, text="")

        logInButton = tk.Button(self, text="Log In",
                                command=self.logUserIn)
        logInButton.pack()

        self.controller = controller

    def logUserIn(self):
        username = self.usernameEntry.get()
        password = self.passwordEntry.get()

        find_user = ("SELECT * FROM user WHERE username == ? AND password == ?")
        cursor.execute(find_user,[(username),(password)])
        results = cursor.fetchall()

I'm not sure as to why I'm getting the error and i have asked other people in my class for help but I'm not getting very far. I'm aware it's probably something very simple but i cant resolve it myself. Thank you for any help you can give.

Upvotes: 0

Views: 94

Answers (2)

Mike - SMT
Mike - SMT

Reputation: 15226

You issue from this error AttributeError: 'LogInScreen' object has no attribute 'usernameEntry' is due to the fact that you have not defined usernameEntry as a class attribute. So when you program initializes it will create the widgets but nothing will be able to interact with it from outside of __init__.

If a method within the class needs to interact with the variables outside of the method or to interact from outside of your class instance the variables need to be assigned as an attribute with the prefix self.

All you need to do is change this:

usernameEntry = tk.Entry(self)
usernameEntry.pack()

passwordEntry = tk.Entry(self, show="*")
passwordEntry.pack()

To this:

self.usernameEntry = tk.Entry(self)
self.usernameEntry.pack()

self.passwordEntry = tk.Entry(self, show="*")
self.passwordEntry.pack()

Upvotes: 1

Guillaume
Guillaume

Reputation: 6019

In method logUserIn(self) you try to access an attribute self.usernameEntry which is not defined.

In __init__() you have a variable named usernameEntry, but this variable is not an attribute. Maybe you want to add self.usernameEntry = usernameEntry to the __init__ method to turn that variable into an attribute which will then be accessible by logUserIn?

BTW it will be the same for passwordEntry.

Upvotes: 0

Related Questions