我的PhotoImage没有显示在我的窗口中

问题描述 投票:0回答:1

我正在尝试使用Label方法将图像放在窗口上。但是,当我place主窗口小部件中的图像时,它不显示。

这是我的代码:

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()

仅供参考:端口画布不会覆盖整个窗口,并且墙纸图像与python图像不在同一目录中。另外,我没有任何错误。

谢谢!

python python-3.x tkinter label tkinter-canvas
1个回答
0
投票

submit()函数退出时,对图像的引用是垃圾回收。保存对标签的引用:

p = PhotoImage(master = Desktop, file = "c:\\Users\\offcampus\\Downloads\\wallpaper.gif")
Wall.image = p    # Save reference
© www.soinside.com 2019 - 2024. All rights reserved.