tkinter button not showing image tkinter button not showing image tkinter tkinter

tkinter button not showing image


Depending on how the rest of your program is structured, your image might be getting cleared by garbage-collection:

From http://effbot.org/tkinterbook/photoimage.htm

Note: When a PhotoImage object is garbage-collected by Python (e.g. when you return from a function which stored an image in a local variable), the image is cleared even if it’s being displayed by a Tkinter widget.

To avoid this, the program must keep an extra reference to the image object. A simple way to do this is to assign the image to a widget attribute, like this:

label = Label(image=photo)label.image = photo # keep a reference!label.pack()

In your case, you can start your function by declaring img1 as a global variable to retain a reference:

global img1

or, if you already have img1 elsewhere in your program:

img1 = PhotoImage(file="C:/Users/Josh Bailey/Desktop/pi_dmx/Gif/mainmenu.gif")img1Btn = Button(recw, image=img1, command=rec_preset_1)img1Btn.image = img1img1Btn.grid(row=1, column=1)