How to create password system in tkinter reading the data from file

1.1k Views Asked by At

I want to create password system in tkinter with notepad as my DB which contains the data in my working directory but when I insert data in the entry fields I receive an error login failed.Have created the txt file but it seems the function can't read from the file.Any suggestion on how to do this.

import tkinter as tk
import sys
from tkinter import messagebox


now = open("passdoc.txt","w+")
now.write("user\n")
now.write("python3")
now.close()

def login_in():
    with open("passdoc.txt") as f:
        new = f.readlines()
        name = new[0].rstrip()
        password = new[1].rstrip()
    if entry1.get() == new[0] in passdoc.txt and entry2.get() == new[1] in 
passdoc.txt:
        root.deiconify()
        log.destroy()
    else:
        messagebox.showerror("error","login Failed")


def close():
    log.destroy() #Removes toplevel window
    root.destroy() #Removes  root window
    sys.exit() #Ends the script


root=tk.Tk()
log = tk.Toplevel() #

root.geometry("350x350")
log.geometry("200x200")

entry1 = tk.Entry(log) #Username entry
entry2 = tk.Entry(log) #Password entry
button1 = tk.Button(log, text="Login", command=login_in) #Login button
button2 = tk.Button(log, text="Cancel", command=close) #Cancel button
label1 = tk.Label(root, text="tkinter password system")

entry1.pack()
entry2.pack()
button1.pack()
button2.pack()
label1.place(x=30,y=300)


label = tk.Label(root, text="welcome").pack()

root.withdraw()
root.mainloop()

I created this functions too but all seems not to work out for me

def login_in():
    with open("passdoc.txt") as f:
        new = f.readlines()
        name = new[0].rstrip()
        password = new[1].rstrip()
    if entry1.get() == name in passdoc.txt and entry2.get() == password in 
passdoc.txt:
        root.deiconify()
        log.destroy()
    else:
        messagebox.showerror("errror","login failed")    #error login failed 
(corrections)
1

There are 1 best solutions below

1
On BEST ANSWER

You have a few things in your code that need some work but the main issue is in your login_in() function.

Your if statement is all wrong. I am not sure why you wrote it the way you did but lets fix it.

Because you defined name = new[0].rstrip() and password = new[1].rstrip() you can use name and password to verify if the user has enter the correct credentials.

So your if statement should look like this:

if entry1.get() == name and entry2.get() == password:

Your use of in passdoc.txt does not do anything and cannot work because passdoc.txt to python looks like an undefined variable and would fail on the if statement. Remember that you imported all the contents of passdoc.txt as f so you didn't create a variable named passdoc you created one named f for the with open() statement

If you wanted to shorten your code a little you could remove name and password variables and just write the if statement with new

So your login_in() function could look like either this:

def login_in():
    with open("passdoc.txt") as f:
        new = f.readlines()
        name = new[0].rstrip()
        password = new[1].rstrip()
    if entry1.get() == name and entry2.get() == password:
        root.deiconify()
        log.destroy()
    else:
        messagebox.showerror("error","login Failed")

Or this:

def login_in():
    with open("passdoc.txt") as f:
        new = f.readlines()
    if entry1.get() == new[0].rstrip() and entry2.get() == new[1].rstrip():
        root.deiconify()
        log.destroy()
    else:
        messagebox.showerror("error","login Failed")