How to fix an attribute error when clearing an entry box
Question:
While I was writing my code I stumbled over this error:
Exception in Tkinter callback
Traceback (most recent call last):
File "C:Python33libtkinter__init__.py", line 1482, in __call__
return self.func(*args)
File "C:UsersBryanDesktopPython overhoorprogramma.py", line 18, in verify
oentry.delete(0,END)
AttributeError: 'NoneType' object has no attribute 'delete'
I have already used the search function, but it didn’t seem to solve my problem. What I want to achieve is clearing an entry box.
# Maak een grafische interface / Create a graphical interface
gui = Tk()
gui.resizable(width=FALSE, height=FALSE)
# Verklaringen / Definitions
def verify():
overify = antwoord.get()
if overify == Nederlandsevertaling:
ctypes.windll.user32.MessageBoxA(0, "Goed gedaan", "werkend", 1)
oentry.delete(0,END)
else:
ctypefs.windll.user32.MessageBoxA(0, "Slecht gedaan", "Werkend", 1)
oentry.delete()
antwoord = StringVar()
willekeurig = random.randint(0,9)
# Laad de database / Load the database
cnx = mysql.connector.connect(user='-', password='-', host='-', database='Overhoor')
cursor = cnx.cursor()
query = "SELECT FRwoord, NLwoord FROM unite8app1 WHERE id=%s"
cursor.execute(query, (willekeurig,))
for (FRwoord, NLwoord) in cursor:
Fransevertaling = FRwoord
Nederlandsevertaling = NLwoord
# Uiterlijk van het venster / Appearance of the window
gui.configure(background="white")
gui.title("Overhoorprogramma - Bryan")
# Grafische objecten / Graphical objects
style = ttk.Style()
olabel = ttk.Label(gui,text=Fransevertaling,font=("Times", 18), background="white").grid(row=0, column=0,padx=5, pady=5, sticky="W")
oentry = ttk.Entry(gui,textvariable=antwoord, font=("Times", 18)).grid(row=1, column=0,padx=5, pady=5)
obutton = ttk.Button(gui,text="Suivant", command = verify).grid(row=1, column=1,padx=5, pady=5)
# Stop de grafische interface
gui.mainloop()
oentry = ttk.Entry(gui,textvariable=antwoord, font=("Times", 18)).grid(row=1, column=0,padx=5, pady=5)
obutton = ttk.Button(gui,text="Suivant", command = verify).grid(row=1, column=1,padx=5, pady=5)
If you press obutton it’s supposed to clear the text in oentry. For this I tried to use:
oentry.delete(0,END)
And a couple of variations on .delete. Instead of clearing the textbox it gives back an error.
Exception in Tkinter callback
Traceback (most recent call last):
File "C:Python33libtkinter__init__.py", line 1482, in __call__
return self.func(*args)
File "C:UsersBryanDesktopPython overhoorprogramma.py", line 18, in verify
oentry.delete(0,END)
AttributeError: 'NoneType' object has no attribute 'delete'
Is there a fix?
Answers:
The grid
method of every Tkinter widget works in-place (it always returns None
).
In other words, every line like this:
olabel = ttk.Label(gui,text=Fransevertaling,font=("Times", 18), background="white").grid(row=0, column=0,padx=5, pady=5, sticky="W")
should be written like this:
olabel = ttk.Label(gui,text=Fransevertaling,font=("Times", 18), background="white")
olabel.grid(row=0, column=0,padx=5, pady=5, sticky="W")
with grid
called on its own line.
I had the delete and insert statement preceding the actual "creation" of the Text box as such:
my_text.delete(1.0, tk.END)
my_text.insert(tk.INSERT, title)
my_text = tk.Text(frame2, width=40, height=10, font=("Helvetica", 18))
my_text.pack(pady=25, padx=25)
The creation coming first fixed it:
my_text = tk.Text(frame2, width=40, height=10, font=("Helvetica", 18))
my_text.pack(pady=25, padx=25)
my_text.delete(1.0, tk.END)
my_text.insert(tk.INSERT, title)
While I was writing my code I stumbled over this error:
Exception in Tkinter callback
Traceback (most recent call last):
File "C:Python33libtkinter__init__.py", line 1482, in __call__
return self.func(*args)
File "C:UsersBryanDesktopPython overhoorprogramma.py", line 18, in verify
oentry.delete(0,END)
AttributeError: 'NoneType' object has no attribute 'delete'
I have already used the search function, but it didn’t seem to solve my problem. What I want to achieve is clearing an entry box.
# Maak een grafische interface / Create a graphical interface
gui = Tk()
gui.resizable(width=FALSE, height=FALSE)
# Verklaringen / Definitions
def verify():
overify = antwoord.get()
if overify == Nederlandsevertaling:
ctypes.windll.user32.MessageBoxA(0, "Goed gedaan", "werkend", 1)
oentry.delete(0,END)
else:
ctypefs.windll.user32.MessageBoxA(0, "Slecht gedaan", "Werkend", 1)
oentry.delete()
antwoord = StringVar()
willekeurig = random.randint(0,9)
# Laad de database / Load the database
cnx = mysql.connector.connect(user='-', password='-', host='-', database='Overhoor')
cursor = cnx.cursor()
query = "SELECT FRwoord, NLwoord FROM unite8app1 WHERE id=%s"
cursor.execute(query, (willekeurig,))
for (FRwoord, NLwoord) in cursor:
Fransevertaling = FRwoord
Nederlandsevertaling = NLwoord
# Uiterlijk van het venster / Appearance of the window
gui.configure(background="white")
gui.title("Overhoorprogramma - Bryan")
# Grafische objecten / Graphical objects
style = ttk.Style()
olabel = ttk.Label(gui,text=Fransevertaling,font=("Times", 18), background="white").grid(row=0, column=0,padx=5, pady=5, sticky="W")
oentry = ttk.Entry(gui,textvariable=antwoord, font=("Times", 18)).grid(row=1, column=0,padx=5, pady=5)
obutton = ttk.Button(gui,text="Suivant", command = verify).grid(row=1, column=1,padx=5, pady=5)
# Stop de grafische interface
gui.mainloop()
oentry = ttk.Entry(gui,textvariable=antwoord, font=("Times", 18)).grid(row=1, column=0,padx=5, pady=5)
obutton = ttk.Button(gui,text="Suivant", command = verify).grid(row=1, column=1,padx=5, pady=5)
If you press obutton it’s supposed to clear the text in oentry. For this I tried to use:
oentry.delete(0,END)
And a couple of variations on .delete. Instead of clearing the textbox it gives back an error.
Exception in Tkinter callback
Traceback (most recent call last):
File "C:Python33libtkinter__init__.py", line 1482, in __call__
return self.func(*args)
File "C:UsersBryanDesktopPython overhoorprogramma.py", line 18, in verify
oentry.delete(0,END)
AttributeError: 'NoneType' object has no attribute 'delete'
Is there a fix?
The grid
method of every Tkinter widget works in-place (it always returns None
).
In other words, every line like this:
olabel = ttk.Label(gui,text=Fransevertaling,font=("Times", 18), background="white").grid(row=0, column=0,padx=5, pady=5, sticky="W")
should be written like this:
olabel = ttk.Label(gui,text=Fransevertaling,font=("Times", 18), background="white")
olabel.grid(row=0, column=0,padx=5, pady=5, sticky="W")
with grid
called on its own line.
I had the delete and insert statement preceding the actual "creation" of the Text box as such:
my_text.delete(1.0, tk.END)
my_text.insert(tk.INSERT, title)
my_text = tk.Text(frame2, width=40, height=10, font=("Helvetica", 18))
my_text.pack(pady=25, padx=25)
The creation coming first fixed it:
my_text = tk.Text(frame2, width=40, height=10, font=("Helvetica", 18))
my_text.pack(pady=25, padx=25)
my_text.delete(1.0, tk.END)
my_text.insert(tk.INSERT, title)