从Tkinter Entry组件获取值时遭遇NameError问题求助
解决Tkinter中Entry组件值获取时的NameError问题
错误原因
e1是login()函数内的局部变量,after_login()作为独立函数无法访问其他函数的局部作用域变量,因此触发NameError: name 'e1' is not defined。
另外原代码还有两处小问题:
- 同时混用
pack()和place()布局,会导致布局冲突; messagebox.showinfo()参数格式错误,且未添加窗口主循环mainloop(),窗口会直接闪退。
解决方案
方案1:使用全局变量
将e1声明为全局变量,让after_login()可以访问:
from tkinter import Tk, Label, Entry, Button, messagebox def login(): global e1 # 声明e1为全局变量 root_login = Tk() root_login.title("Login") root_login.geometry("400x400") label_text_name = Label(root_login, text="Type mail your :") label_text_name.place(x=25, y=50) label_text_token = Label(root_login, text="Type Pasword :" ) label_text_token.place(x=58, y=150) input_text_email = Entry(root_login, width=30) input_text_email.place(x=150, y=50) input_text_token = Entry(root_login, width=30) input_text_token.place(x=150, y=150) e1 = Entry(root_login) e1.pack() btn_login = Button(root_login,command=after_login, text="Login", height=1, width=10 ) btn_login.place(x=50, y=250) root_login.mainloop() # 添加主循环维持窗口 def after_login(): global e1 # 声明使用全局变量e1 var = e1.get() messagebox.showinfo("提示", var) # 修正messagebox参数:标题+内容 if __name__ == "__main__": login()
方案2:嵌套函数(推荐,避免全局变量污染)
将after_login()嵌套在login()内部,直接访问外层函数的局部变量:
from tkinter import Tk, Label, Entry, Button, messagebox def login(): root_login = Tk() root_login.title("Login") root_login.geometry("400x400") label_text_name = Label(root_login, text="Type mail your :") label_text_name.place(x=25, y=50) label_text_token = Label(root_login, text="Type Pasword :" ) label_text_token.place(x=58, y=150) input_text_email = Entry(root_login, width=30) input_text_email.place(x=150, y=50) input_text_token = Entry(root_login, width=30) input_text_token.place(x=150, y=150) e1 = Entry(root_login) e1.pack() # 嵌套函数,直接访问外层的e1变量 def after_login(): var = e1.get() messagebox.showinfo("提示", var) btn_login = Button(root_login,command=after_login, text="Login", height=1, width=10 ) btn_login.place(x=50, y=250) root_login.mainloop() if __name__ == "__main__": login()
方案3:使用类(适合复杂界面扩展)
用面向对象方式将组件作为类属性,便于统一管理和访问:
from tkinter import Tk, Label, Entry, Button, messagebox class LoginWindow: def __init__(self): self.root_login = Tk() self.root_login.title("Login") self.root_login.geometry("400x400") self.create_widgets() def create_widgets(self): label_text_name = Label(self.root_login, text="Type mail your :") label_text_name.place(x=25, y=50) label_text_token = Label(self.root_login, text="Type Pasword :" ) label_text_token.place(x=58, y=150) self.input_text_email = Entry(self.root_login, width=30) self.input_text_email.place(x=150, y=50) self.input_text_token = Entry(self.root_login, width=30) self.input_text_token.place(x=150, y=150) self.e1 = Entry(self.root_login) self.e1.pack() btn_login = Button(self.root_login, command=self.after_login, text="Login", height=1, width=10 ) btn_login.place(x=50, y=250) def after_login(self): var = self.e1.get() messagebox.showinfo("提示", var) def run(self): self.root_login.mainloop() if __name__ == "__main__": app = LoginWindow() app.run()
内容的提问来源于stack exchange,提问作者van Thielen
相关产品推荐
相关产品推荐

