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

时间:2017-01-11 18:05:39

标签: python authentication login tkinter username

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

var arr = [ 'com--test,LFutx9mQbTTyRo4A9Re5ksjdnfsI4cKN4q2,on',
  'com--test,LFutx9mQbTTyRo4A9Re5ksjdnfsI4cKN4q2,off',
  'com--fxtrimester,SEzlksdfMpW3FxkSbzL7eo5MmqkPczCl2,on',
  'com--fxtrimester,LFutx9mQbTTyRoldksfns4A9Re5I4cKN4q2,on',
  'com--fxtrimester,LFutx9mQbTTyRoldksfns4A9Re5I4cKN4q2,off',
  'com--fxyear,SEzlksdfMpW3FxkSbzL7eo5MmqkPczCl2,on',
  'com--fxyear,SEzlksdfMpksfhksfMmqkPczCl2,off' ];

var removeOffOrOnFromStr = function(str){
  return str.split(",", 2).toString()
}

var removeOffFromArray = function(arr, first, second){
  var off = first;
  if(second[second.length - 1] == "f"){
    off = second;
  }
  arr.splice(arr.indexOf(off), 1)
}

var filtered = arr.slice()

arr.sort(function(first, second){
  var removedOnOrOff1 = removeOffOrOnFromStr(first)
  var removedOnOrOff2 = removeOffOrOnFromStr(second)
  var thirdValueDiff = false
  if(first[first.length - 1] != second[second.length - 1]){
    thirdValueDiff = true
  }
  
  if(removedOnOrOff1 == removedOnOrOff2 && thirdValueDiff ){
    removeOffFromArray(filtered, first, second)
  }
});

console.log(filtered)

2 个答案:

答案 0 :(得分:2)

您需要为自己设置一些变量,这些变量可以在您想要

时从条目框中获取值
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()中

答案 1 :(得分:-1)

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

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

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

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