[英]Checking username and password for python (Tkinter)
I am creating a login system in python using Tkinter and have been able, write to the text file with a username and password in my Register window.我正在使用 Tkinter 在 python 中创建一个登录系统,并且已经能够在我的注册窗口中使用用户名和密码写入文本文件。 However, inside of my Login window, I am not sure how to go about checking whether or not the username or password entered is correct or not?
但是,在我的登录窗口内,我不确定如何检查输入的用户名或密码是否正确?
#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)
You need to set yourself up with some variables that can grab the values out of the Entry boxes when you want您需要为自己设置一些变量,以便在需要时从输入框中获取值
self.userNameVar = StringVar()
self.passwordVar = StringVar()
Then add these as parameters when creating the entry widgets然后在创建条目小部件时将这些添加为参数
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")
For example you could bind and Left-click to check for the validness of your user name and password例如,您可以绑定并左键单击以检查您的用户名和密码的有效性
self.master.bind('<Return>', lambda e: self.checkValidUsernamePassword())
self.master.bind("<Button-1>", lambda e: self.checkValidUsernamePassword())
Then you write your callback function where you actually go about checking these username / password combos然后你写你的回调函数,你实际上去检查这些用户名/密码组合
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
But you will need to add this to your init :但是您需要将其添加到您的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
Note: You used a space as the deliminator on names/passwords (bug because some names have spaces).注意:您使用空格作为名称/密码的分隔符(错误,因为某些名称有空格)。 Try using another symbol like a : and pop that symbol into where you write the user/pass and in the split() above
尝试使用另一个符号,例如 : 并将该符号弹出到您编写用户/密码的位置以及上面的 split() 中
Try to only post your code which is in question ;)尝试只发布有问题的代码;)
Treat it as normal - get the user input from a text box, assign it to a variable, then check that for the username, and then add a second and box repeat that process for your password.将其视为正常 - 从文本框中获取用户输入,将其分配给一个变量,然后检查用户名,然后添加第二个并为您的密码重复该过程。 So in Pseudo code:
所以在伪代码中:
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.