0

##importing tkinter

from tkinter import *

###function to destroy button

def hide():
       b.pack_forget()
       l=Label(r,text="destryed").pack()

##main body

r=Tk()
r.geometry("1000x1000")


b=Button(r,text="click",command=hide()).pack()



r.mainloop()
acw1668
  • 40,144
  • 5
  • 22
  • 34
tanvi
  • 17
  • 1
  • 4
  • Using `b=Button(r,text="click",command=hide()).pack()` is highly discouraged. The reason is that whatever .pack() returns (always a None type) will be stored in P1 and not the Button object. – PCM Jun 10 '21 at 10:17

1 Answers1

1

The problem lies in the definition of b. If you want b to be the button, you can't pack it on the same line.

The pack() method will always return None as there is no return. So, you define your variable b as None. Instead, first define the button and then pack it:

b=Button(r,text="click",command=hide())
b.pack()

So, the button is stored in variable b and you can destroy it.

Martin Wettstein
  • 2,771
  • 2
  • 9
  • 15