function automatically calls before i call it myself?

Question:

I’m trying to make a GUI version of a verification app that checks a CSV spread sheet and checks if the data that the user has inputted is a match with any of the data on the spread sheet.

The problem I have is the functions I have used start to automatically call them selves before I call them in the code

Her is the None-GUI version of the script that works fine:

import csv

def logged ():
    print("You have succesfully logged in!")
def wrong ():
    print("You never got the correct log in details!")
    login()

def login (x, y):
    with open("login_details.csv") as file:
        read = csv.reader(file, delimiter=",")

        log = []
        pas = []

        for i in read:
            l = i[0]
            p = i[1]
            log.append(l)
            pas.append(p)

        try:
            logindx = log.index(x)
            pasindx = pas.index(y)
            if(logindx == pasindx):
                logged()
            else:
                wrong()
        except Exception as e:
            wrong()

login()

The problem I then have now is that I am using a GUI and the wrong() function seems to call it self.

I’ll post full code so its easy to understand

from tkinter import *
import csv
#import logon

def wrong():
    print("You got the Wrong login combination!")
    enter.login()
def logged ():
    print("You have successfuly loggoed in!")

class enter:
    def login(x, y):
        with open("login_details.csv") as file:
            read = csv.reader(file, delimiter=",")
            log = []
            pas = []
            for i in read:
                l = i[0]
                p = i[1]
                log.append(l)
                pas.append(p)
            try:
                logindx = log.index(x)
                pasindx = pas.index(y)
                if(logindx == pasindx):
                    logged()
                else:
                    wrong()
            except Exception as e:
                wrong()



#Window
root = Tk()
root.title("Login")
root.geometry("250x250")
root.configure(bg="white")


main = Frame(root, bg="white")
main.pack()
main.place(height=100, x=25, y=10)
#Username
usr_lbl = Label(main, text="Username: ", bg="white")
usr_lbl.grid(row=0, column=0)
usr_inp = Entry(main, bg="light grey")
usr_inp.grid(row=0, column=1)
#Password
pass_lbl = Label(main, text="Password: ", bg="white")
pass_lbl.grid(row=1, column=0)
pass_inp = Entry(main, bg="light grey")
pass_inp.grid(row=1, column=1)
#Enter
enter = Button(main, bg="light grey", text="Enter", command=enter.login(usr_inp.get(), pass_inp.get()))
enter.grid()
enter.place(width=100, y=45, x=50)



root.mainloop()

The error I get goes:

"You got the Wrong login combination!"

(Which is what the wrong() function prints)

  File ..., line 23, in login
    logindx = log.index(x)
ValueError: '' is not in list

During handling of the above exception, another exception occurred:

  File ..., line 55, in <module>
    enter = Button(main, bg="light grey", text="Enter", command=enter.login(usr_inp.get(), pass_inp.get()))
  File ..., line 30, in login
    wrong()
  File ..., line 7, in wrong
    enter.login()
TypeError: login() missing 2 required positional arguments: 'x' and 'y' 

This makes it seem as the function is calling before I get a chance to call it. How would I go about solving this?

Asked By: Ashmoreinc

||

Answers:

You are calling the enter.login() method when you are creating the button:

enter = Button(main, bg="light grey", text="Enter", command=enter.login(usr_inp.get(), pass_inp.get()))

You are passing the result of enter.login() to the command parameter, instead of having the button call it when a user presses the button.

Add a lambda anonymous function:

enter = Button(main, bg="light grey", text="Enter",
   command=lambda: enter.login(usr_inp.get(), pass_inp.get()))

Now a function object (produced from the lambda is passed to the command argument, which is called when the button is pressed. In turn, the lambda then calls usr_inp.get() and pass_inp.get() and passes the results on to enter.login().

Answered By: Martijn Pieters