我做了这个简单的gui,它要求你注册或登录。当我们注册我们的详细信息时,我们输入的条目存储在SQL数据库中。当u登录时,它获取username条目的值并检查该用户名是否存在于SQL表中。如果存在,则检查我们输入的密码是否与相应的用户名匹配。
def login_submit():
n1 = en_username.get()
n2 = en_password.get()
q1 = "select password from data where username = '{}';".format(n1)
cursor.execute(q1)
r = cursor.fetchone()
if r:
if r == n2:
success = messagebox.showinfo(title="Logged in", message="Logged in Succesfully")
elif r != n2:
wrong_pas = messagebox.showerror(title="Failed to login", message="Wrong password")
else:
fail = messagebox.showerror(title="Error", message="No such account exists")
firstwindow()
def login():
global login_win
global en_username
global en_password
firstwin.destroy()
login_win= Toplevel()
login_win.title("Login")
icon= ImageTk.PhotoImage(file= "C://Users//Win10//photos//icon.jpg")
login_win.iconphoto(False, icon)
login_win.geometry("300x140")
login_username = Label(login_win, text="Username: ", anchor=W, font=("Arial", 12))
login_username.pack(anchor=W, ipady= 10)
login_passwd = Label(login_win, text="Password: ", anchor=W, font=("Arial", 12))
login_passwd.pack(anchor=W, ipady=10)
submit_btn = Button(login_win, text="Submit", command= login_submit).pack(anchor=S)
goback = Button(login_win,text="Go Back", command= go_lback).pack(anchor=S)
en_username = Entry(login_win, bd=3, relief=GROOVE, font=("Arial", 9))
en_username.place(x= 90, y= 10)
en_password = Entry(login_win, bd=3, relief=GROOVE, font=("Arial", 9), show="*")
en_password.place(x= 90, y= 55)
但是每当我尝试登录时,即使我输入了正确的密码,也会弹出错误的密码提示框。
您正在将密码值(n2)与从数据库获取的元组(r)进行比较,该元组的计算结果始终为False,因此每次都失败。
你可以这样做:
def login_submit():
n1 = en_username.get()
n2 = en_password.get()
q1 = "select password from data where username = '{}';".format(n1)
cursor.execute(q1)
r = cursor.fetchone()
if r:
if r[0] == n2: # Compare the password with the first element of the tuple
success = messagebox.showinfo(title="Logged in", message="Logged in Successfully")
else:
wrong_pas = messagebox.showerror(title="Failed to login", message="Wrong password")
else:
fail = messagebox.showerror(title="Error", message="No such account exists")
firstwindow()