我正在尝试使用Python在Tkinter中创建一个登录系统。因此,我要这样做的方法是要求用户输入用户名。当他们单击下一步时,用户名字段应消失,而应显示一个名为password的新字段。因此,当我尝试执行此操作时,我得到一个错误:
e1 is not defined in line 27, in button
我知道该变量尚未声明,但它应该完成声明,因为我已将所有内容放入main()函数中。
这是我的代码
from tkinter import *
from tkinter import messagebox
root = Tk()
root.title("Loginsystem")
root.geometry("300x200")
def main():
def username_page():
Label(root, text="Username").place(x=10, y=10)
e1 = Entry(root)
e1.place(x=140, y=10)
uname = e1.get()
def password_page():
Label(root, text="Password").place(x=10, y=40)
e2 = Entry(root)
e2.place(x=140, y=40)
e2.config(show="*")
def button():
#Hide the username input field
e1.delete()
#Show the password input field
password_page()
username_page()
Button(root, text="Next", command=button, height = 3, width = 35, background = "blue", foreground = "white").place(x=10, y=100)
root.mainloop()
main()
有一种非常简单的方法,无需在此四处e1
移动行,即声明为Global。
另外,隐藏并不意味着删除,用于e1.place_forget()
隐藏小部件
这是有效的代码:
from tkinter import *
root = Tk()
root.title("Loginsystem")
root.geometry("300x200")
def username_page():
global e1, myLabel
myLabel = Label(root, text="Username")
myLabel.place(x=10, y=10)
e1 = Entry(root)
e1.place(x=140, y=10)
uname = e1.get()
def password_page():
Label(root, text="Password").place(x=10, y=40)
e2 = Entry(root)
e2.place(x=140, y=40)
e2.config(show="*")
def button():
# Hide the username input field
e1.place_forget()
myLabel.place_forget()
# Show the password input field
password_page()
def main():
username_page()
Button(root, text="Next", command=button, height = 3, width = 35, background = "blue", foreground = "white").place(x=10, y=100)
root.mainloop()
main()
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句