我的 PhotoImage 没有显示在我的 window 中
My PhotoImage isn't being displayed in my window
我正在尝试使用 Label
方法将图像放在 window 上。但是,当我 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()
仅供参考:端口 canvas 并未覆盖整个 window,并且墙纸图像与 python 图像不在同一目录中。另外,我没有收到任何错误。
提前致谢!
当 submit()
函数退出时,对图像的引用被垃圾回收。保存对标签的引用:
p = PhotoImage(master = Desktop, file = "c:\Users\offcampus\Downloads\wallpaper.gif")
Wall.image = p # Save reference
我正在尝试使用 Label
方法将图像放在 window 上。但是,当我 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()
仅供参考:端口 canvas 并未覆盖整个 window,并且墙纸图像与 python 图像不在同一目录中。另外,我没有收到任何错误。
提前致谢!
当 submit()
函数退出时,对图像的引用被垃圾回收。保存对标签的引用:
p = PhotoImage(master = Desktop, file = "c:\Users\offcampus\Downloads\wallpaper.gif")
Wall.image = p # Save reference