python中的Tkinter:未解析的引用

时间:2015-11-03 01:47:03

标签: python python-3.x tkinter

所以我做了一个简单的登录和注册页面,一切顺利,直到我尝试从我的注册页面的定义中获取变量。 Python说它是一个未解决的参考,应该解决打字匹配和所有,但也许有人可以帮助。另外我知道有一种更简单的方法来通过使用类添加页面,但我决定这样做。提前谢谢。 p.s错误在sign_up_check()代码中,变量是从sign_up()定义中获得的。

import tkinter

# window set up

window = tkinter.Tk()
window.geometry("300x200")
window.title("Game")
window.configure(bg="grey26")

# username label

lbl = tkinter.Label(window, text="username", bg="gold")
enter_username = tkinter.Entry(window, bg="indian red1")
lbl.pack()
enter_username.pack()

# password setup

label_password = tkinter.Label(window, text="password", bg="gold")
enter_password = tkinter.Entry(window, bg="indian red1")
label_password.pack()
enter_password.pack()


# sign in


def log_in():
    username = enter_username.get()
    password = enter_password.get()
    if len(username) > 0 and len(password) > 0:
        print("it passed")
    else:
        print("Log in error: Double check username and password")


def sign_up_check():
    # init vars
    username = signup_username.get()
    password = signup_pasword.get()
    age = signup_age.get()
    # check if vars greater than one
    if len(username) > 0 and len(password) > 0 and age > 0:
        print("this all passed")
    else:
        print("all else failed")


def sign_up():
    # create new window

    window1 = tkinter.Toplevel()
    window1.geometry("300x200")
    window1.title("Sign up")
    window1.configure(bg="grey26")
    # create buttons and labels

    # username
    label_sign_up = tkinter.Label(window1, text="Enter new username", bg="indian red1")
    signup_username = tkinter.Entry(window1, bg="gold")
    label_sign_up.pack()
    signup_username.pack()

    # password
    label_sign_up_password = tkinter.Label(window1, text="Enter new password", bg="indian red1")
    signup_password = tkinter.Entry(window1, bg="gold")
    label_sign_up_password.pack()
    signup_password.pack()

    # age
    label_enter_age = tkinter.Label(window1, text="Enter your age", bg="indian red1")
    signup_age = tkinter.Entry(window1, bg="gold")
    label_enter_age.pack()
    signup_age.pack()

    # confirm account
    button_account = tkinter.Button(window1, text="Create account", bg="red", command=lambda: sign_up_check())
    button_account.pack()


# log in button set up

button_login = tkinter.Button(window, text="Log in", bg="gold", command=lambda: log_in())
button_login.pack()

# sign up button


button_signup = tkinter.Button(window, text="Sign up", bg="gold", command=lambda: sign_up())
button_signup.pack()

window.mainloop()

1 个答案:

答案 0 :(得分:0)

您行为的主要原因是函数中本地定义的变量位于本地范围内。 这意味着它们仅在您的本地功能中可用,而不在其外部。

就像马丁诺指出的那样,你在函数sign_up_check中输入了一个错字。

使用全局变量:

  • 为了完成这项工作,需要在全局范围内创建在函数内创建的每个窗口小部件。

    def login():
        global enter_username, enter_password
        # [... The rest of the code ...]
    def sign_up_check():
        global signup_username, signup_password, signup_age
        # [... The rest of the code ...]

您是否有任何特定且与程序相关的原因,您不想使用类?

老实说,在使用python编写图形用户界面时,我认为使用全局变量和过程编程没有任何优势。

我强烈建议使用面向对象的编程风格来改进代码维护并减少冗余编码量(开始时的全局声明,每个函数的全局使用等等)。