Joshua Baldassari
Joshua Baldassari

Reputation: 15

Unable to get function in class to work

One part of the coursework project that I am doing requires me to create a login page and incorporate a database, I have doe so successfully using python in its normal form but I now have had to put this into a GUI using tkinter and to get it work I have had to use a function on the page which calls for records in the database to be found and compared with the user input. The issue that I am experiencing is that when I call on this function it does nothing, probably due to a simple mistake in my coding but I cant figure it out for some reason or another.

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()

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

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

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

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

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

        self.controller = controller

        button1 = tk.Button(self, text="Back to Home",
                    command=lambda: controller.show_frame(SplashScreen))
        button1.pack()

        button2 = tk.Button(self, text="Sign Up",
                    command=lambda: controller.show_frame(SignUpScreen))
        button2.pack()



    def logUserIn():
        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()

        if results:
            controller.show_frame(HubScreen)
        else:
             loginResult = tk.Label(self, text="Account credentials not recognised, please try again")
             loginResult.pack()

I'm unsure as to how I should go about getting this function to work and really need the help people on here are capable of offering. I have spent far too long looking at the code and doing nothing about it to carry on, I've tired to work around it, doing other parts of the program but it is hard to assess them without this feature working. Sorry to be an inconvenience to those who are more talented than I am but its a learning process and I am trying to improve as I go.

Upvotes: 0

Views: 103

Answers (1)

Iguananaut
Iguananaut

Reputation: 23306

On this line

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

this

lambda: self.logUserIn

does nothing. This defines a function that takes no arguments and returns the function self.logUserIn. It does not call that function, it just returns whatever self.logUserIn is. So in other words, it's effectively a no-op. Instead you can just write command=self.logUserIn. However, you will need to properly make logUserIn a method which takes one argument (self):

def logUserIn(self):
    ...

You have some other mistakes there such as

controller.show_frame(HubScreen)

where that should probably be self.controller. Debugging Tkinter is tricky because you don't always see a traceback in the console right away, but if you exit the Window you'll see a traceback showing where you messed up.

Upvotes: 2

Related Questions