Image on a button Image on a button tkinter tkinter

Image on a button


The only reference to the image object is a local variable. When __init__ exits, the local variable is garbage collected so the image is destroyed. In the second example, because the image is created at the global level it never goes out of scope and is therefore never garbage collected.

To work around this, save a reference to the image. For example, instead of photo use self.photo.


its work

x1=Button(root)photo=PhotoImage(file="Re.png")x1.config(image=photo,width="40",height="40",activebackground="black",bg="black", bd=0,command=sil)x1.place(relx=1,x=5, y=-5, anchor=NE)

but this is useless

def r():    x1=Button(root)    photo=PhotoImage(file="Re.png")    x1.config(image=photo,width="40",height="40",activebackground="black",    bg="black", bd=0,command=sil)    x1.place(relx=1,x=5, y=-5, anchor=NE)r()


logo = PhotoImage(file = 'mine32.gif')small_logo = logo.subsample(5, 5)self.b.config(image = small_logo , compound = LEFT )