AttributeError: 'NoneType' object has no attribute 'delete'
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.
Related videos on Youtube
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, 2022Comments
-
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 over 11 yearsI will vote this up as soon as I build my reputation... That was the fix. Thank you !!!!!!
-
Phil J Fry over 11 yearsThis works. Thank you !!! I will come back and vote this up as soon as i build my reputation :) Thank you !!!
-
Russell Smith over 11 yearsNo, that's simply not the problem in this specific case.