【问题标题】:Tkinter in python: Unresolved referencepython中的Tkinter:未解决的参考
【发布时间】:2016-02-03 01:47:05
【问题描述】:

所以我做了一个简单的登录和注册页面,一切都很顺利,直到我尝试从我的注册页面的定义中获取我的变量。 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()

【问题讨论】:

  • 一方面在sign_up_check() 中你有password = signup_pasword.get() 而不是password = signup_password.get()。即使你解决了这个问题,signup_password 也是在sign_up() 函数中定义的局部变量(它不能在它之外被引用,并且一旦函数返回就会消失)。为了避免这个问题,你应该使用类(或许多人用来解决它的全局变量)。

标签: python python-3.x tkinter


【解决方案1】:

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

就像 martineau 指出的那样,您在函数 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。

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

【讨论】:

  • @RAPH43L 你能详细说明一下吗? GUI 往往都是关于副作用的;我会认为功能风格不匹配。我一直认为 GUI 是 OOP 真正闪耀的地方。我真的很想看一个功能风格的例子。能发个链接之类的吗?
  • @saulspatz 我不太确定你在哪里读到我的回答,因为“功能风格对 GUI 有好处”——无意冒犯。我的意思是指出,OOP 是编写 GUI 的“更好”方式,而功能性是一种缺乏很多东西的方法,并且包括像冗余代码、维护混乱等坏习惯。我不知道任何例子,但是简单的测试/实验,功能可以很好地用于 GUI
  • 你是对的。我也不知道我怎么看错了。我正在从眩晕症中恢复过来;也许这解释了它。我以为我的精神敏锐度已经恢复正常,但显然没有。道歉。
  • 你不必为此道歉 :) 这里没有冒犯 ;)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-02
  • 2021-08-17
  • 2016-12-14
  • 2020-05-27
  • 2019-11-16
  • 2021-01-24
相关资源
最近更新 更多