在Tkinter Python中删除
我正在尝试使用Python在Tkinter中创建一个登录系统。 因此,我想这样做的方式是询问用户的用户名。当他们单击“下一步”时,“用户名”字段应消失,而应显示一个名为“密码”的新字段。所以当我尝试这样做时,我得到了一个错误:在Tkinter Python中删除,python,tkinter,Python,Tkinter,我正在尝试使用Python在Tkinter中创建一个登录系统。 因此,我想这样做的方式是询问用户的用户名。当他们单击“下一步”时,“用户名”字段应消失,而应显示一个名为“密码”的新字段。所以当我尝试这样做时,我得到了一个错误: e1 is not defined in line 27, in button 我知道变量尚未声明,但它应该已经声明了,因为我已经将所有内容都放在main()函数中。 这是我的密码 from tkinter import * from tkinter import m
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()
从tkinter导入*
从tkinter导入消息框
root=Tk()
root.title(“登录系统”)
根几何(“300x200”)
def main():
e1=条目(根)#将e1移到此处
def username_page():
标签(root,text=“Username”).place(x=10,y=10)
e1.位置(x=140,y=10)
uname=e1.get()
def password_page():
标签(root,text=“Password”).place(x=10,y=40)
e2=条目(根)
e2.位置(x=140,y=40)
e2.config(show=“*”)
def按钮():
#隐藏用户名输入字段
e1.地点
#显示密码输入字段
密码(第页)
用户名\页面()
按钮(根,text=“Next”,command=Button,高度=3,宽度=35,背景=“蓝色”,前景=“白色”)。放置(x=10,y=100)
root.mainloop()
main()
e1
是在username\u page()
函数的作用域中声明的,因此它只能在其中访问,不能在外部访问。username\u page()
是在main()
中声明的,这并不意味着e1
是在main()
的范围内声明的
而且,
e1.delete()
不起作用,我相信您想要e1.place\u forget()
。当您希望e1
再次可见时,您可以调用.place()
,就像您在username\u page()
中所做的那样,有一种非常简单的方法可以做到这一点,无需到处移动行,即将e1
声明为全局
此外,隐藏并不意味着删除,请使用e1.place\u-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()
e1.delete()
或e1.delete(0)
未隐藏Entry@SamakshGupta你是对的,我错了。我会编辑它的。嗯,很抱歉,代码上说要隐藏,销毁不是隐藏的替代方法。可能使用.place\u-forget()
?@SamakshGupta再次更正,我没有意识到!谢谢我会删除我的答案,留下你的答案,因为我们的程序几乎完全相同,但我不喜欢在几乎所有情况下都使用globals,所以我宁愿让OP选择两者。哈哈,是的,我们的答案非常相同,但我担心你先回答,所以你不应该删除:)酷!这是可行的,但不会删除用户名文本。有类似的方法吗?@lukas-Answer更新为包含它:)