Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Tkinter:AttributeError:NoneType对象没有属性<;属性名称>;_Python_User Interface_Tkinter - Fatal编程技术网

Python Tkinter:AttributeError:NoneType对象没有属性<;属性名称>;

Python Tkinter:AttributeError:NoneType对象没有属性<;属性名称>;,python,user-interface,tkinter,Python,User Interface,Tkinter,我创建了这个简单的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('&l

我创建了这个简单的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()

为什么将
entryBox
设置为
None

网格
pack
place
对象和所有其他小部件的
函数返回
None
。在python中,当执行
a().b()
时,表达式的结果是
b()
返回的值,因此
条目(…)。网格(…)
将返回

您应该将其拆分为两行,如下所示:

entryBox = Entry(root, width=60)
entryBox.grid(row=2, column=1, sticky=W)
这样,您就可以将
条目
引用存储在
条目框
中,并按照您的预期进行布局。如果您将所有
网格
和/或
pack
语句分块收集,这会产生额外的副作用,使布局更易于理解和维护。

更改此行:

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
所做的一样

对于
entryBox.get()
要访问
get()
方法,您需要Entry对象,但
条目(根,宽度=60)。网格(行=2,列=1,粘性=W)
返回无

entryBox=Entry(根,宽度=60)
创建一个新的Entry对象

此外,您不需要
entryBox=entryBox.grid(row=2,column=1,sticky=W)
因为它将重写
entryBox


只需替换entryBox=entryBox.grid(行=2,列=1,粘性=W)


谢谢你,亚历克斯。我应该想到这一点:-)这不只是公认答案的复制品,而是11年后的复制品吗?
entryBox=Entry(root,width=60)
entryBox.grid(row=2, column=1,sticky=W)
entryBox = Entry(root, width=60)
entryBox.grid(row=2, column=1, sticky=W)