我正在编写一个要求用户名和密码的登录屏幕。一旦我让这个屏幕正常工作,我计划将它添加到我正在处理的更大程序的开头。
我目前遇到的问题是我无法验证特定用户的密码。现在,程序将检查用户名是否已存在,如果存在,则将检查密码是否存在。问题是有人可以输入另一个用户名和自己的密码,然后登录到其他人的帐户。安全性对我的程序来说不是什么大问题(我只是在制作一个多邻国类型的语言应用程序),但这是一个非常明显的问题,我想弄清楚如何解决。
from tkinter import *
import json
class LoginFrame(Frame):
def __init__(self, master):
super().__init__(master)
master.title("Login")
self.label_username = Label(self, text="Username")
self.label_password = Label(self, text="Password")
self.entry_username = Entry(self)
self.entry_username.focus() #This sets the focus to the username entry box
self.entry_password = Entry(self, show="*")
self.label_username.grid(row=0, column=0)
self.label_password.grid(row=1, column=0)
self.entry_username.grid(row=0, column=1)
self.entry_password.grid(row=1, column=1)
self.login_button = Button(self, text="Login", command=self.Login)
self.login_button.grid(columnspan=2)
self.grid()
def Login(self):
current_info = open ("C:\\LearningArabic\\LiblibArriby\\Usernames\\usernames.json").read()
username = self.entry_username.get()
password = self.entry_password.get()
if username in current_info:
print("Test")
if password in current_info:
print("Now we're talking")
else:
print("Well, you're trying")
else: #This section appends the new username and password combo to the json file
usr = {username: password}
with open("C:\\LearningArabic\\LiblibArriby\\Usernames\\usernames.json") as a:
data = json.load(a)
data.update(usr)
with open("C:\\LearningArabic\\LiblibArriby\\Usernames\\usernames.json", "w") as a:
json.dump(data, a)
root = Tk()
lf = LoginFrame(root)
root.mainloop()
任何帮助将不胜感激,如果对代码有其他评论,请不要退缩。我想学习——不仅仅是得到答案!
三国纷争
相关分类