密码问题

时间:2014-03-17 11:53:55

标签: python tkinter passwords

您好,我想弄明白我的代码有什么问题。我正在尝试创建一个密码应用程序,当我单击一个按钮时,将打开窗口以在两个密码匹配时插入配方,如果它们不匹配则会打开一个错误对话框。但是,当我尝试运行它时,它不断出现错误,说明没有定义密码。请有人帮忙吗?

def password_screen():

    password = Tk()
    password.title("Password")

    lbl1 = Label(password, text="Please enter the password to access the program's code.")
    lbl1.grid(row = 0, column = 0, sticky=W)

    Label(password, text = "Password").grid(row = 1, column = 0, sticky = W)
    Label(password, text = "Repeat Password").grid(row = 2, column = 0, sticky = W)

    btna = Button(password, text = "Continue", command=GetValue)
    btna.grid(row = 2, column = 2, padx = 5)

    e4 = Entry(password)
    e5 = Entry(password)

    e4.grid(row=1, column=1)
    e5.grid(row=2, column=1)

def GetValue():

    global e4, e5

    e4 = Entry(password)
    e5 = Entry(password)

    pass1 = e4.get()
    pass2 = e5.get()

    if pass1 == pass2:
        btna.configure(command=insert_new_recipe)
    else:
        btna.configure(command=dialog)

1 个答案:

答案 0 :(得分:0)

正如qwe所提到的,密码是password_screen函数的本地密码。一种解决方案是将return password添加到password_screen函数的末尾,然后将其传递给GetValue。我认为你也不需要global e4, e5。类似的东西:

password = password_screen()
GetValue(password)

(作为旁注,在同一段代码中有多个命名样式令人困惑阅读。它有助于在大写和下划线/ camelCase等方面统一命名函数)