用tkinter更改登录页面的密码

时间:2019-06-22 19:55:00

标签: python tkinter

我正在尝试使用tkinter制作登录gui,并且我希望在单击更改密码时打开一个新窗口。

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

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()

1 个答案:

答案 0 :(得分:0)

首先使用window.destroy()销毁初始窗口,然后在通过按钮调用该函数时在函数中创建一个新窗口。使用root.destroy()删除更改密码窗口后,继续添加已知的条目小部件和标签并获取新密码,并运行另一个功能以返回初始页面。

以下是显示我刚才说的内容的简单代码:

from tkinter import *
from tkinter import messagebox

storedPassword = 'password'

def loginok(username,password):
    global storedPassword
    if username == "admin":
        if password == storedPassword:
            messagebox.showinfo("Login Successful!","Welcome Admin")
        else:
            messagebox.showerror("Login Failed!","Wrong Password")
    else:
        messagebox.showerror("Login Failed!","Wrong Username")

def cpass():
    global window
    window.destroy()
    window = Tk()
    Label(window,text="Enter new password:").pack()
    passn = Entry(window,width=15)
    passn.pack()
    Button(window,text="OK",command=lambda:chgpass(passn.get())).pack()
    window.mainloop()

def chgpass(newpass):
    global window
    global storedPassword
    storedPassword = newpass
    window.destroy()
    login()

def login():
    global window
    window = Tk()
    Label(window,text="Username:").pack()
    usern = Entry(window,width=15)
    usern.pack()
    Label(window,text="Password:").pack()
    passw = Entry(window,width=15,show="*")
    passw.pack()
    Button(window,text="Login",command=lambda:loginok(usern.get(),passw.get())).pack()
    Button(window,text="Forgot",command=cpass).pack()
    window.mainloop()

login()