-1

I'm making a program to login or register an account. But when I tried to read a text file to check for the username and password, file.read() returned nothing for some reason. Here is the code:

def login_incorrect():
    Label(loginPage, text='Username or password incorrect.').place(x=120, y=120)
def LoginToAccount():
    with open('AccountDatabase.txt', 'r'):
        if loginUsernameE.get() + '.' + loginPasswordE.get() not in open('AccountDatabase.txt').read():
            login_incorrect()
        else:
            print('Logged in!')

This program will always give me the 'password incorrect' message, because open('AccountDatabase.txt', 'r'): always returns a blank line.

Here is my full code:

from tkinter import *
import time
root = Tk()
root.title("Account Signup")
DarkBlue = "#2460A7"
LightBlue = "#B3C7D6"
root.geometry('350x230')
LoggedIn = False
Menu = Frame()
loginPage = Frame()
registerPage = Frame()
for AllFrames in (Menu, loginPage, registerPage):
    AllFrames.place(relwidth=1, relheight=1)  # w/h relative to size of master
    AllFrames.configure(bg=LightBlue)
root.grid_rowconfigure(0, weight=1)
root.grid_columnconfigure(0, weight=1)
def show_frame(frame):
    frame.tkraise()
show_frame(Menu)

def login_incorrect():
    Label(loginPage, text='Username or password incorrect.').place(x=120, y=120)
def LoginToAccount():
    with open('AccountDatabase.txt', 'r'):
        if loginUsernameE.get() + '.' + loginPasswordE.get() not in open('AccountDatabase.txt').read():
            login_incorrect()
        else:
            print('Logged in!')
def CreateNewAccount():
    print('create new account')
    while True:  # This loop will run as long as the new account hasn't been created.
        with open('AccountDatabase.txt'):
            if len(newUsernameE.get()) < 4:
                lenError = Label(text='Username must be 4 characters or more.')
                print('4')
                lenError.place(x=120, y=120)
            if newUsernameE.get() + "." in open('AccountDatabase.txt').read():
                print('username taken')
                # newUsername = input("Sorry, this username is already taken. Please choose another username:")
                continue
            if newUsernameE.get() + '.' not in open('AccountDatabase.txt').read():
                print('create pass')
                AccountDatabase.write(newUsernameE.get() + "." + newPasswordE.get() + "\n")
                break

# ============= Menu Page =========

menuTitle = Label(Menu, text="Menu", font=("Arial", 25), bg=LightBlue)
menuTitle.place(x=130, y=25)

loginMenuButton = Button(Menu, width=25, text="Login", command=lambda: show_frame(loginPage))
loginMenuButton.place(x=85, y=85)

registerMenuButton = Button(Menu, width=25, text="Register", command=lambda: show_frame(registerPage))
registerMenuButton.place(x=85, y=115)

# ======== Login Page ===========

loginUsernameL = Label(loginPage, text='Username')
loginUsernameL.place(x=30, y=60)
loginUsernameE = Entry(loginPage)
loginUsernameE.place(x=120, y=60)
loginPasswordL = Label(loginPage, text='Password')
loginPasswordL.place(x=30, y=90)
loginPasswordE = Entry(loginPage)
loginPasswordE.place(x=120, y=90)
backButton1 = Button(loginPage, text='Back', command=lambda: show_frame(Menu))
backButton1.place(x=0, y=0)
loginButton = Button(loginPage, text='Login', width=20, command=LoginToAccount)
loginButton.place(x=100, y=150)

# ======== Register Page ===========

newUsernameL = Label(registerPage, text='New Username')
newUsernameL.place(x=43, y=60)
newUsernameE = Entry(registerPage)
newUsernameE.place(x=140, y=60)
newPasswordL = Label(registerPage, text='New Password')
newPasswordL.place(x=45, y=90)
newPasswordE = Entry(registerPage)
newPasswordE.place(x=140, y=90)
confirmPasswordL = Label(registerPage, text='Confirm Password')
confirmPasswordL.place(x=25, y=120)
confirmPasswordE = Entry(registerPage)
confirmPasswordE.place(x=140, y=120)
backButton2 = Button(registerPage, text='Back', command=lambda: show_frame(Menu))
backButton2.place(x=0, y=0)
registerButton = Button(registerPage, text='Login', width=20, command=CreateNewAccount)
registerButton.place(x=100, y=180)

root.mainloop()

BluBalloon
  • 75
  • 6
  • 5
    Why are you opening the file again while it's already open from the `with` block? – John Gordon Dec 17 '22 at 23:31
  • To check whether there's a string in the file. How else would I check for a string? – BluBalloon Dec 17 '22 at 23:33
  • 1
    You would check for a string by opening the file _once_ and calling `read()`. There's no need to do it twice. However, I don't think this is causing the error. – John Gordon Dec 17 '22 at 23:38
  • What do you think is causing the error? – BluBalloon Dec 17 '22 at 23:39
  • 2
    `open('AccountDatabase.txt')` looks for the file in the current directory. It could be that you actually have two copies of that file, and the one in the current directory really does not have the expected contents. – John Gordon Dec 17 '22 at 23:44
  • 1
    Change the `LoginToAccount` function to save the file contents in a variable. Then you can check for the expected username and also print it, as a debugging aid. – John Gordon Dec 17 '22 at 23:47
  • 1
    You can also print `os.getcwd()` to confirm that the current directory is what you expect. – John Gordon Dec 17 '22 at 23:48
  • What's the output of putting `print([loginUsernameE.get(), loginPasswordE.get(), open('AccountDatabase.txt').read()])` before the `if` line? – Kelly Bundy Dec 17 '22 at 23:49
  • @JohnGordon Thank you very much, I think this is the error. I tried to fill the file and print it in the beginning of my program, and it returned nothing. I will try to find the duplicate file – BluBalloon Dec 17 '22 at 23:53

2 Answers2

0

replace this

with open('AccountDatabase.txt', 'r'):
    if loginUsernameE.get() + '.' + loginPasswordE.get() not in open('AccountDatabase.txt').read():

to

with open('AccountDatabase.txt', 'r') as f:
    if loginUsernameE.get() + '.' + loginPasswordE.get() not in f.read():
        login_incorrect()
    else:
        print('Logged in!')
Nikappa_
  • 127
  • 4
0
def LoginToAccount():
    with open('AccountDatabase.txt', 'r') as f:
        if loginUsernameE.get() + '.' + loginPasswordE.get() not in f.read():
            login_incorrect()
        else:
            print('Logged in!')

and here we open the file in append and in read mode we read the file and make sure that the user isn't already there and append the new user.password if the user isn't in the database

def CreateNewAccount():
    print('create new account')
    while True:  # This loop will run as long as the new account hasn't been created.
        with open('AccountDatabase.txt', 'r') as fr, open('AccountDatabase.txt', 'a') as fa:
            if len(newUsernameE.get()) < 4:
                lenError = Label(text='Username must be 4 characters or more.')
                print('4')
                lenError.place(x=120, y=120)
            if newUsernameE.get() + "." in fr.read():
                print('username taken')
                # newUsername = input("Sorry, this username is already taken. Please choose another username:")
                continue
            else:
                print('create pass')
                fa.write(newUsernameE.get() + "." + newPasswordE.get() + "\n")
                break

Hanna
  • 1,071
  • 1
  • 2
  • 14