NoneType 对象没有要获取的属性(Tkinter)

Cry*_*row 2 python tkinter

我不知道为什么这不起作用,而且真的很烦人,

from tkinter import *

root = Tk()
def do(r, a):
    s = r.get()
    p = a.get()
Button(root, text="DEL3TE", fg="red", command=lambda: do(r, a)).grid(row=0, column=0)
r = Entry(root, width=15, bg="white").grid(row=0, column=1)
a = Entry(root, width=15, bg="white").grid(row=1, column=1)
Label(text="text1").grid(row=1, column=2)
Label(text="text2").grid(row=0, column=2)
Label(text="You have obtained the death note", fg="red").grid(row=2, column=0)
mainloop()
Run Code Online (Sandbox Code Playgroud)

错误代码如下:

Traceback (most recent call last):
  File "C:\Users\anglc\AppData\Local\Programs\Python\Python37-32\lib\tkinter\__init__.py", line 1705, in __call__
    return self.func(*args)
  File "C:/Users/anglc/Desktop/test.py", line 7, in <lambda>
    Button(root, text="DEL3TE", fg="red", command=lambda: do(r, a)).grid(row=0, column=0)
  File "C:/Users/anglc/Desktop/test.py", line 5, in do
    s = r.get()
AttributeError: 'NoneType' object has no attribute 'get'
Run Code Online (Sandbox Code Playgroud)

我不知道如何解决它,请帮助,谢谢!

Cry*_*row 11

如果您正在创建一个条目,请确保它不是这样的格式:

r = Entry(root, width=15, bg="white").grid(row=0, column=1)
Run Code Online (Sandbox Code Playgroud)

删除一个AttributeError应该是这样的:

r = Entry(root, width=15, bg="white")
r.grid(row=0, column=1)
Run Code Online (Sandbox Code Playgroud)