-3

While using Tkinter to create an application, I encountered the an error with the destroy function. Here is my Code:

from tkinter import *
root = Tk()

def show():
    global myl
    myl = Label(root,text='Done!!!').pack()

def ref():
    myl.destroy()
    
b1 = Button(root,text='Submit',font=('ariel',16),command = show).pack()
b2 = Button(root,text='Refresh',font=('cambria',16),command=ref).pack()

root.mainloop()

and the Error I am getting:

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Python\Python37\lib\tkinter\__init__.py", line 1705, in __call__       
    return self.func(*args)
  File "c:\Users\dhrit\OneDrive\Desktop\Dhriti\python\test101.py", line 10, in ref
    myl.destroy()
AttributeError: 'NoneType' object has no attribute 'destroy'

How can I fix this bug?

quamrana
  • 37,849
  • 12
  • 53
  • 71
Bhavya Vohra
  • 13
  • 1
  • 2
  • 4
    Does this answer your question? [Tkinter: AttributeError: NoneType object has no attribute ](https://stackoverflow.com/questions/1101750/tkinter-attributeerror-nonetype-object-has-no-attribute-attribute-name) – Wups Sep 12 '20 at 12:56

1 Answers1

3

Inside the show() you have to say:

def show():
    global myl
    myl = Label(root,text='Done!!!')
    myl.pack()

All your Label and other widgets should be "put on the screen", on a separate line.

Why? Because myl = Label(..).pack() returns None. When you call destroy() method or any other methods on myl, your saying myl = Label(..).pack().destroy() which does not exist. So you have to pack() it in a separate line.

Delrius Euphoria
  • 14,910
  • 3
  • 15
  • 46