AttributeError: 'NoneType' object has no attribute 'delete'

22,622

Solution 1

In this line:

entryBox = tk.Entry(mainWindow, textvariable=v).grid(column=0, row=1)

grid doesn't return anything, so entryBox is None, which doesn't have a delete method. You have to set entryBox to tk.Entry(mainWindow, textvariable=v) then call the grid method on entryBox

Solution 2

The reason this is happening is because you are gridding it in the same variable. If you change your code to the following, it should work:

import Tkinter as tk
def main():
    mainWindow = tk.Tk()
    v = tk.StringVar()
    entryBox = tk.Entry(mainWindow, textvariable=v)
    def test():
        entryBox.delete(0,20)
    testButton = tk.Button(mainWindow, text='Go!', command=test, padx=10)
    testButton.grid(row=2, column=0) 
    entryBox.grid(column=0, row=1)
    tk.mainloop()
main()

The reason this works is because grid() does not return anything.

Share:
22,622

Related videos on Youtube

Phil J Fry
Author by

Phil J Fry

My dream is to be a programmer. I am currently a part-time coder at best. I don't even know the terminology yet. I was in the automotive industry until a couple years ago and just recently realized how much I love to program. Please don't get too mad at me for dumb questions. I am working on learning as hard as I can.

Updated on February 21, 2022

Comments

  • Phil J Fry
    Phil J Fry about 2 years

    I have run into this issue and I can't understand why.

    I took my code from my application and made this test code so you don't have to go through a bunch of junk to see what I am asking.

    I have this working in other code. But after comparing the two, I can't for the life of me figure this out.

    In this application, I get the error "AttributeError: 'NoneType' object has no attribute 'delete' ".

    import Tkinter as tk
    
    def main():
        mainWindow = tk.Tk()
        v = tk.StringVar()
        entryBox = tk.Entry(mainWindow, textvariable=v).grid(column=0, row=1)
        def test():
            entryBox.delete(0,20)
        testButton = tk.Button(mainWindow, text='Go!', command=test, padx=10).grid(row=2, column=0) 
        tk.mainloop()
    main()
    
  • Phil J Fry
    Phil J Fry over 11 years
    I will vote this up as soon as I build my reputation... That was the fix. Thank you !!!!!!
  • Phil J Fry
    Phil J Fry over 11 years
    This works. Thank you !!! I will come back and vote this up as soon as i build my reputation :) Thank you !!!
  • Russell Smith
    Russell Smith over 11 years
    No, that's simply not the problem in this specific case.