Search code examples
pythontkintertkinter-entry

how to fix AttributeError: 'NoneType' object has no attribute 'get' for tkinter entry


My tkinter entry is returning none.

My main code is having this same issue so I decided to write a smaller code to test and I had the same issue:

from tkinter import *

root = Tk()

a = Entry(root).grid(row = 0, column = 0)

b = Button(root,text = 'CLICK', command= lambda: test()).grid(row = 2, column = 2)

def test():
    print(a.get())


root.mainloop()

Solution

  • it's entry.grid(...) return None

    I think you want

        from tkinter import *
    
        root = Tk()
    
        a = Entry(root)
        a.grid(row = 0, column = 0)
    
        b = Button(root,text = 'CLICK', command= lambda: test()).grid(row = 2, column = 2)
    
        def test():
            print(a.get())
    
    
        root.mainloop()