Peter Galiba Year 7
Peter Galiba Year 7

Reputation: 1

NoneType error always occurs and I can't remove anything from the tkinter window

from turtle import *
from tkinter import *
window = Tk()
window.config(background="green")
window.bind("<Escape>", quit)
def clogopen():
label = Label(window, bg="green", text="""Changelog:
0.0.1:
    Added title
    Added caps
0.0.2:
    Added Changelog button
    Added Changelog""").grid(row=5, column=0)
    window.after(2000, label.destroy_widget)#this is where I get the error
lab1 = Label(bg="red", fg="white",  text="Welcome to alphabet draw 0.0.2!").grid(rowspan=2, column=0)
cbttn = Checkbutton(text="Caps?").grid(row=3, column=0)
clogbttn = Button(bg="yellow", text="Open Changelog", command=clogopen).grid(row=4,  column=0)
speed(0)
window.mainloop()

I am currently adding the basics of the project, and the changelog is broken. The error is:

Exception in Tkinter callback Traceback (most recent call last):
File "/data/data/ru.iiec.pydroid3/files/arm-linux-androideabi/lib/python3.7/tkinter/init.py", line 1705, in call return self.func(*args) File "/data/data/ru.iiec.pydroid3/files/coding folder/alphabet_draw0.0.3.py", line 14, in clogopen window.after(2000, label.destroy_widget) AttributeError: 'NoneType' object has no attribute 'destroy_widget'

Upvotes: 0

Views: 146

Answers (1)

glhr
glhr

Reputation: 4547

The grid() method returns None. So, with label = Label(window, bg="green", text="""...""").grid(row=5, column=0), you're essentially setting label to None, hence the NoneType error.

Instead, get the label object first, and apply the grid afterwards:

label = Label(window, bg="green", text="""...""")
label.grid(row=5, column=0)

And to destroy label, use label.destroy (not label.destroy_widget):

window.after(2000, label.destroy)

Full code and demo: https://repl.it/@glhr/55705699-tkinter-turtle

Upvotes: 1

Related Questions