Login verification fails while checking for userid, password from text file

Viewed 86

I am currently making a python GUI with a login verification. I am using a text file to verify the login details. When I try to compare the userid and password against data from the file, it goes into the else condition and prints "Fail".

I am using tkinter for the GUI, but the login details do not work.

from tkinter import *

def verify_login():
    file = open("login.txt","r")
    for row in file:
        field = row.split(",")
        if username.get() == field[0] and password.get() == field[1]:
            print("Correct!")
        else:
            print("Fail")

### LOGIN SCREEN ###
def LoginPage():
    global username
    global password
    login_screen=Tk()
    login_screen.title("Login")
    login_screen.geometry("300x250")
    Label(login_screen, text="Please enter login details").pack()
    Label(login_screen, text="").pack()
    Label(login_screen, text="Username").pack()
    username=StringVar()
    password=StringVar()
    username_login = Entry(login_screen, textvariable=username)
    username_login.pack()
    Label(login_screen, text="").pack()
    Label(login_screen, text="Password").pack()
    password_login= Entry(login_screen, textvariable=password, show= '*')
    password_login.pack()
    Label(login_screen, text="").pack()
    Button(login_screen, text="Login", width=10, height=1,command=verify_login).pack()
    login_screen.mainloop(

      

### MAIN SCREEN ###
def Startpage():
    global gui
    gui=Tk(className="Login Form")
    gui.geometry("500x200")
    button = Button(gui, text='Login', width=20, height=3, bg='#0052cc', fg='#ffffff', activebackground='#0052cc', activeforeground='#aaffaa', command=LoginPage).pack()
    gui.mainloop()
Startpage()


my text file has logins and passwords in this format: login,a,yex,joe,morgan,1,sanjay,2

3 Answers

maybe username and password have tailing whitespaces, like sanjay and sanjay are not same.

to clean it up you can do this before comparing:

username.get().rstrip()

password.get().rstrip()

this rstrip() function will remove the tailing whitespaces.

also, make sure, your file has every username password pair in a new line.

then you can do this:

for row in file.readlines():

so, your verify_login() function should be like this:

def verify_login():
    file = open("login.txt","r")
    for row in file.readlines():
        field = row.split(",")
        if username.get().rstrip() == field[0] and password.get().rstrip() == field[1]:
            print("Correct!")
        else:
            print("Fail")

your text file format

login,a,yex,joe,morgan,1,sanjay,2

apears to have the type of values stored in the first word login

this means when you access item 0 from the list you are getting "login" instead of "a"

you should change the index you try to acces to 1 & 2 instead of 0 & 1

def verify_login():
file = open("login.txt","r")
for row in file:
    field = row.split(",")
    if username.get() == field[1] and password.get() == field[2]:
        print("Correct!")
    else:
        print("Fail")

The below should work for you.

verify_login is a boolean function now.

def verify_login(user_name,password):
    with open("login.txt","r") as f:
      for row in f:
          row = row.strip()
          field = row.split(",")
          if user_name == field[0] and password == field[1]:
              return True
      return False

print(verify_login('Jack','pp'))
print(verify_login('usr1','pwd1'))

login.txt

usr1,pwd1
usr5,pwd5

output

False
True
Related