如何解决Tkinter图像未显示
我对编码还很陌生,我正在尝试在tkinter窗口中显示图像。
但是,当我在下面运行代码时,图像应该存在空白。这段代码也没有错误。
window2 = Toplevel()
window2.geometry("1920x1200")
Namearea = Label(window2,text="Please Name the Prebuild:")
Namearea.pack()
e = Entry(window2,width=50,borderwidth=3,bg="Light Grey",fg="black")
e.pack()
#Here is the part that is not working.
img3 = PhotoImage(file=r"C:\\Tkinter\\ComputerImage.png")
picture1 = Label(window2,image=img3)
picture1.pack()
SaveAndContinue = Button(window2,text="Save and Return to Main Menu",padx=75,pady=20,bg="Light Grey")
SaveAndContinue.pack()
解决方法
尝试以下问题的答案来自josav09: How to add an image in Tkinter?
from tkinter import *
from PIL import ImageTk,Image
import os
root = Tk()
img = ImageTk.PhotoImage(Image.open("True1.gif"))
panel = Label(root,image = img)
panel.pack(side = "bottom",fill = "both",expand = "yes")
root.mainloop()