用tkin更改登录页面密码

2021-04-12 00:19:19 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试使用tkinter创建一个登录gui,我希望在单击changepassword时打开一个新窗口。在

当窗口打开时,它应该提示用户输入新密码,然后更改并存储密码。之后,它应该使用更改后的密码再次登录。在

import tkinter as tk
import tkinter.messagebox as tm



class LoginFrame(tk.Frame):
    storedPassword='password'
    def __init__(self, master):
        super().__init__(master)
        #storedPassword='password'
        self.label_username = tk.Label(self, text="Username")
        self.label_password = tk.Label(self, text="Password")



        self.entry_username = tk.Entry(self)
        self.entry_password = tk.Entry(self)#, show="*") to help us see new password

        self.label_username.grid(row=0)
        self.label_password.grid(row=1)
        self.entry_username.grid(row=0, column=1)
        self.entry_password.grid(row=1, column=1)

        self.logbtn = tk.Button(self, text="Login", command=self._login_btn_clicked)
        self.logbtn.grid(columnspan=2)
        newPassword = self.entry_password.get()
        print(newPassword)
        self.logbtn = tk.Button(self, text="change_password", command=self.change_password)
        self.logbtn.grid(columnspan=2)


    def change_password(self,newPassword):
        self.new_password = tk.Label(self, text="new password")
        self.entry_newpassword = tk.Entry(self)
        self.storedPassword=newPassword
        print(self.entry_newpassword.get())
    def _login_btn_clicked(self):

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



        if username == "admin":
            if password == self.storedPassword:
                tm.showinfo("Login info", "Welcome admin")
                self.storedPassword=self.entry_password.get()

            else:
                tm.showerror("Login error", "Incorrect password")
        else:
            tm.showerror("Login error", "Incorrect username")

window= tk.Tk()
lf = LoginFrame(window)
#root = tk.Tk()
#newWindow= LoginFrame(root)

#root.mainloop()
window.mainloop()