如何正确地将用户信息附加到json文件并进行搜索?

时间:2018-09-27 18:20:10

标签: python json python-3.x user-interface authentication

我正在处理的程序将充当较大程序的登录屏幕。现在,程序要求输入用户名和密码。如果用户名和密码与文件中的密码匹配,则它将登录用户。如果密码错误,则提示用户重试。而且,如果用户名是新用户名,则它将用户名添加到列表中。

问题:当程序添加新的用户名/密码组合时,它会以防止程序搜索潜在的用户名和密码匹配的方式进行操作。

我的代码:     从tkinter导入*     导入json     导入时间     类LoginFrame(Frame):         def init (自我,主人):             super()。初始化(主)             master.title(“登录”)

        self.label_username = Label(self, text="Username ", font=("Helvetica", 25))
        self.label_password = Label(self, text="Password ", font=("Helvetica", 25))

        self.entry_username = Entry(self, font=("Helvetica", 25))
        self.entry_username.focus()     #This sets the focus to the username entry box
        self.entry_password = Entry(self, font=("Helvetica", 25), 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", font=("Helvetica", 25), command=self.Login)
        self.login_button.grid(row=2, columnspan=2)

        self.grid()

    def Login(self):
        account_file_path = "C:\\LearningArabic\\LiblibArriby\\Usernames\\accounts.json"
        with open (account_file_path, "r") as f:
            current_info = json.load(f)

        username = self.entry_username.get()
        password = self.entry_password.get()

        saved_password = current_info.get("password")
        saved_username = current_info.get("username")

        if username in current_info["username"]:
            print(saved_username, saved_password)
            if password in current_info["password"]:
                L1 = Label(self, text=" You have successfully  \n logged in.", font=("Helvetica", 25))
                L1.grid(row=3, column=0, columnspan=2)

            else:
                L2 = Label(self, text="You have entered either your username \n or password incorrectly. \n Please try again.", font=("Helvetica", 15))
                L2.grid(row=3, column=0, columnspan=2)
        else:       #This section appends the new username and password combo to the json file
            L1 = Label(self, text=" You have successfully  \n added a new user.", font=("Helvetica", 25))
            L1.grid(row=3, column=0, columnspan=2)            
            usr = {"username": username,
                   "password": password,
                   "accessable modules": {
                       "lesson 1": [
                           "part 1"
                            ]
                           }}

            with open(account_file_path, "a") as test:
                current_info.update(usr)
                json.dump(current_info, test)

root = Tk()
lf = LoginFrame(root)
root.mainloop()

初始JSON文件:

{"username": "username", "password": "a", "accessable modules": {"lesson 1": ["part 1", "part 2", "part 3", "part 4"]}}

添加新用户后的JSON文件:

{"username": "username", "password": "a", "accessable modules": {"lesson 1" "part 1", "part 2", "part 3", "part 4"]}}{"username": "usernamed", "password": "a", "accessable modules": {"lesson 1": ["part 1"]}}

我想找到一种方法来正确添加新用户,而不是替换旧用户。我计划稍后使用“可用模块”部分来确定哪些菜单项可供用户使用。我还应该注意,该程序仅旨在在本地计算机上运行,​​因此我不必担心密码安全性。实际上,我几乎都没有添加它们。

0 个答案:

没有答案