欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

用tkinter时,想要Entry的值输出,调用了get(),报错:AttributeError: 'NoneType' object has no attribute 'get'

程序员文章站 2022-04-28 19:32:21
...

用tkinter时,想要Entry的值输出,调用了get(),报错:AttributeError: ‘NoneType’ object has no attribute ‘get’

原来代码:

from tkinter import *
root = Tk()
theLabel1=Label(root,text="作品:")
theLabel1.grid(row=0,column=0)
theLabel2=Label(root,text="作者:")
theLabel2.grid(row=1,column=0)
e1=Entry(root).grid(row=0,column=1,)
e2=Entry(root).grid(row=1,column=1)
def show():
 print("作品:《%s》" % e1.get())
 print("作品:%s "% e2.get())
Button(root,text="搜索",width=10,command=show).grid(row=3,column=0,sticky=W,padx=10,pady=5)
Button(root,text="退出",width=10,command=root.quit).grid(row=3,column=1,sticky=E,padx=10,pady=5)
root.mainloop()

原因在e1和e2的定义上,改为:

e1=Entry(root)
e1.grid(row=0,column=1,)
e2=Entry(root)
e2.grid(row=1,column=1)