Home > Enterprise >  read if name and password are in the same line in a txt file
read if name and password are in the same line in a txt file

Time:02-17

I’m creating a login with registration where the data is saved in a txt file, and a login. but now if I login with name and password not matched, I still login.

in the txt file shows only username ( , ) and the password entered in the registration

this is the code:

    from tkinter import *
    import numpy as np
    import ast

    root = Tk()
    root.geometry("500x300")
    root.title("form")


    def signup():

      # verificare se un utente gia esiste

        registro_fatto.configure(text="Completata")

        file1 = open("provafile.txt", "a")
        file1.write(nome_utente_entry.get())
        file1.write(", ")
        file1.write(password_entry.get())
        file1.write("\n")
        file1.close()

        nome_utente_entry.delete(0, END)
        password_entry.delete(0, END)


    def signin():
        global nome_utente_entry
        global password_entry

        file = open('provafile.txt', 'r')
        file.readlines()
        file.close()

    if nome_utente_entry.get() and password_entry.get() in file:
        accesso_fatto.configure(text="accesso eseguito")
    elif nome_utente_entry.get() and password_entry.get() != file:
        accesso_fatto.configure(text="accesso negato")
    else:
        accesso_fatto.configure(text="Nome utente e password negato")

    nome_utente_entry.delete(0, END)
    password_entry.delete(0, END)



   #input nome utente
nome_utente = Label(root, text="Nome", font="Times 10 bold")
nome_utente.grid(row=1, column=1, padx=60)
nome_utente_entry = Entry(root)
nome_utente_entry.grid(row=1, column=2)

#input password
password = Label(root, text="Password", font="Times 10 bold")
password.grid(row=2, column=1)
password_entry = Entry(root, show="*")
password_entry.grid(row=2, column=2)

#bottone per accedere
accedi_button = Button(root, text="accedi", command=signin)
accedi_button.grid(row=3, column=1)
accedi_button.configure(cursor="hand2")
accesso_fatto = Label(root, text="", font="Times 10 bold")
accesso_fatto.grid(row=4, column=1)


#bottone per registrarsi
registrati_button = Button(root, text="registrati", command=signup)
registrati_button.grid(row=3, column=2)
registrati_button.configure(cursor="hand2")
registro_fatto = Label(root, text="", font="Times 10 bold")
registro_fatto.grid(row=4, column=2)


    if __name__ == '__main__':
        root.mainloop()

Please Can you tell me how to check if username and password are in the same line as the txt file?

CodePudding user response:

@matteopet, you can use this line of code for solution, but you have to improve it for further solution. Change the two functions by:

def signup():
    # verificare se un utente gia esiste

    if (nome_utente_entry.get() != '' and password_entry.get() != ''):
        registro_fatto.configure(text="Completata")
        file1 = open("provafile.txt", "a")
        file1.write(nome_utente_entry.get())
        file1.write(", ")
        file1.write(password_entry.get())
        file1.write("\n")
        file1.close()

        nome_utente_entry.delete(0, END)
        password_entry.delete(0, END)
    else:
        registro_fatto.configure(text="Provide data")


def signin():
    global nome_utente_entry
    global password_entry

    file = open('provafile.txt', 'r')
    match = 0
    if(nome_utente_entry.get() == '' or password_entry.get() == ''):
            accesso_fatto.configure(text="Nome utente e password negato")
    else:
        for line in file.readlines():
            nome, password = line.strip().split(",")
            if(nome.strip() == nome_utente_entry.get() and password.strip() == password_entry.get()):
                match  =1
                break
            else:
                continue
        if match == 1:
            accesso_fatto.configure(text="accesso eseguito")
        elif match == 0:
            accesso_fatto.configure(text="accesso negato")

    nome_utente_entry.delete(0, END)
    password_entry.delete(0, END)
    file.close()
  • Related