python中的Tkinter:未解析的引用

python中的Tkinter:未解析的引用,python,python-3.x,tkinter,Python,Python 3.x,Tkinter,所以我做了一个简单的登录和注册页面,一切都很顺利,直到我尝试从我的注册页面定义中获取变量。Python说,这是一个未解析的引用,应该可以解析类型匹配,除了可能有人可以帮助之外,其他人都可以。我也知道有一种更简单的方法可以通过使用类来添加页面,但我决定这样做。提前谢谢。p、 s错误在sign\u up\u check()代码中,变量是从sign\u up()定义中获取的 import tkinter # window set up window = tkinter.Tk() window.ge

所以我做了一个简单的登录和注册页面,一切都很顺利,直到我尝试从我的注册页面定义中获取变量。Python说,这是一个未解析的引用,应该可以解析类型匹配,除了可能有人可以帮助之外,其他人都可以。我也知道有一种更简单的方法可以通过使用类来添加页面,但我决定这样做。提前谢谢。p、 s错误在sign\u up\u check()代码中,变量是从sign\u 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()

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

正如martineau指出的,您在函数
注册检查
中输入了密码

使用全局变量:

  • 为了实现这一点,在函数中创建的每个小部件都需要全局创建
您不想使用类是否有任何特定的和编程相关的原因

老实说,在编写图形用户界面(尤其是python)时使用全局变量和过程编程并没有任何优势


我在此强烈推荐一种面向对象的编程风格,以改进代码的维护并减少冗余编码的数量(开始时的全局声明、每个函数中的全局使用等等)。

sign\u check()
中,您有
password=signup\u pasword.get()
而不是
password=signup\u password.get()
。即使您解决了这个问题,
signup\u password
也是在
sign\u up()
函数中定义的一个局部变量(该变量不能在函数外部引用,并将在函数返回时立即消失)。为了避免这个问题,你应该使用类(或全局变量,这是很多人为了避免这个问题而做的事情)。@RAPH43L你能详细说明一下吗?图形用户界面往往都是关于副作用的;我会认为功能性风格不适合。我一直认为GUI是OOP真正闪耀的地方。我真的很想看一个函数式的例子。你能发布一个链接或什么吗?@saulspatz我不确定你在哪里读到我的回答“功能风格有利于GUI”-无意冒犯。我想指出的是,OOP是编写GUI的“更好”方法,functional是一种缺少很多东西的方法,包括冗余代码、维护混乱等坏习惯。。我不知道任何例子,但简单的测试/实验功能可以很好地用于GUI。你是对的。我也不知道我是怎么误读的。我从眩晕发作中恢复过来;也许这就是原因。我以为我的智力已经恢复正常,但显然不是。道歉。你不必为此道歉:)这里没有冒犯;)

    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 ...]