0

When I use this code to execute an image inside a button within a frame it works just fine:

image2= PhotoImage(file="questionmark.gif")
f3buttonQ=Button(f3,image=image2, command = findcustomer).place(x=425,y=10)

However, when I just want to use this in the root frame I use this code:

image2= PhotoImage(file="questionmark.gif")
f3buttonQ=Button(root, image=image2,command = findcustomer).place(x=450,y=110)

The image doesn't load in the latter piece of code and the only difference between the two would be the f3 in the first one. Thanks.

Update:

So I managed to get the command to work by using this:

photo2= tk.PhotoImage(file="questionmark.gif")
button = Button(image=photo2,command = findcustomer).place(x=450,y=110)

When I run this alone the image is loaded just perfectly onto the button, but when I add this line under it they both go blank again and the image isn't loaded:

f3findProduct=Button(image=photo2, command = findproduct).place(x=110,y=190)
Coder101
  • 107
  • 2
  • 12
  • 2
    I would recommend providing a full test case so we can see what is going on – Danny Cullen Apr 08 '16 at 12:30
  • My guess is that in the second one the reference to image2 is not kept, according to [the official documentation](https://docs.python.org/2/library/tkinter.html#images) : _When the last Python reference to the image object is deleted, the image data is deleted as well._ – Tadhg McDonald-Jensen Apr 08 '16 at 12:43
  • I tried calling the image again as a new name, such as photo3 and then running the second button command but also seemed to cause them both to load empty. Do I need a duplicate copy of the image but save it with a different name and call each individual picture of each button? – Coder101 Apr 08 '16 at 12:49
  • @LHez, if you can provide a [Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve) that would be very helpful for answering this question, is the code you provided in the global scope or in a function? – Tadhg McDonald-Jensen Apr 08 '16 at 12:59
  • It was in a function but the question has been answered. Thanks for your help though! – Coder101 Apr 08 '16 at 13:01
  • Possible duplicate of [Cannot Display an Image in Tkinter](https://stackoverflow.com/questions/3359717/cannot-display-an-image-in-tkinter) – Nae Nov 25 '17 at 19:34

1 Answers1

1

The short story is that your image is garbage collected and is no longer displayed. You must keep a reference of it.

from effbot:

When you add a PhotoImage or other Image object to a Tkinter widget, you must keep your own reference to the image object. If you don’t, the image won’t always show up.

The problem is that the Tkinter/Tk interface doesn’t handle references to Image objects properly; the Tk widget will hold a reference to the internal object, but Tkinter does not. When Python’s garbage collector discards the Tkinter object, Tkinter tells Tk to release the image. But since the image is in use by a widget, Tk doesn’t destroy it. Not completely. It just blanks the image, making it completely transparent…

The solution is to make sure to keep a reference to the Tkinter object, for example by attaching it to a widget attribute:

photo = PhotoImage(...)

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

You must do that for each image you need to display

Reblochon Masque
  • 35,405
  • 10
  • 55
  • 80
  • The tkinter interface is very much handling the images correctly as using pythons garbage collection is the intended behaviour according to [the documentation](https://docs.python.org/2/library/tkinter.html#images) : _When the last Python reference to the image object is deleted, the image data is deleted as well,_ – Tadhg McDonald-Jensen Apr 08 '16 at 13:02