-1

I am trying to put an image on a window using the Label method. However, when I place the image in the master widget, it doesn't display.

Here is my code:

from tkinter import *
import os
import math
from random import randint, choice



window = Tk()
window.title("Welcome to Nutshell OS")
window.geometry("500x500+0+0")
window.attributes("-fullscreen", True)
user_entered = Entry(window, show = "•")

user_entered.place(relx = 0.5, rely = 0.6, anchor = "center")

password = ""

def submit(event = "<Return>"):
    if user_entered.get() != password:
        cree = Label(window, text = "The password you entered is incorrect").place(relx = 0.5, rely = 0.63, anchor = "center")
    else:
        window.withdraw()
        Desktop = Toplevel()
        Desktop.attributes("-fullscreen", True)

        taskwidth = Desktop.winfo_screenwidth()
        taskheight = Desktop.winfo_screenheight()
        Port = Canvas(Desktop, height = 0.1 * (taskheight), width = taskwidth, bg = "blue")
        Port.place(relx = 0.5, rely = 0.995, anchor = "center")

        p = PhotoImage(master = Desktop, file = "c:\\Users\\offcampus\\Downloads\\wallpaper.gif")
        Wall = Label(Desktop, image = p)
        Wall.place(relx = 0.5, rely = 0.5, anchor = "center")
window.bind("<Return>", submit)

submit_button = Button(window, text = "➡️", command = submit, bg = "light blue")
submit_button.place(relx = 0.535, rely = 0.6, anchor = "center")
window.mainloop()

FYI: The port canvas doesn't cover the entire window, and the wallpaper image isn't in the same directory as the python image. Also, I am not getting any error.

Thanks in advance!

10 Rep
  • 2,217
  • 7
  • 19
  • 33

1 Answers1

2

The reference to the image is garbage collected when the submit() function exits. Save a reference to the label:

p = PhotoImage(master = Desktop, file = "c:\\Users\\offcampus\\Downloads\\wallpaper.gif")
Wall.image = p    # Save reference
figbeam
  • 7,001
  • 2
  • 12
  • 18