0

I've searched for an answer but it seems that no one has had this issue before. I don't know what I am missing. Here's a simplified version of the code, with the same problem:

app = tk.Tk()

location  = 'C:\\Users\\User\\Desktop'


image = location + '\\ab.PNG'
image_final = tk.PhotoImage(file = image)
var = tk.StringVar(app)
list_file=['1','2']



def do(event):

    if var.get() == 1:
        image = location + '\\cd.PNG'
    else:
        image = location + '\\ab.PNG'
    image_final = tk.PhotoImage(file = image)

    a.config(image = image_final)




style_optionMenu = ttk.Style()
style_optionMenu.configure('style_option.TMenubutton', background = "green", foreground = "white")
option_athletes = ttk.OptionMenu(app,var,list_file[0],*list_file, style = 'style_option.TMenubutton', command = do )
option_athletes.config(width = 20)
option_athletes.pack()    



a= tk.Label(master = app, image = image_final)
a.pack()


app.mainloop()

The problem is that, although displaying a first image, whenever I change the value in the OptioMenu, the image simply vanishes and doesn't reaper anymore. Everything else (not in this example code) is performing just fine. Does anyone have any clue or knows a better alternative to do this? Thank you very much!

Gonçalo
  • 13
  • 4
  • 1
    This is exactly the same problem as *every other Tkinter disappearing image problem ever asked*: you are referring to the image only via a local variable, so it gets garbage-collected before it ever becomes visible. Store a reference somewhere with a lifetime at least as long as the widget displaying it; `a.photo = image_final` perhaps. – jasonharper Jul 13 '18 at 16:12
  • of course...solved that by using a global on image_final. Thank you very much! – Gonçalo Jul 13 '18 at 17:05

0 Answers0