Tkinter in python:未解决的参考
所以我即将做一个简单的登录和注册页面,一切都很顺利,直到我尝试从我的注册页面中获取我的变量。 Python说这是一个未解决的参考,应该解决打字匹配和所有,但也许有人可以提供帮助。另外我知道有一种更简单的方式来添加页面,但我决定用这种方法来添加页面。提前致谢。 p.s的错误是在sign_up_check()代码中,这些变量是从sign_up()定义中获得的。Tkinter in python:未解决的参考
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指出的那样,函数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是OOP真正闪耀的地方。我真的很想看到功能风格的例子。你能发布一个链接或什么? – saulspatz
@saulspatz我不太清楚你在哪里阅读我的答案,因为“功能风格对GUI有好处” - 没有任何意图。我的意思是指出,OOP是编码GUI的“更好”方式,而功能是一种缺乏许多东西并且包含冗余代码之类的不良习惯,难以维护的方法等。 我不知道任何示例但简单的测试/实验功能可以很好地用于GUI – R4PH43L
你是对的。我不知道我是怎么误解它的。我正在从眩晕的袭击中恢复过来;也许这可以解释它。我认为我的精神状态已恢复正常,但显然不是。道歉。 – saulspatz
对于'sign_up_check()'中的一件事,你有'password = signup_pasword.get()'而不是'password = signup_password.get()'。即使你解决了这个问题,'signup_password'是在'sign_up()'函数中定义的局部变量(它不能在其外引用,并在函数返回时立即消失)。为了避免这个问题,你应该使用类(或者全局变量,这是许多人为了解决它而做的)。 – martineau