首页 > 解决方案 > Python中使用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-f4,则main.py无需任何身份验证即可自动打开。关于如何解决这个问题的任何建议?

标签: pythonauthenticationtkinter

解决方案


推荐阅读