Tkinter:AttributeError:NoneType对象没有属性get我创建了这个简单的GUI:from tkinter import *root = Tk()def grabText(event): print(entryBox.get()) entryBox = Entry(root, width=60).grid(row=2, column=1, sticky=W)grabBtn = Button(root, text="Grab")grabBtn.grid(row=8, column=1)grabBtn.bind('<Button-1>', grabText)root.mainloop()我启动并运行UI。当我单击Grab按钮时,我在控制台上收到以下错误:C:\Python> python.exe myFiles\testBed.pyException in Tkinter callbackTraceback (most recent call last): File "C:\Python\lib\lib-tk\Tkinter.py", line 1403, in __call__ return self.func(*args) File "myFiles\testBed.py", line 10, in grabText if entryBox.get().strip()=="":AttributeError: 'NoneType' object has no attribute 'get'我究竟做错了什么?
2 回答
![?](http://img1.sycdn.imooc.com/545869510001a20b02200220-100-100.jpg)
三国纷争
TA贡献1804条经验 获得超7个赞
改变这一行:
entryBox=Entry(root,width=60).grid(row=2, column=1,sticky=W)
分为以下两行:
entryBox=Entry(root,width=60) entryBox.grid(row=2, column=1,sticky=W)
正如你已经正确做的那样grabBtn
!
![?](http://img1.sycdn.imooc.com/5333a0780001a6e702200220-100-100.jpg)
蝴蝶不菲
TA贡献1810条经验 获得超4个赞
的grid
,pack
并且place
在功能Entry
对象和所有其他部件的回报None
。在python a().b()
中,表达式的结果是任何b()
返回,因此Entry(...).grid(...)
将返回None
。
你应该把它分成两行,如下所示:
entryBox = Entry(root, width=60) entryBox.grid(row=2, column=1, sticky=W)
通过这种方式,您可以将您的Entry
参考文件存储起来entryBox
并按照您的预期进行布局。如果您收集块中的所有grid
和/或pack
语句,这会产生额外的副作用,使您的布局更容易理解和维护。
添加回答
举报
0/150
提交
取消