0

I'm trying to set an image as a background with Tkinter. I'm doing this by using a label with an image and at first, it worked fine. However, when I try to do the same thing within a function that creates a new window all I get is an empty (all white) image.

This is the code that doesn't work:

def myWardrobeWindow():
    wardrobeWindow = tk.Toplevel(root)
    wardrobeWindow.title("My Wardrobe")
    wardrobeWindow.geometry("800x800")

    wardrobeBackgroundImage = tk.PhotoImage(file='simpleWallpaper3.png')
    wardrobeBackgroundLabel = tk.Label(wardrobeWindow, image=wardrobeBackgroundImage)
    wardrobeBackgroundLabel.pack()

And this is the code that does work:

root = tk.Tk()
root.resizable(width=False, height=False)

canvas = tk.Canvas(root, height=700, width=700)
canvas.pack()

backgroundImage = tk.PhotoImage(file='simpleWallpaper3.png')
backgroundLabel = tk.Label(root, image=backgroundImage)
backgroundLabel.place(relx=0, rely=0, relheight=1, relwidth=1)
Saad
  • 3,340
  • 2
  • 10
  • 32
Elias W.
  • 15
  • 7
  • Well, that is cuz variables of a function deletes when the function ends that is why we have the `return` functionality. Now coming to your issue please scroll down and read the **Note** [here](https://effbot.org/tkinterbook/photoimage.htm). – Saad Jun 28 '20 at 08:49
  • @Saad Oooh. Thank you very much! I'm still quite new to this :) But I get it now! – Elias W. Jun 28 '20 at 09:00

0 Answers0