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

在使用Tkinter库创建GUI应用时,尝试通过get()方法获取Entry组件的输入值,但遇到了AttributeError,提示'NoneType' object has no attribute 'get'。错误源在于Entry对象的定义。修正代码后问题解决。

用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)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值