how do i check the data from text file in python tkinter? login system not working

271 Views Asked by At

I have made a singup window which saved the data of people in text file and in login window i used a for loop to read thorugh the data and check if the password and username is correct or not but everytime i press login, it only shows incorrect username or password and doesnt open the dashboard window even if the password is correct.

def check():
        storefile = open('store.txt','r')
        for line in storefile:
            if namepasss and nameuser in line:
                dashboard()
            else:
                Label(my, text="Incorrect username or\n password.", fg="red", bg="lightgray").grid(row=4, column=2)


    name_useval = StringVar()
    name_passval = StringVar()
    namepasss = name_passval.get()
    nameuser = name_useval.get()
    Entry(my,  textvariable=name_useval,bg="#e3e2e2", bd=0, highlightthickness=0,).grid(row=2, column=2)
    Entry(my,  textvariable=name_passval,bg="#e3e2e2", bd=0, highlightthickness=0,).grid(row=3, column=2)
    login = Button(my, text="Login", font=("sans serif", 12, ), bg="lightgray", bd=2,
                    highlightthickness=0, fg="Black", command=check).grid(row=10, column=2)

this is how the data are saved in a text file after users singup in singup window

('randomguy', '[email protected]', '901909210', 'randompassword')
('randomguy2', '[email protected]', '901978210', 'randompassword2')
1

There are 1 best solutions below

5
Alex Mandelias On

The statement

namepasss and nameuser in line

is actually evaluated as

namepasss and (nameuser in line)

due to operator precedence. Therefore, taking into consideration the laziness of the and operator, the (poorly written) order of evaluation is as follows:

if namepasss == True:
    if (nameuser in line) == True:
        return True
return False

Since namepasss is a string, anything but "" will evaluate to True.

What you really need is the following:

(namepasss in line) and (nameuser in line)

Without knowing what is written in the file and what values are assigned to namepasss and nameuser it's difficult to help more.