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)
You have a few things in your code that need some work but the main issue is in your
login_in()function.Your
ifstatement 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()andpassword = new[1].rstrip()you can usenameandpasswordto verify if the user has enter the correct credentials.So your
ifstatement should look like this:Your use of
in passdoc.txtdoes not do anything and cannot work becausepassdoc.txtto python looks like an undefined variable and would fail on the if statement. Remember that you imported all the contents ofpassdoc.txtasfso you didn't create a variable namedpassdocyou created one namedffor thewith open()statementIf you wanted to shorten your code a little you could remove
nameandpasswordvariables and just write the if statement withnewSo your
login_in()function could look like either this:Or this: