Image on a button

Unrelated answer, but this is the answer I was looking for when I first came here. Use this to resize the image before adding it to the button.

from PIL import Image, ImageTk

image = Image.open("path/to/image.png")
image = image.resize((25, 25), Image.ANTIALIAS)
self.reset_img = ImageTk.PhotoImage(image)
self.button = tk.Button(frame, image=self.reset_img)

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.


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

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()