0

Code not displaying the image

home = PhotoImage(file = 'homebutton.png')
Button(hotfood, image = home, bg = 'white', command = 
hotfood.destroy).grid(row = 0, column = 0, sticky = EW)
return home

Code displaying image

logo = PhotoImage(file = 'logo.PNG')
Label (order, image = logo, bg = 'white').grid(row = 0, column = 0, 
sticky = E)
  • It's impossible to determine from this snippet but this type of issue occurs if you don't keep a reference to the [`PhotoImage`](https://effbot.org/tkinterbook/photoimage.htm) object and it gets garbage collected. – ASaunders Jun 27 '19 at 22:54
  • the top code is in a def function and the bottom one isnt not sure if that helps but its all i got – Carlos Rojas Jun 28 '19 at 09:17
  • That does help. The variables that you assign in a function scope are discarded once the function finishes execution and we exit that scope. Since `home` is declared inside the function, its reference is lost when the function exits and the image is not displayed. If you `return home` at the end of your function call (or just add it to whatever existing returns you have), you will keep the reference in your workspace and the image should stay visible as it does in your second snippet. – ASaunders Jun 28 '19 at 18:20
  • i added the return home to the end of my function but it still is not working, i have updated the top code to what it looks like right now – Carlos Rojas Jun 29 '19 at 23:41
  • Returning the image object isn't sufficient, you have to store a reference to it somewhere that will live for as long as the widget displaying the image. – jasonharper Jun 30 '19 at 02:10

0 Answers0