Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.3k views
in Technique[技术] by (71.8m points)

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

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()
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...