Image not displayed when using it as a background in Tkinter Image not displayed when using it as a background in Tkinter tkinter tkinter

Image not displayed when using it as a background in Tkinter


It should be quite simple. When you're working with images in tkinter you always need to set a reference to that image on the widget you used it on. In other words do something like this:

from tkinter import Tk,Label,PhotoImageroot = Tk()img = PhotoImage(file='background.png')small_img=img.subsample(2, 2)background_label = Label(root, image=small_img)background_label.img=imgbackground_label.place(x=0, y=0, relheight=1, relwidth=1)root.mainloop()

Especially when you create the Label holding the image in a functions scope or something similar it's important to set that reference since the variable to that Labels instance will be gone after the function call ends.

PS: To be more clear about master widgets and for small performance improvements always give a widget a master widget when initializing it. I know tkinter also automatically assigns the last created Tk instance as master itself but a) does that need unneccesary computing power and b) is it easier to keep track of what widget is a child of another and so on. ;)