86

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()

When I click on the Grab button, an error occurs:

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?


See also Why do I get AttributeError: 'NoneType' object has no attribute 'something'? for the general case.

Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
Arnkrishn
  • 29,828
  • 40
  • 114
  • 128
  • TODO: make a **fully general** canonical explaining built-in and library methods that return `None`, and the design decision behind that (command-query separation). There are many of them, but it is the **same problem** every time. That covers the other aspect of this problem - https://stackoverflow.com/questions/8949252 covers understanding the error message. – Karl Knechtel Mar 26 '23 at 05:48

4 Answers4

144

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.

Mike - SMT
  • 14,784
  • 4
  • 35
  • 79
Cassie Meharry
  • 2,633
  • 3
  • 20
  • 20
  • So single line coding is not just syntactical, it also has semantic ramifications which is very unexpected. – user7420124 Mar 16 '22 at 19:45
  • 2
    @user7420124, not really. The difference is what you assign to `entryBox`: in one case it's what `Entry()` returns, in another it's what `grid()` returns. – ChrisGPT was on strike Aug 14 '22 at 15:26
  • If we want adding a final button like this: tkinter.Button(root, text="Ok", command=lambda: [print(selection), root.destroy()]).pack() pack is require to add it but is not compatible with grid, how to deal that? – Jonathan Roy Nov 15 '22 at 16:44
  • 1
    It's only unexpected if you pay no attention to what a method returns. `a.b.c` simply is not the same as `a.b`, then `a.c`. – chepner Nov 19 '22 at 16:42
16

Change this line:

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

into these two lines:

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

Just as you already correctly do for grabBtn!

Aran-Fey
  • 39,665
  • 11
  • 104
  • 149
Alex Martelli
  • 854,459
  • 170
  • 1,222
  • 1,395
6

Alternative solution for Python3.8+ versions that allows to put all of this in one line using the walrus operator:

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

Now entryBox will refer to the Entry widget and also get packed.

For characters per line management I can suggest something like this:

(var := Button(
    text='fine', command=some_func, width=20, height=15, activebackground='grey'
)).grid(row=0, column=0, columnspan=0, rowspan=0, sticky='news')

But at that point might as well just do this "normally" (as suggested by other answers)

Sources:

Matiiss
  • 5,970
  • 2
  • 12
  • 29
3

For entryBox.get() to access get() method you need Entry object but Entry(root, width=60).grid(row=2, column=1, sticky=W) returns None.

entryBox = Entry(root, width=60) creates a new Entry Object.

Moreover, you won't need entryBox = entryBox.grid(row=2, column=1, sticky=W) as it will rewrite entryBox with None


Just replace entryBox = entryBox.grid(row=2, column=1, sticky=W) with

entryBox = Entry(root, width=60)
entryBox.grid(row=2, column=1, sticky=W)
7u5h4r
  • 459
  • 3
  • 10