【问题标题】:Password Problems密码问题
【发布时间】:2014-03-17 11:53:55
【问题描述】:

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

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)

【问题讨论】:

  • passwordpassword_screen 中的一个局部变量。无法从GetValue 访问。
  • 那我该怎么解决这个问题?

标签: python tkinter passwords


【解决方案1】:

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

password = password_screen()
GetValue(password)

(作为旁注,在同一段代码中具有多种命名样式会让人难以阅读。它有助于根据大写和下划线/驼峰式大小写一致地命名函数)

【讨论】:

    猜你喜欢
    • 2015-06-20
    • 1970-01-01
    • 2011-04-01
    • 2014-08-03
    • 2020-01-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-05
    相关资源
    最近更新 更多