Tkinter: AttributeError: NoneType object has no attribute <attribute name>

Arnkrishn picture Arnkrishn · Jul 9, 2009 · Viewed 88.7k times · Source

I've created this simple 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()

I get the UI up and running. When I click on the Grab button, I get the following error on the console:

C:\Python> python.exe myFiles\testBed.py
Exception in Tkinter callback
Traceback (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'

Why is entryBox set to None?

Answer

Nick Meharry picture Nick Meharry · Jul 9, 2009

The grid, pack and place functions of the Entry object and of all other widgets returns None. In python when you do a().b(), the result of the expression is whatever b() returns, therefore Entry(...).grid(...) will return None.

You should split that on to two lines like this:

entryBox = Entry(root, width=60)
entryBox.grid(row=2, column=1, sticky=W)

That way you get your Entry reference stored in entryBox and it's laid out like you expect. This has a bonus side effect of making your layout easier to understand and maintain if you collect all of your grid and/or pack statements in blocks.