繁体   English   中英

检查python的用户名和密码(Tkinter)

[英]Checking username and password for python (Tkinter)

我正在使用 Tkinter 在 python 中创建一个登录系统,并且已经能够在我的注册窗口中使用用户名和密码写入文本文件。 但是,在我的登录窗口内,我不确定如何检查输入的用户名或密码是否正确?

#Register window code
    self.newNameEntry = Entry(self.master,
                     highlightcolor="grey",
                     highlightthickness=2,
                     highlightbackground="#FF4500")

    self.newNameEntry.place(x=245, y=100)

    self.newPasswordEntry = Entry(self.master,
                     highlightcolor="grey",
                     highlightthickness=2,
                     highlightbackground="#FF4500")

    self.newPasswordEntry.place(x=245, y=140)
def signUp(self):
    text = self.newNameEntry.get() + " " + self.newPasswordEntry.get() + "\n"
    with open("LoginDetails.txt", 'a')as f:
        f.write(text)
#Login window code
   self.nameEntry = Entry(self.master,
                     highlightcolor="grey",
                     highlightthickness=2,
                     highlightbackground="#FF4500")

   self.nameEntry.place(x=245, y=100)

   self.passwordEntry = Entry(self.master,
                     highlightcolor="grey",
                     highlightthickness=2,
                     highlightbackground="#FF4500")
   self.loginBtn = Button(self.master,
                   text="Login",
                   fg="white",
                   bg="#282828",
                   command = self.openHubWindow)#calls the function
   self.loginBtn.place(x=298, y=192)
   self.passwordEntry.place(x=245, y=140)
def openHubWindow(self): #Where I want the login checks to happen
          self.master.withdraw()
          root2 = Toplevel(self.master)
          hub = HubWindow.TheHub(root2)

您需要为自己设置一些变量,以便在需要时从输入框中获取值

self.userNameVar = StringVar()
self.passwordVar = StringVar()

然后在创建条目小部件时将这些添加为参数

self.nameEntry = Entry(self.master,
                       highlightcolor="grey",
                       textvariable=self.userNameVar,
                       highlightthickness=2,
                       highlightbackground="#FF4500")

self.passwordEntry = Entry(self.master,
                           highlightcolor="grey",
                           textvariable=self.passwordVar,
                           highlightthickness=2,
                           highlightbackground="#FF4500")

例如,您可以绑定并左键单击以检查您的用户名和密码的有效性

self.master.bind('<Return>', lambda e: self.checkValidUsernamePassword())

self.master.bind("<Button-1>", lambda e: self.checkValidUsernamePassword())

然后你写你的回调函数,你实际上去检查这些用户名/密码组合

def checkValidUserPass(self):
    user = self.userNameVar.get()
    attemptPassword = self.passwordVar.get()

    try:
        realPassword = self.users[user]
        if attemptPassword == realPassword:
             self.actuallyIsValidUser()  # They are good - do something

    except KeyError:  # That user name is not in our dictionary of users
        self.invaidUser()

def invalidUser():
    print("That is a bad user.")
    # Turn this into a message box, or other UX

但是您需要将其添加到您的init 中

def __init__(self):
    # .....
    self.users = {}  # empty dictionary
    self.loadUsers() # and load the user base
    # .....


def loadUsers(self):
    # Loadup all of the usernames and passwords into a dictionary
    with open("LoginDetails.txt", 'r') as f:
        for row in f:
            user, password = row.split(" ")
            self.users[user] = password # Puts this user & password into the dictionary

注意:您使用空格作为名称/密码的分隔符(错误,因为某些名称有空格)。 尝试使用另一个符号,例如 : 并将该符号弹出到您编写用户/密码的位置以及上面的 split() 中

尝试只发布有问题的代码;)

将其视为正常 - 从文本框中获取用户输入,将其分配给一个变量,然后检查用户名,然后添加第二个并为您的密码重复该过程。 所以在伪代码中:

PASSWORD = "password"
USERNAME = "user"
ACCESS = FALSE

if INPUT_ONE = PASSWORD then
    if INPUT_TWO = USERNAME then
        ACCESS = TRUE
    end if
end if

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM