使用 Tkinter 的 Python 身份验证系统

时间:2020-12-21 10:12:28

标签: python authentication tkinter

我对 python 中的身份验证系统很好奇。所以我开始在 tkinter 模块的帮助下构建一个。我的身份验证系统工作正常,但我遇到了问题。

这是主页面:

from tkinter import *
import SignIn.AccountsPage

if SignIn.AccountsPage.Access:
    # Initialising tkinter
    main = Tk()
    main.title("Home")
    main.geometry("{0}x{1}+0+0".format(main.winfo_screenwidth(), main.winfo_screenheight()))
    main.bind("<Escape>", lambda x: x.widget.geometry("400x200"))
    main.bind("<KeyPress-f>",
              lambda x: x.widget.geometry(
                  "{0}x{1}+0+0".format(main.winfo_screenwidth(), main.winfo_screenheight())))

    # Frames for widgets
    LeftFrame = LabelFrame(main, padx=100, pady=20)
    LeftFrame.place(x=0, y=0, height=main.winfo_screenheight(), width=(main.winfo_screenwidth() * 0.277))

    TopFrame = LabelFrame(main, padx=10, pady=10)
    TopFrame.place(x=main.winfo_screenwidth() * 0.277, y=0, height=(main.winfo_screenheight() / 2),
                   width=(main.winfo_screenwidth() - main.winfo_screenwidth() * 0.277))

    BottomFrame = LabelFrame(main, padx=10, pady=10)
    BottomFrame.place(x=main.winfo_screenwidth() * 0.277, y=main.winfo_screenheight() / 2,
                      height=(main.winfo_screenheight() / 2),
                      width=(main.winfo_screenwidth() - main.winfo_screenwidth() * 0.277))

    main.mainloop()
else:
    sys.exit()

这是登录页面:

from tkinter import *

from SignIn import LoginModule as LM, EncryptionModule as EM
from SignIn.LoginModule import CommonPassword

Access = False

# Creating Database
EM.CredentialSetUp()


# Initializing
root = Tk()
root.title("Login")

# Entry Widget
e = Entry(root, width=50, borderwidth=7)
e.grid(row=0, column=1, columnspan=4)

f = Entry(root, show='*', width=50, borderwidth=7)
f.grid(row=2, column=1, columnspan=4)


# Functions
def login():
    searchu = e.get()
    searchp = f.get()
    if LM.AlmostThere(searchu, searchp):
        root.destroy()
        logger.info("{} has logged in to Systaurant".format(searchu))
        return True

    else:
        messagebox.showerror("Login Denied", "Oops looks like you have entered a wrong username or password!")
        logger.info("{} has been denied access to Systaurant".format(searchu))
        return False

# Buttons and Labels
Username = Label(root, text="Username:")
Username.grid(row=0, column=0)

Password = Label(root, text="Password:")
Password.grid(row=2, column=0)

Login = Button(root, text="Login", command=login)
Login.grid(row=3, column=1)

Exit = Button(root, text="Quit", command=exit1)
Exit.grid(row=3, column=2)

root.bind('<Return>', lambda event=None: Login.invoke())
root.bind('<Escape>', lambda event=None: Exit.invoke())

if login:
    Access = True
else:
    Access = False

root.mainloop()

AccountsPage.py 文件中:

所以基本上,这里发生的事情是用户在 entry 小部件中输入他/她的用户名和密码。然后 login() 函数检查它是否有效。如果 login()False,则 Access = False,如果 login()True,则 Access = True

然后在 main.py 文件中:

如果 Access = True 则显示帧。 否则调用 sys.exit()

如果使用得当,系统运行良好,但我发现了一个错误。

如果通过按下关闭按钮或 AccountsPage.py 关闭 Alt-f4main.py 会自动打开,无需任何身份验证。有关如何解决此问题的任何建议?

0 个答案:

没有答案